我无法访问http响应的Cookie。根据邮递员的说法,它如下所示发送:
根据Google Chrome的网络开发人员标签,这就是请求的样子: Google Chrome's Network Tab
以下是我用来发送get请求的代码:
createAuthorizationHeader(headers: Headers, user:String, pass:String) {
headers.append('Authorization', 'Basic ' +
btoa(user+":"+pass));
headers.append('Content-Type', 'application/json');
}
login() {
var user = this.username.nativeElement.value;
var pass = this.password.nativeElement.value;
let header = new Headers();
this.createAuthorizationHeader(header, user, pass);
let options = new RequestOptions({ headers: header, withCredentials: true });
this.http.get('http://localhost:8080/sample-webapp/rest/auth/login', options)
.subscribe(res => {
console.log(res);
console.log(res.headers.get('Set-Cookie'));
});
}
查看了一段时间后,withCredentials:true应该修复我的问题并在响应头中包含cookie。但我得到的只是:
它似乎不是后端问题导致浏览器和邮递员收到cookie。请告诉我如何才能获得美味的饼干?
后端还有以下标题:
response.getHeaders().add("Access-Control-Allow-Origin", request.getHeaderString("origin"));
response.getHeaders().add("Access-Control-Allow-Headers", request.getHeaderString("access-control-request-headers"));
response.getHeaders().add("Access-Control-Allow-Credentials", "true");
response.getHeaders().add("Access-Control-Expose-Headers", "Set-Cookie");
response.getHeaders().add("Access-Control-Allow-Methods", request.getHeaderString("access-control-request-method"));