jquery - 使用带有 ajax Rest 调用的 Spring CSRF 和带有 Thymeleaf 的 HTML 页面

标签 jquery html spring csrf thymeleaf

我将 Spring Security 与 CSRF 结合使用,我的 javascript 中的一些 POST 调用有问题。对于我的页面,我使用 Thymeleaf 和 HTML 5,对于我的 Controller 的 Rest 调用,我使用 jQuery.ajax。 如果我像这样对我的表单使用 ajax 调用:

$(function() {
    $("#saveCarButton").click(
            function() {
                var form = $("#addCarForm");        
                $.ajax({
                    type : "POST",
                    url : form.attr("action"),
                    data : form.serialize(),
                    // all right with rest call
                    success : function(data) {...}  
                    //error during rest call
                    error : function(data) {
                        window.location.href = "/500";
                    }
                });
            });
});

一切正常,但是当我调用这个函数时:

jQuery.ajax({
        url : 'upload',
        type: 'POST',
        contentType: false,
        processData: false,
        data:formData,
        beforeSend:function(xhr) {
            waitingModal.showPleaseWait();
        },  
        success: function(data,status,xhr){...}
        error: function(xhr,status,e){
        }
    }).complete(function() {
        //add timeout because otherwise user could see a too fast waiting modal
        setTimeout(function(){
            waitingModal.hidePleaseWait();
        }, 1000);
    });

我收到错误 403。 也许在表单中,使用 thymeleaf,它工作正常,但对于第二种类型的请求,我必须添加 CSRF token 。 我试过

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

在我的 html 页面中添加了

!--  -->
<meta name="_csrf" th:content="${_csrf.token}"/>
<!-- default header name is X-CSRF-TOKEN -->
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>

为什么它与表单一起工作? 使用表格时我不必添加任何内容吗? 使用浏览器的开发工具可以看到 _csrf 和 _csrf_header 是否有危险? 谢谢

最佳答案

这个解决方案对我有用 Trying to use React/Ajax calls with Spring MVC and Thymeleaf

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

Ajax :

var token = $('#_csrf').attr('content');
var header = $('#_csrf_header').attr('content');

$.ajax({
                        type: "POST",
                        url: url,
                        beforeSend: function(xhr) {
                            xhr.setRequestHeader(header, token);
                        },

                        success: function(data, textStatus, jqXHR) {
                            alert(status);
                        },
                        error: function(request, status, error) {
                            alert(status);
                        }
                    });

关于jquery - 使用带有 ajax Rest 调用的 Spring CSRF 和带有 Thymeleaf 的 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34747437/

相关文章:

java - 如何重用 Spring XML 片段

java - 当我们使用 Rest 模板在内部点击 API 时,如何在 Spring Boot 中获取 API 的 HTTP 状态

javascript - 如何区分 DOM 函数中使用的相同 div?

javascript - 不光滑的方法不起作用

javascript - Jquery 动态地将 Bootstrap 列包装在行中

html - 具有不同 CSS 的 Angular2 组件实例

javascript - xampp 服务器不适用于以下代码,但该代码适用于 appserv

jquery - 基于没有类的无序列表的 CSS 菜单

html - Bootstrap 4 截断移动设备表格内的长文本

java - 添加拦截器时出现异常