set cookie和自定义标头在angular中不可访问?

yv5phkfx  于 2021-07-23  发布在  Java
关注(0)|答案(0)|浏览(192)

我正在开发一个web应用程序,前端使用angular8,后端使用java的spring引导。我的后端使用一个过滤器,它为每个正确的传入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应用程序调用,就不会发生这种情况,而且我找不到 myCookie 我的浏览器中有cookie!
这是Angular 代码:

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: true 这个 set-cookie 问题解决了,但还没有解决!最后,我无法访问我的响应中的任何标题(但是可以访问主体!)我不知道为什么!这样我甚至不能用代码设置cookies,使用 Custom-header 头!代码如下:

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

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

})


我不知道我错在哪里,因为在《 Postman 》杂志上,我看到了所有回复的标题,而且还有很多!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题