@Test public void whenFilterCalledWithoutAuthorizationHeaderThenCallNextFilter() throws Exception { when(request.getHeader(AUTHORIZATION_HEADER)).thenReturn(null); jwtAuthenticationFilter.doFilterInternal(request, response, filterChain); verify(request, times(1)).getHeader(AUTHORIZATION_HEADER); verify(filterChain, times(1)).doFilter(request, response); }
@Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException { try { String authorizationHeader = request.getHeader(AUTHORIZATION_HEADER); if (authorizationHeader != null && authorizationHeader.startsWith(BEARER_PREFIX)) { authorizationHeader = authorizationHeader.substring(BEARER_PREFIX_LENGTH); } if (authorizationHeader != null) { final Claims claims = jwtUtils.validateTokenAndGetClaims(authorizationHeader); LOG.info(() -> "JWT is valid"); final String username = jwtUtils.getUsernameFromTokenClaims(claims); LOG.info(() -> "Username in JWT: " + username); if (SecurityContextHolder.getContext().getAuthentication() == null) { final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( username, null, jwtUtils.getRolesFromTokenClaims(claims)); authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authentication); LOG.info( () -> "Authenticated User: " + username + " has been set in Spring SecurityContext."); } } chain.doFilter(request, response); } catch (Exception e) { LOG.error( MessageFormat.format("JWT Authentication failure! Details: {0}", e.getMessage()), e); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); } }
JwtAuthenticationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException { try { String authorizationHeader = request.getHeader(AUTHORIZATION_HEADER); if (authorizationHeader != null && authorizationHeader.startsWith(BEARER_PREFIX)) { authorizationHeader = authorizationHeader.substring(BEARER_PREFIX_LENGTH); } if (authorizationHeader != null) { final Claims claims = jwtUtils.validateTokenAndGetClaims(authorizationHeader); LOG.info(() -> "JWT is valid"); final String username = jwtUtils.getUsernameFromTokenClaims(claims); LOG.info(() -> "Username in JWT: " + username); if (SecurityContextHolder.getContext().getAuthentication() == null) { final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( username, null, jwtUtils.getRolesFromTokenClaims(claims)); authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authentication); LOG.info( () -> "Authenticated User: " + username + " has been set in Spring SecurityContext."); } } chain.doFilter(request, response); } catch (Exception e) { LOG.error( MessageFormat.format("JWT Authentication failure! Details: {0}", e.getMessage()), e); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); } } }
JwtAuthenticationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException { try { String authorizationHeader = request.getHeader(AUTHORIZATION_HEADER); if (authorizationHeader != null && authorizationHeader.startsWith(BEARER_PREFIX)) { authorizationHeader = authorizationHeader.substring(BEARER_PREFIX_LENGTH); } if (authorizationHeader != null) { final Claims claims = jwtUtils.validateTokenAndGetClaims(authorizationHeader); LOG.info(() -> "JWT is valid"); final String username = jwtUtils.getUsernameFromTokenClaims(claims); LOG.info(() -> "Username in JWT: " + username); if (SecurityContextHolder.getContext().getAuthentication() == null) { final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( username, null, jwtUtils.getRolesFromTokenClaims(claims)); authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authentication); LOG.info( () -> "Authenticated User: " + username + " has been set in Spring SecurityContext."); } } chain.doFilter(request, response); } catch (Exception e) { LOG.error( MessageFormat.format("JWT Authentication failure! Details: {0}", e.getMessage()), e); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); } }  }
JwtAuthenticationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException { try { String authorizationHeader = request.getHeader(AUTHORIZATION_HEADER); if (authorizationHeader != null && authorizationHeader.startsWith(BEARER_PREFIX)) { authorizationHeader = authorizationHeader.substring(BEARER_PREFIX_LENGTH); } if (authorizationHeader != null) { final Claims claims = jwtUtils.validateTokenAndGetClaims(authorizationHeader); LOG.info(() -> "JWT is valid"); final String username = jwtUtils.getUsernameFromTokenClaims(claims); LOG.info(() -> "Username in JWT: " + username); if (SecurityContextHolder.getContext().getAuthentication() == null) { final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( username, null, jwtUtils.getRolesFromTokenClaims(claims)); authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authentication); LOG.info( () -> "Authenticated User: " + username + " has been set in Spring SecurityContext."); } } chain.doFilter(request, response); } catch (Exception e) { LOG.error( MessageFormat.format("JWT Authentication failure! Details: {0}", e.getMessage()), e); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); } }  @Autowired void setJwtUtils(JwtUtils jwtUtils); }
JwtAuthenticationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException { try { String authorizationHeader = request.getHeader(AUTHORIZATION_HEADER); if (authorizationHeader != null && authorizationHeader.startsWith(BEARER_PREFIX)) { authorizationHeader = authorizationHeader.substring(BEARER_PREFIX_LENGTH); } if (authorizationHeader != null) { final Claims claims = jwtUtils.validateTokenAndGetClaims(authorizationHeader); LOG.info(() -> "JWT is valid"); final String username = jwtUtils.getUsernameFromTokenClaims(claims); LOG.info(() -> "Username in JWT: " + username); if (SecurityContextHolder.getContext().getAuthentication() == null) { final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken( username, null, jwtUtils.getRolesFromTokenClaims(claims)); authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); SecurityContextHolder.getContext().setAuthentication(authentication); LOG.info( () -> "Authenticated User: " + username + " has been set in Spring SecurityContext."); } } chain.doFilter(request, response); } catch (Exception e) { LOG.error( MessageFormat.format("JWT Authentication failure! Details: {0}", e.getMessage()), e); response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); } }  @Autowired void setJwtUtils(JwtUtils jwtUtils);  }