我正在尝试实现基于 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; }}
添加回答
举报
0/150
提交
取消