繁体   English   中英

用于 AngularJS 和 Spring 启动应用程序的 CORS 域 cookie

[英]CORS domain cookie for AngularJS and Spring boot application

目前我正在使用 AngularJS 1.6 ( http://localhost:8000 ) 和 Spring Boot ( http://localhost:8080 ) 构建一个应用程序。 当用户登录时,spring boot api会在响应中添加一个cookie,我可以在浏览器中看到这个cookie。

然后在另一个获取用户的请求中,我的后端代码使用

httpServletRequest.getCookies()

获取cookie,但它总是返回null。

当我使用jsp和spring boot时,效果很好。 所以我想知道这是否是跨域问题。

在后端,我已经添加了配置:

response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

谁能给我一些建议?

谢谢,彼得

您是否尝试过 AngularJS 中的 withCredentials 选项?

如果添加这个配置,angular $http 服务会在发送请求时发送cookie。

例如:

$http.post(url, {withCredentials: true, ...})

或为所有请求添加此配置:

 angular.module('myApp')
 .config(['$httpProvider', function($httpProvider) {
  $httpProvider.defaults.withCredentials = true;
}])
  1. https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
  2. 带凭据的 AngularJS

暂无
暂无

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

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