diff --git a/commons-auth-core/src/main/java/io/rocketbase/commons/filter/JwtAuthenticationTokenFilter.java b/commons-auth-core/src/main/java/io/rocketbase/commons/filter/JwtAuthenticationTokenFilter.java index 9b53ece8..a05e510f 100644 --- a/commons-auth-core/src/main/java/io/rocketbase/commons/filter/JwtAuthenticationTokenFilter.java +++ b/commons-auth-core/src/main/java/io/rocketbase/commons/filter/JwtAuthenticationTokenFilter.java @@ -36,11 +36,18 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { - final String requestHeader = request.getHeader(jwtProperties.getHeader()); + String authToken = getAuthToken(request); + String username = getValidatedUsername(authToken); + + setAuthenticationIfValid(authToken, username, request); + + chain.doFilter(request, response); + } - String username = null; + protected String getAuthToken(HttpServletRequest request) { String authToken = null; + final String requestHeader = request.getHeader(jwtProperties.getHeader()); if (requestHeader != null && requestHeader.startsWith(jwtProperties.getTokenPrefix())) { // check header authToken = requestHeader.substring(jwtProperties.getTokenPrefix().length()); @@ -48,10 +55,13 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse // check uiParam authToken = request.getParameter(jwtProperties.getUriParam()); } + return authToken; + } + protected String getValidatedUsername(String authToken) { if (authToken != null) { try { - username = jwtTokenService.getUsernameFromToken(authToken); + return jwtTokenService.getUsernameFromToken(authToken); } catch (IllegalArgumentException e) { log.error("an error occured during getting username from token. {}", e.getMessage()); } catch (ExpiredJwtException e) { @@ -62,10 +72,12 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse log.error("other token exception: {}", e.getMessage()); } } + return null; + } + protected void setAuthenticationIfValid(String authToken, String username, HttpServletRequest request) { if (username != null && SecurityContextHolder.getContext() .getAuthentication() == null) { - AppUser user = (AppUser) appUserService.loadUserByUsername(username); if (jwtTokenService.validateToken(authToken, user)) { @@ -80,7 +92,5 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse .setAuthentication(authentication); } } - - chain.doFilter(request, response); } } \ No newline at end of file