为了账号安全,请及时绑定邮箱和手机立即绑定

JWT令牌在java中解析时总是抛出ExpiredJwtException

JWT令牌在java中解析时总是抛出ExpiredJwtException

慕的地10843 2021-10-27 10:49:28
我正在尝试实现基于 jwt 的身份验证以使用 Spring Boot 公开我的 REST api,但我面临 JWT 到期日期的问题。即使我设置了到期时间,它也总是抛出“ExpiredJwtException”。我已经提供了代码,如果有人找到确切的问题和解决方案,请告诉我。谢谢。git 仓库https://github.com/asim-ch/JWT-authentication-with-spring-boot身份验证过滤器public class JWTAuthenticationFilter extends OncePerRequestFilter {   @Autowired   TokenProvider tokenProvider;   @Autowired   CustomUserDetailsService userDetailsService;    @Override    protected void doFilterInternal(HttpServletRequest httpServletRequest,     HttpServletResponse httpServletResponse, FilterChain filterChain) throws     ServletException, IOException {        try {            String jwt = getJwt(httpServletRequest);            if (jwt!=null && tokenProvider.validateJwtToken(jwt)) {                String username = tokenProvider.getUserNameFromJwtToken(jwt);                UserDetails userDetails = userDetailsService.loadUserByUsername(username);                UsernamePasswordAuthenticationToken authentication                        = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());                authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));                SecurityContextHolder.getContext().setAuthentication(authentication);            }        } catch (Exception e) {            logger.error("Can NOT set user authentication ", e);        }        filterChain.doFilter(httpServletRequest, httpServletResponse);    }    private String getJwt(HttpServletRequest request) {        String authHeader = request.getHeader("Authorization");        if (authHeader != null && authHeader.startsWith("Bearer ")) {            return authHeader.replace("Bearer ","");        }        return null;    }}
查看完整描述

2 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

经我测试,您的代码似乎运行良好。

所以尝试几件事

  1. 通过命令行进行全新安装 mvn clean install

  2. 删除 m2 存储库,然后再次导入依赖项

  3. 尝试删除缓存并重新启动 IDE 和系统


查看完整回答
反对 回复 2021-10-27
?
慕勒3428872

TA贡献1848条经验 获得超6个赞

我遇到过同样的问题。试过@kj007 答案。然而它对我不起作用。

就我而言,这是一个失眠错误。重新启动后,一切正常。我认为它以某种方式发送了一个旧令牌。


查看完整回答
反对 回复 2021-10-27
  • 2 回答
  • 0 关注
  • 677 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信