Spring security、thymeleaf 和 cookie
•浏览 1
Spring security, thymeleaf and cookie
实际上,我使用的是 spring boot、thymeleaf 和 spring security。
应用程序中的安全性分为两部分。
一个用于mvc,另一个用于rest call。
如果登录可以,我会搜索一种方法来创建一个带有登录名/密码的 cookie,以便能够为每个 ajax 调用添加标头。
headers: {
"Authorization":"Basic" + $.cookie('cookie-authorization')
},$(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);
});
})
所以如果登录成功,我会搜索创建一个cookie
编辑可以在服务器端做吗?
您可以使用 official doc 中的此代码将标记包含在所有 Ajax 调用中。
headers: {
"Authorization":"Basic" + $.cookie('cookie-authorization')
},$(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);
});
})
希望这会有所帮助。