最近在项目过程中加超时的设置,登录后,每次后台都会在response的头部返回给一个token,并且利用这个token作为新的请求头部的token传给后台,以此来保证在操作系统的过程中,不会因为超时而被强制退出系统的不合理要求
一直在操作系统,超时时间的开始时间以最后一次操作系统的时间开始计时
我们可以在自主封装的接口里面写这个方法,但是我们需要在后台返回的response的headers里面拿到最新的token,但是一般情况下我们只能在response的headers里面看到type,看不到token,这就让我们前台不会拿到这个token
后来经过一番查找终于找到
浏览器默认只能访问以下几种响应头:
- Cache-Control
- Content-Length
- Content-Type
- Expires
- Pragma
若想让浏览器能访问到其它响应头(例如后台新加的token)则需在服务器上设置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啦