问题描述
在做登录功能的时候 调用登录接口 想获取Header里的Token 然而发现里面并没有Token:
在用Postman测试的时候发现是正常的:
原因
浏览器默认只能访问以下几种响应头:
- Cache-Control
- Content-Length
- Content-Type
- Expires
- Pragma
若想让浏览器能访问到其它响应头 则需在服务器上设置Access-Control-Expose-Headers
其值为要被访问的响应头的属性名
解决方法
在拦截器里进行配置 添加响应头
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
...
@Override
protected void successfulAuthentication(HttpServletRequest request,HttpServletResponse response,FilterChain chain,Authentication authResult) throws IOException {
...
// 让浏览器能访问到其它响应头
response.addHeader("Access-Control-Expose-Headers","token");
...
}
...
}
成功拿到token: