spring security 拦截了post put delete 请求 ,解决方案

使用spring security 做的安全框架时,会自动拦截post,pu,delete等请求,这时因为spring security 开启了防止跨域访问攻击的配置,那么怎么解决呢?请往下看:

我使用了thymeleaf在网页的head中添加:

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

然后在js全局的方法中添加:

  $(function () {

            var token = $("meta[name='_csrf']").attr("content");
            var header = $("meta[name='_csrf_header']").attr("content");
            $(document).ajaxSend(function(e, xhr, options) {
                xhr.setRequestHeader(header, token);
            });

}

这样就可以正常使用post, put, delete请求啦~

猜你喜欢

转载自blog.csdn.net/july_young/article/details/82346232
今日推荐