我已经为CSRF设置了Spring Boot 1.5.x.在spring上测试一切正常:(1)服务器生成令牌并发送到客户端(2)如果客户端发送令牌,一切正常。
使用Web客户端,我将Spring Boot作为rest API(使用http://localhost:8088/)并将angularjs作为客户端(使用http://localhost:9000) 但是接缝问题出在 angularjs上,它会在标题键XSRF-TOKEN而不是X-XSRF-TOKEN (spring正在等待的那个)上发送令牌。
通过阅读angularjs文档,此标题密钥应自动发送(https://docs.angularjs.org/api/ng/service/ $ http#cross-site-request-forgery-xsrf-protection)
其中,angularjs与弹簧靴相互作用的打印屏幕
(1)接收spring boot csrf令牌的GET xhr请求
在春天,我使用CORS过滤器,只使用弹簧安全功能生成CSRF令牌
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
在angularjs中,我已经尝试通过拦截器定义标头密钥(只是尝试解决方案)
$httpProvider.defaults.xsrfHeaderName = 'X-XSRF-TOKEN';
var interceptor = ['$rootScope', '$q', 'httpBuffer', '$cookieStore', function ($rootScope, $q, httpBuffer, $cookieStore) {
return {
'request': function (config, a, b) {
return config;
},
'requestError': function (rejection) {
return $q.reject(rejection);
},
'response': function (response) {
$httpProvider.defaults.headers.common['X-XSRF-TOKEN'] = response.headers('XSRF-TOKEN');
return response;
},
'responseError': function (rejection) {
console.log('rejection', rejection.headers());
var deferred = $q.defer();
return $q.reject(rejection);
}
};
}];
$httpProvider.interceptors.push(interceptor);
答案 0 :(得分:0)
需要在响应时存储令牌并根据请求发送。如果您需要发送所有请求,请覆盖拦截器上的request
方法:
request: function (config) {
config.headers['x-xsrf-token'] = stored_token;
return config;
}
否则,请在单个$ http请求中发送。