Set-cookie标头不保存cookie

问题描述 投票:-1回答:1

我知道这个问题已经问过很多次了,但是在大多数情况下,答案是将“ withCredential”标头设置为true,但它对我不起作用,我仍在寻找对正在发生的情况的理解。

我正在使用Angular应用程序,在登录时,服务器在响应中返回WebSession令牌和set-cookie标头。我可以在Chrome检查器中看到set-cookie标头:

但是正如我在标题中所述,cookie不会保存...我读到在XHR请求中可以忽略set-cookie标头,但我还读了许多其他线程,其中说withCredentials选项应该可以解决问题。

因此,我仍然对这些行为感到困惑,我想了解这种通用性,而不是只复制/粘贴stackoverflow答案,直到可行为止。

在我的请求标头中,我指定了我想要WithCredentials:true,通过使用有角度的http拦截器,我还指定了另外两个标头: “访问控制允许来源”:'*', “访问控制允许凭据”:'true',

request = request.clone({
    withCredentials: true,
    setHeaders: {
      Authorization: `Bearer ${currentUser.webSessionToken}`,
      "Access-Control-Allow-Origin": '*',
      "Access-Control-Allow-Credentials": 'true',
    }
  });

[在服务器端,我最终添加了带有jersey的CORS过滤器以配置响应标头:

public void filter(ContainerRequestContext request,
                   ContainerResponseContext response) throws IOException {
    response.getHeaders().add("Access-Control-Allow-Origin", "*");
    response.getHeaders().add("Access-Control-Allow-Headers",
        "origin, content-type, accept, authorization, X-Requested-With, X-HTTP-Method-Override");
    response.getHeaders().add("Access-Control-Allow-Credentials", "true");
    response.getHeaders().add("Access-Control-Allow-Methods",
        "GET, POST, PUT, DELETE, OPTIONS, HEAD");
}

有什么我忘了吗?

编辑:

[我注意到的另一种奇怪的行为:当我注销(清理存储空间以摆脱WebSession令牌)并再次登录时,Cookie将在身份验证请求中发送,其值取自上一次尝试的get,但仍然没有显示在Chrome的Cookie标签中:

Authentication response with set cookie header

Authentication log in and logout

Cookie tab in chrome

angular xmlhttprequest cors jersey-2.0 setcookie
1个回答
0
投票

在服务器端进行设置时,您还需要指定cookie的路径(/随处可见)。如果不是,浏览器将认为该cookie仅适用于设置它的路径。

© www.soinside.com 2019 - 2024. All rights reserved.