Spring Security Ajax is blocked

Source: Internet
Author: User

Background is the security control of the project using spring security

When using Ajax will be reported 403 (Ajax get mode is no problem when the post will be reported)

Spring Security was originally designed to prevent CSRF attacks now that Ajax has been accidentally injured ...

Then paste the solution below, the page's head tag is appended

(This is to say that the use of the Thymeleaf template all will have th: If it is a JSP, use the EL expression bar th: can be used to remove it)

<name= "_CSRF"  th:content= "${_csrf.token}"/> <  name= "_csrf_header"  th:content= "${_csrf.headername}" />

Then use JS to take the value

var header = $ ("meta[name= ' _csrf_header ')"). attr ("content"); var token =$ ("Meta[name= ' _csrf ')"). attr ("content");

Ajax calls are used, the other is as usual as beforesend write down as follows

$.ajax ({URL:"", type:"POST", Data: "", ContentType:' Application/json;charset=utf-8 ',            //Async:false,            beforesend: function  (XHR) {Xhr.setrequestheader (header, token); }, success:function(resdata) {}, Error:function(XHR, ajaxoptions, ThrowError) { }        });

Spring Security Ajax is blocked

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.