@Test public void doFilterInternal() throws IOException, ServletException { when(jwtTool.getTokenFromHttpServletRequest(request)).thenReturn("SupeSecretAccessToken"); when(authenticationManager.authenticate(any())) .thenReturn(new UsernamePasswordAuthenticationToken("Principal", null)); doNothing().when(chain).doFilter(request, response); authenticationFilter.doFilterInternal(request, response, chain); verify(authenticationManager, times(1)).authenticate(any()); verify(chain, times(1)).doFilter(any(), any()); }
@Override public void doFilterInternal(@SuppressWarnings("NullableProblems") HttpServletRequest request, @SuppressWarnings("NullableProblems") HttpServletResponse response, @SuppressWarnings("NullableProblems") FilterChain chain) throws IOException, ServletException { String token = jwtTool.getTokenFromHttpServletRequest(request); if (token != null) { try { Authentication authentication = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(token, null)); log.info("User successfully authenticate - {}", authentication.getPrincipal()); SecurityContextHolder.getContext().setAuthentication(authentication); } catch (ExpiredJwtException e) { log.info("Token has expired: " + token); } catch (Exception e) { log.info("Access denied with token: " + e.getMessage()); } } chain.doFilter(request, response); }
AccessTokenAuthenticationFilter extends OncePerRequestFilter { @Override public void doFilterInternal(@SuppressWarnings("NullableProblems") HttpServletRequest request, @SuppressWarnings("NullableProblems") HttpServletResponse response, @SuppressWarnings("NullableProblems") FilterChain chain) throws IOException, ServletException { String token = jwtTool.getTokenFromHttpServletRequest(request); if (token != null) { try { Authentication authentication = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(token, null)); log.info("User successfully authenticate - {}", authentication.getPrincipal()); SecurityContextHolder.getContext().setAuthentication(authentication); } catch (ExpiredJwtException e) { log.info("Token has expired: " + token); } catch (Exception e) { log.info("Access denied with token: " + e.getMessage()); } } chain.doFilter(request, response); } }
AccessTokenAuthenticationFilter extends OncePerRequestFilter { @Override public void doFilterInternal(@SuppressWarnings("NullableProblems") HttpServletRequest request, @SuppressWarnings("NullableProblems") HttpServletResponse response, @SuppressWarnings("NullableProblems") FilterChain chain) throws IOException, ServletException { String token = jwtTool.getTokenFromHttpServletRequest(request); if (token != null) { try { Authentication authentication = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(token, null)); log.info("User successfully authenticate - {}", authentication.getPrincipal()); SecurityContextHolder.getContext().setAuthentication(authentication); } catch (ExpiredJwtException e) { log.info("Token has expired: " + token); } catch (Exception e) { log.info("Access denied with token: " + e.getMessage()); } } chain.doFilter(request, response); } AccessTokenAuthenticationFilter(JwtTool jwtTool, AuthenticationManager authenticationManager); }
AccessTokenAuthenticationFilter extends OncePerRequestFilter { @Override public void doFilterInternal(@SuppressWarnings("NullableProblems") HttpServletRequest request, @SuppressWarnings("NullableProblems") HttpServletResponse response, @SuppressWarnings("NullableProblems") FilterChain chain) throws IOException, ServletException { String token = jwtTool.getTokenFromHttpServletRequest(request); if (token != null) { try { Authentication authentication = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(token, null)); log.info("User successfully authenticate - {}", authentication.getPrincipal()); SecurityContextHolder.getContext().setAuthentication(authentication); } catch (ExpiredJwtException e) { log.info("Token has expired: " + token); } catch (Exception e) { log.info("Access denied with token: " + e.getMessage()); } } chain.doFilter(request, response); } AccessTokenAuthenticationFilter(JwtTool jwtTool, AuthenticationManager authenticationManager); @Override void doFilterInternal(@SuppressWarnings("NullableProblems") HttpServletRequest request,
                                 @SuppressWarnings("NullableProblems") HttpServletResponse response,
                                 @SuppressWarnings("NullableProblems") FilterChain chain); }
AccessTokenAuthenticationFilter extends OncePerRequestFilter { @Override public void doFilterInternal(@SuppressWarnings("NullableProblems") HttpServletRequest request, @SuppressWarnings("NullableProblems") HttpServletResponse response, @SuppressWarnings("NullableProblems") FilterChain chain) throws IOException, ServletException { String token = jwtTool.getTokenFromHttpServletRequest(request); if (token != null) { try { Authentication authentication = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(token, null)); log.info("User successfully authenticate - {}", authentication.getPrincipal()); SecurityContextHolder.getContext().setAuthentication(authentication); } catch (ExpiredJwtException e) { log.info("Token has expired: " + token); } catch (Exception e) { log.info("Access denied with token: " + e.getMessage()); } } chain.doFilter(request, response); } AccessTokenAuthenticationFilter(JwtTool jwtTool, AuthenticationManager authenticationManager); @Override void doFilterInternal(@SuppressWarnings("NullableProblems") HttpServletRequest request,
                                 @SuppressWarnings("NullableProblems") HttpServletResponse response,
                                 @SuppressWarnings("NullableProblems") FilterChain chain);  }