axios如何获取后台spring-security+jwt生成的token?

clipboard.png

我现在后台已经正确返回了jwt token但是就是不知道如何在axios获取这个保存在cookie中

阅读 5.5k
2 个回答

解决了:
@Override

protected void successfulAuthentication(HttpServletRequest req,
                                        HttpServletResponse res,
                                        FilterChain chain,
                                        Authentication auth) throws IOException, ServletException {
    String token = Jwts.builder()
            .setSubject(((org.springframework.security.core.userdetails.User) auth.getPrincipal()).getUsername())
            .setExpiration(new Date(System.currentTimeMillis() + 60 * 60 * 24 * 1000))
            .signWith(SignatureAlgorithm.HS512, SECRET)
            .compact();
    res.addHeader("Authorization", TOKEN_PREFIX + token);
    res.setHeader("Access-Control-Expose-Headers","Authorization");//在这里添加 就OK了
}

参考:
[Axios get access to response header fields Ask Question](https://stackoverflow.com/questions/37897523/axios-get-access-to-response-header-fields)
response.headers.Authorization
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题