简体   繁体   English

Angular 8 从 HttpResponse 获取“Set-Cookie” header

[英]Angular 8 get “Set-Cookie” header from HttpResponse

I have the following Java Spring REST API method:我有以下 Java Spring REST ZDB974238714CA8DE634A7CE1D083A1 方法:

@RequestMapping(value = "/login", method = RequestMethod.POST)
public ResponseEntity login(@RequestBody LoginRequest request) {
    request.getSession(true);
    LoginResponse res = this.authService.login(request);
    return new ResponseEntity<>(res, HttpStatus.OK);
}

When called using Postman or from a FireFox browser, I can clearly see the "Set-Cookie" header:当使用 Postman 或从 FireFox 浏览器调用时,我可以清楚地看到“Set-Cookie”header:

在此处输入图像描述

Yet, when I'm using console.log to print the response in Angular, I don't see this header:然而,当我使用console.log在 Angular 中打印响应时,我没有看到这个 header:

在此处输入图像描述

This is the REST call in Angular:这是 Angular 中的 REST 调用:

this.http.post<Login>(this.url, this.loginRequest, {
  headers: AuthService.getHeaders(),
  observe: 'response',
  withCredentials: true
}).subscribe(
  response => {
    console.log(response);
});

I did add withCredentials: true and observe: 'response' to the request call as suggested, and I do get the whole response, but without the cookie.我确实按照建议添加withCredentials: trueobserve: 'response'到请求调用,我确实得到了整个响应,但没有 cookie。 What I want to do is to get a cookie after a successful login, which will be delivered with any request afterwards for authentication in the server.我想要做的是在成功登录后获取一个 cookie,该 cookie 将与之后的任何请求一起传递,以便在服务器中进行身份验证。

Thanks for the help!谢谢您的帮助!

Seems that browsers do not share the cookies to the client if it's not https .如果不是 https ,浏览器似乎不会与客户端共享https I had to follow this guide in order to serve the application in https mode, and then I was able to see the cookie in the server side.为了在 https 模式下为应用程序提供服务,我必须遵循本指南,然后才能在服务器端看到 cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM