@Test public void doFilterInternalAuthenticationNotNull() throws ServletException, IOException { String token = "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiJ9.GE2q1gX6T-mcjf0xmIlGru1gzu-PQF1leFK4U3Kphj8ZLpQG3Rn8qyLLO38ilyvP2u03Ft7bEBAJqRS-86WXCg"; when(request.getHeader(AUTHORIZATION)).thenReturn(token); SecurityContext context = mock(SecurityContext.class); when(securityAppContext.getContext()).thenReturn(context); Authentication authentication = mock(Authentication.class); when(context.getAuthentication()).thenReturn(authentication); jwtAuthenticationTokenFilter.doFilterInternal(request, response, chain); verifyZeroInteractions(userService, usernamePasswordAuthenticationTokenFactory ); verify(chain).doFilter(request, response); verify(context, never()).setAuthentication(any(Authentication.class)); }
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { String authToken = request.getHeader(AUTHORIZATION); if(authToken != null) { try { authToken = new String(authToken.substring(BEGIN_INDEX).getBytes(), UTF_8); SecurityContext context = securityAppContext.getContext(); if(context.getAuthentication() == null) { logger.info("Checking authentication for token " + authToken); User u = userService.validateUser(authToken, request.getRemoteAddr()); if(u != null) { logger.info("User " + u.getUsername() + " found."); Authentication authentication = usernamePasswordAuthenticationTokenFactory.create(u); context.setAuthentication(authentication); } } } catch (StringIndexOutOfBoundsException e) { logger.error(e.getMessage()); } } chain.doFilter(request, response); }
JwtAuthenticationTokenFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { String authToken = request.getHeader(AUTHORIZATION); if(authToken != null) { try { authToken = new String(authToken.substring(BEGIN_INDEX).getBytes(), UTF_8); SecurityContext context = securityAppContext.getContext(); if(context.getAuthentication() == null) { logger.info("Checking authentication for token " + authToken); User u = userService.validateUser(authToken, request.getRemoteAddr()); if(u != null) { logger.info("User " + u.getUsername() + " found."); Authentication authentication = usernamePasswordAuthenticationTokenFactory.create(u); context.setAuthentication(authentication); } } } catch (StringIndexOutOfBoundsException e) { logger.error(e.getMessage()); } } chain.doFilter(request, response); } }
JwtAuthenticationTokenFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { String authToken = request.getHeader(AUTHORIZATION); if(authToken != null) { try { authToken = new String(authToken.substring(BEGIN_INDEX).getBytes(), UTF_8); SecurityContext context = securityAppContext.getContext(); if(context.getAuthentication() == null) { logger.info("Checking authentication for token " + authToken); User u = userService.validateUser(authToken, request.getRemoteAddr()); if(u != null) { logger.info("User " + u.getUsername() + " found."); Authentication authentication = usernamePasswordAuthenticationTokenFactory.create(u); context.setAuthentication(authentication); } } } catch (StringIndexOutOfBoundsException e) { logger.error(e.getMessage()); } } chain.doFilter(request, response); }  }
JwtAuthenticationTokenFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { String authToken = request.getHeader(AUTHORIZATION); if(authToken != null) { try { authToken = new String(authToken.substring(BEGIN_INDEX).getBytes(), UTF_8); SecurityContext context = securityAppContext.getContext(); if(context.getAuthentication() == null) { logger.info("Checking authentication for token " + authToken); User u = userService.validateUser(authToken, request.getRemoteAddr()); if(u != null) { logger.info("User " + u.getUsername() + " found."); Authentication authentication = usernamePasswordAuthenticationTokenFactory.create(u); context.setAuthentication(authentication); } } } catch (StringIndexOutOfBoundsException e) { logger.error(e.getMessage()); } } chain.doFilter(request, response); }   }
JwtAuthenticationTokenFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { String authToken = request.getHeader(AUTHORIZATION); if(authToken != null) { try { authToken = new String(authToken.substring(BEGIN_INDEX).getBytes(), UTF_8); SecurityContext context = securityAppContext.getContext(); if(context.getAuthentication() == null) { logger.info("Checking authentication for token " + authToken); User u = userService.validateUser(authToken, request.getRemoteAddr()); if(u != null) { logger.info("User " + u.getUsername() + " found."); Authentication authentication = usernamePasswordAuthenticationTokenFactory.create(u); context.setAuthentication(authentication); } } } catch (StringIndexOutOfBoundsException e) { logger.error(e.getMessage()); } } chain.doFilter(request, response); }    }