无法访问响应 header(x-auth-token 由 spring session 发送)

Can't access response header (x-auth-token sent by spring session)

我正在使用

我执行了以下操作来启用 SpringSession

aplication.properties

### Spring Session
spring.session.store-type=jdbc

HttpSessionConfig.java

@Configuration
public class HttpSessionConfig
{
    @Bean
    public HttpSessionStrategy httpSessionStrategy() {
        return new HeaderHttpSessionStrategy();
    }
}

正在创建数据库表,一切正常。现在我想通过调用 /login 通过我的 API 登录。我现在不明白的是,如何访问响应中spring session发送的x-auth-token。在 chrome 开发工具中,我可以清楚地看到 x-auth-token 包含在响应 header.

但是当我尝试使用 angulars httpclient 访问 header 时,我什至看不到它。

this.http.post(this.apiBaseURL + "api/session/login", {
    username: username,
    password: password,
    platform: 'webapp',
    platformVersion: '0.1',
    apiLevel: 1
}, { observe: 'response' })
.subscribe(data => {
    console.log(data.headers.keys());
});

控制台输出:

这可以通过在 header 中允许 Access-Control-Expose-Headers 来解决。 x-auth-token是一个自定义的header,需要通过允许上面的标签来暴露给外界。您可以使用下面的代码来解决这个问题。

@Configuration
public class WebSecurityCorsFilter extends OncePerRequestFilter {

    @Override
    public void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        res.setHeader("Access-Control-Allow-Credentials", "x-auth-token");
    }
}