我已经通读了很多解决方案,但没有一个对我有效。
这个github问题本质上就是我的问题(https://github.com/angular/angular/issues/20511)
我使用的是Angular 5.2.5,Chrome版本65.0.3325.146,Spring boot 1.5.4。
在我的登录表单上,我首先发送GET请求以获取XSRF cookie。然后我发送一个带有登录信息的帖子
Send GET request that returns Set-Cookie header
配置了CSRF的Spring boot应用
http
.csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());我使用的是角度的webpack代理,所以网址是相对的,并使用withCredentials选项发送请求。
我还尝试按照Github问题中的建议编写自己的拦截器,但在尝试获取标记cookie时却得不到null。我在Postman中检查到let token = this.tokenExtractor.getToken() as string;的httpOnly标志设置为false
发布于 2018-04-05 09:48:52
我已经有一段时间有同样的问题了。我只是用一点小技巧修复了它
// function to get cookie by name
getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
getItemsFromServer(){
let httpOptions={
headers: new HttpHeaders({
'Content-Type': 'application/json',
'X-XSRF-TOKEN': this.getCookie("XSRF-TOKEN")
}),
withCredentials: true
};
this.httpClient.get(url, httpOptions)
.subscribe(response=>{
// more code here
}
);
}https://stackoverflow.com/questions/49517447
复制相似问题