首页 > 解决方案 > Angular 中无法访问 Set-Cookie 和自定义标头?

问题描述

我正在开发一个 Web 应用程序,前端使用 Angular 8,后端使用 Java 的 Spring Boot。我的后端使用一个过滤器,它对每个正确的传入 HTTP 调用执行此操作:

public void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain)
            throws IOException, ServletException {

     // Code of the filter....
     res.addHeader("Set-Cookie", "myCookie=myCustomCookie");
     res.addHeader("Custom-header", "otherData");

}

这是我的控制器:

    @GetMapping("/")
    @ResponseStatus(value = HttpStatus.OK)
    public String myMethod() {
    
    return "JsonString";

}

现在,我知道如果我设置Set-Cookie标题,我的调用响应应该在我的浏览器中自动设置 cookie 。如果我使用 Postman 或浏览器,这种情况总是会发生。但是,如果我从 Angular 应用程序进行调用,则不会发生这种情况,并且我在浏览器中找不到myCookiecookie!

这是角代码:

async doTheCall() {

const myOptions = {

  type: ENDPOINT_TYPE.SERVICES,
  options: {
    observe: 'response',
    responseType: 'json',
    withCredentials:true
  },
  pathParams: paramsList
};

return this.get<Configuration>(ENDPOINT_URL.SEARCH_CONFIGURATION, myOptions);

}

Configuration我的应用程序的对象在哪里。在互联网上我读到通过设置问题解决withCredentials: trueset-cookie,但事实并非如此!最后,我无法访问响应中的任何标题(但可以访问正文!)而且我不知道为什么!这样我什至不能使用代码设置cookie,使用Custom-header标题!这是代码:

let res = await this.doTheCall();
    res.subscribe(
      response => {

        console.log("Headers: ", response.headers);

})

在此处输入图像描述

我不知道我错在哪里,因为Postman 向我显示了我回复的所有标题,而且还有很多!

标签: angularspringhttpcookieshttp-headers

解决方案


推荐阅读