地址:http://erlend.oftedal.no/blog/?blogid=118

When building a ajax based application, you want to protect any POST request against CSRF attacks. If you are using jQuery, then jQuery provides a lot of convenience methods for ajax calls ($.get(), $.post(), $.getJSON() etc.) and it would be a shame if you would have to duplicate adding CSRF tokens to all your ajax calls manually or by going back to $.ajax(), because the convenience method didn't support the way you wanted to add the token. But jQuery, being the customizable framework it is, of course allows you to add these kinds of things through events.

Session based tokens

If you are using session based tokens, you probably generate a secure token when generating the session, and store that token in the session. When a request comes back to the server, you check that the token is included in the request and compare it to what's in the session. If it's the same token, you accept the request, if not you reject it.

To use this token with jQuery, you need to make it available to javascript. You typically do this by adding it as a javascript variable.

var csrf_token = '<%= token_value %>';

Next, the trick is to bind to the global ajaxSend event, and add the token to any POST request

$("body").bind("ajaxSend", function(elm, xhr, s){
if (s.type == "POST") {
xhr.setRequestHeader('X-CSRF-Token', csrf_token);
}
});

In the example above I add the token as a request header, but you could optionally add it as a form post parameter in stead.

Double-submit of cookie

When using double submit of cookie, you adjust the example above to extract the value of csrf_tokenfrom the cookies instead.

Update: Bug in jQuery 1.5.0

This does not work in jQuery 1.5.0 because of bug 8360. Looks like it will be fixed in 1.5.1. Works in 1.4.4.

[Security] Automatically adding CSRF tokens to ajax calls when using jQuery--转的更多相关文章

  1. [webgrid] – Ajax – (Reloading a Razor WebGrid after Ajax calls using a partial view)

    Reloading a Razor WebGrid after Ajax calls using a partial view If you are using Razor and MVC you p ...

  2. Spring Security Oauth2 : Possible CSRF detected

    Spring Security Oauth2 : Possible CSRF detected 使用Spring Security 作为 Oauth2 授权服务器时,在授权服务器登录授权后,重定向到客 ...

  3. jQuery Ajax calls and the Html.AntiForgeryToken()

    jQuery Ajax calls and the Html.AntiForgeryToken() https://stackoverflow.com/a/4074289/3782855 I use ...

  4. 原生态AJAX详解和jquery对AJAX的封装

    AJAX: A :Asynchronous [eI`sinkrenes] 异步 J :JavaScript    JavaScript脚本语言 A: And X :XML 可扩展标记语言 AJAX现在 ...

  5. ajax请求原理及jquery $.ajax封装全解析

    .ajax原理: Ajax的原理简单来说通过XmlHttpRequest对象来向服务器发异步请求,从服务器获得数据,然后用javascript来操作DOM而更新页面.这其中最关键的一步就是从服务器获得 ...

  6. $.ajax()方法详解 jquery

    $.ajax()方法详解   jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...

  7. jquery Ajax请求示例,jquery Ajax基本请求方法示例

    jquery Ajax请求示例,jquery Ajax基本请求方法示例 ================================ ©Copyright 蕃薯耀 2018年5月7日 https: ...

  8. 【Ajax 4】Ajax、JavaScript和JQuery的联系和区别

    导读:在之前,就分别学习了Ajax.JavaScript和JQuery,然后对于这三者之间的关系,是一直云里雾里的.尤其是后来学到了Ajax,就更是不明白了.现在,就给总结总结. 一.基本概述 1.1 ...

  9. 又一个ajax实例,结合jQuery

    又一个ajax实例,配合jQuery   html <!DOCTYPE html> <html lang="zh-cn"> <head> < ...

随机推荐

  1. oracleasm方式创建ASM

    1.准备oracleasm包 [root@localhost oracle]# uname -r 2.6.18-164.el5 [oracle@localhost ~]$ ls -l total 26 ...

  2. bzoj 1025 [SCOI2009]游戏(置换群,DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1025 [题意] 给定n,问1..n在不同的置换下变回原序列需要的不同排数有多少种. [ ...

  3. Ruby多字节字符的设计

    Perl.Python的多字节字符处理方式是UCS(Universal Code Set),Ruby的多字节字符处理方式是CSI(Code Set Independent).UCS的做法是,不管你读取 ...

  4. 第二百九十、一、二天 how can I 坚持

    上周日,腊八,好冷,去菏泽了.坐火车,没座.好冷,下午菏泽学院,大体看了看,很是怀念,有点遗憾,在网吧玩的时间太长了,留不住. 周一,长太婚礼,竟然没有只是吃了个饭,有点小失望,下午坐车回京,两点坐的 ...

  5. 【转】Maven实战(六)--- dependencies与dependencyManagement的区别

    原博文出自于:http://blog.csdn.net/liutengteng130/article/details/46991829   感谢! 在上一个项目中遇到一些jar包冲突的问题,之后还有很 ...

  6. Java设计模式系列之迭代器模式

    迭代器模式定义 迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示. 迭代器模式的角色构成 (1)迭代器角色(Iterator):定义遍历元素所需 ...

  7. iOS开发-为程序添加应用设置

    一.设置捆绑包 设置捆绑包是应用自带的一组文件,用于告诉设置该应用期望得到用户的哪些偏好设置. 新建设置捆绑包:Command+N,在iOS部分中的Resource,选择Settings Bundle ...

  8. Thinkphp框架 -- ajax无刷新上传图片

    用Thinkphp框架做无刷新上传图片 视图层 View <!doctype html> <html lang="en"> <head> < ...

  9. 如何同时启动多个Tomcat服务

    在项目开发中,有时会需要同时启动多个Tomcat服务,如果直接启动多个的话,会报以下错误: Port busy xxxx java.net.SocketException: Unrecognized ...

  10. 标准C++ 字符串处理增强函数

    转自:http://dewei.iteye.com/blog/1566734 //标准C++ string 去除首尾空白字符 2012-8-12 By Dewei static inline void ...