java - Angular 8 从 HttpResponse 获取“Set-Cookie”标头
问题描述
我有以下 Java Spring REST API 方法:
@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);
}
当使用 Postman 或从 FireFox 浏览器调用时,我可以清楚地看到“Set-Cookie”标题:
然而,当我使用console.log
Angular 打印响应时,我看不到这个标题:
这是 Angular 中的 REST 调用:
this.http.post<Login>(this.url, this.loginRequest, {
headers: AuthService.getHeaders(),
observe: 'response',
withCredentials: true
}).subscribe(
response => {
console.log(response);
});
我确实按照建议将withCredentials: true
和添加observe: 'response'
到请求调用中,并且确实得到了整个响应,但没有 cookie。我想要做的是在成功登录后获取一个 cookie,该 cookie 将与之后的任何请求一起传递,以便在服务器中进行身份验证。
谢谢您的帮助!
解决方案
如果不是,浏览器似乎不会将 cookie 共享给客户端https
。我必须遵循本指南才能在 https 模式下为应用程序提供服务,然后我才能在服务器端看到 cookie。
推荐阅读
- scala - 在 Ubuntu 上的 Intellij 中运行 Spark 时出现错误
- reactjs - 处理Graphql Mutation update,缓存读写查询,如果查询是动态的?
- java - 使用堆栈测试字符串是否为回文
- java - 如何将 ferrari 和 rollsroyce 类别添加到我的 ArrayList
- python - 将 pandas 交叉表合并到数据表并获得部分结果
- mysql - MySQL,根据两张表获取数据
- mobile - Flutter 移动 Web 材质图标
- java - 如果 pay=false,为什么我的代码无法读取?它只是跳过它并在 pay = false 时打印成本而不是最终成本
- visual-studio-code - 在 vscode 扩展 api 的工作区中创建的新文件的侦听器
- javascript - 用锚标记替换 DOM 文本