我正在开发一个基于客户端 AngularJS 和服务器端 API(Tomcat + Jersey for WS)的 Java 应用程序。
我的API的某些路径受到限制,如果用户没有会话,则返回的响应状态为401。在客户端,401 http状态被拦截以将用户重定向到登录页面。
用户通过身份验证后,我在服务器端创建一个会话
httpRequest.getSession(true);
and the response send to the client does have the Set-cookie instruction in its header :
Set-Cookie:JSESSIONID=XXXXXXXXXXXXXXXXXXXXX; Domain=localhost; Path=/api/; HttpOnly
问题是 cookie 永远不会放在客户端。当我检查 localhost 域的 cookie 时,它是空的,因此下一个请求的标头中没有此 cookie,并且客户端仍然无法访问我的 API 的受限路径。
客户端和服务器位于同一域,但它们没有相同的路径和相同的端口号:
客户 :http://localhost:8000/app/index.html
服务器 :http://localhost:8080/api/restricted/
附加信息:双方均启用 CORS:
"Access-Control-Allow-Methods", "GET, POST, OPTIONS"
"Access-Control-Allow-Origin", "*"
"Access-Control-Allow-Credentials", true
有什么办法可以让 Set-cookie 正常工作吗?
这是 AngularJS 相关的问题吗?