我已经为CSRF设置了Spring Boot 1.5.x.对spring的测试一切正常:(1)服务器生成令牌并发送到客户端(2)如果客户端发送令牌一切正常 .
使用Web客户端,我使用Spring Boot作为休息API(使用http://localhost:8088/),将angularjs作为客户端(使用http://localhost:9000)但是接缝问题是 angularjs, it sends the token on the header key XSRF-TOKEN instead of the X-XSRF-TOKEN ( Spring 天正在等待的那个) .
通过阅读angularjs文档,这个 Headers 密钥应该自动发送(https://docs.angularjs.org/api/ng/service/ $ http#cross-site-request-forgery-xsrf-protection)
哪里是angularjs与 spring 靴相互作用的打印屏幕
(1) GET xhr request that receives the spring boot csrf token
(2) POST xhr request that needs to send the token with name
在 Spring 天,我使用CORS过滤器,只使用 spring 安全功能来生成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);
1 回答
需要将令牌存储在响应中并根据请求发送 . 如果你需要发送所有请求覆盖拦截器上的
request
方法:否则,请在单个$ http请求中发送 .