@Test void doFilter() throws IOException, ServletException { SecurityContext previous = SecurityContextHolder.getContext(); try { SecurityContext testContext = SecurityContextHolder.createEmptyContext(); SecurityContextHolder.setContext(testContext); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); FilterChain chain = mock(FilterChain.class); RestAuthenticationToken auth = new RestAuthenticationToken( "admin", "admin", Arrays.asList(new SimpleGrantedAuthority("admin")), "token"); request.setRequestURI("/api/v1/dataset"); request.addHeader(TokenExtractor.TOKEN_HEADER, "token"); when(authenticationProvider.authenticate(new RestAuthenticationToken("token"))) .thenReturn(auth); filter.doFilter(request, response, chain); verify(chain).doFilter(request, response); assertEquals(auth, getContext().getAuthentication()); } finally { SecurityContextHolder.setContext(previous); } }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; if (!httpRequest.getRequestURI().endsWith("login") && !httpRequest.getRequestURI().endsWith("logout")) { String token = TokenExtractor.getToken(httpRequest); if (StringUtils.isNotBlank(token)) { try { RestAuthenticationToken authToken = new RestAuthenticationToken(token); Authentication authentication = authenticationProvider.authenticate(authToken); if (authentication.isAuthenticated()) { SecurityContextHolder.getContext().setAuthentication(authentication); } } catch (AuthenticationException e) { } } } chain.doFilter(request, response); }
TokenAuthenticationFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; if (!httpRequest.getRequestURI().endsWith("login") && !httpRequest.getRequestURI().endsWith("logout")) { String token = TokenExtractor.getToken(httpRequest); if (StringUtils.isNotBlank(token)) { try { RestAuthenticationToken authToken = new RestAuthenticationToken(token); Authentication authentication = authenticationProvider.authenticate(authToken); if (authentication.isAuthenticated()) { SecurityContextHolder.getContext().setAuthentication(authentication); } } catch (AuthenticationException e) { } } } chain.doFilter(request, response); } }
TokenAuthenticationFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; if (!httpRequest.getRequestURI().endsWith("login") && !httpRequest.getRequestURI().endsWith("logout")) { String token = TokenExtractor.getToken(httpRequest); if (StringUtils.isNotBlank(token)) { try { RestAuthenticationToken authToken = new RestAuthenticationToken(token); Authentication authentication = authenticationProvider.authenticate(authToken); if (authentication.isAuthenticated()) { SecurityContextHolder.getContext().setAuthentication(authentication); } } catch (AuthenticationException e) { } } } chain.doFilter(request, response); } TokenAuthenticationFilter(AuthenticationProvider authenticationProvider); }
TokenAuthenticationFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; if (!httpRequest.getRequestURI().endsWith("login") && !httpRequest.getRequestURI().endsWith("logout")) { String token = TokenExtractor.getToken(httpRequest); if (StringUtils.isNotBlank(token)) { try { RestAuthenticationToken authToken = new RestAuthenticationToken(token); Authentication authentication = authenticationProvider.authenticate(authToken); if (authentication.isAuthenticated()) { SecurityContextHolder.getContext().setAuthentication(authentication); } } catch (AuthenticationException e) { } } } chain.doFilter(request, response); } TokenAuthenticationFilter(AuthenticationProvider authenticationProvider); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); }
TokenAuthenticationFilter extends GenericFilterBean { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; if (!httpRequest.getRequestURI().endsWith("login") && !httpRequest.getRequestURI().endsWith("logout")) { String token = TokenExtractor.getToken(httpRequest); if (StringUtils.isNotBlank(token)) { try { RestAuthenticationToken authToken = new RestAuthenticationToken(token); Authentication authentication = authenticationProvider.authenticate(authToken); if (authentication.isAuthenticated()) { SecurityContextHolder.getContext().setAuthentication(authentication); } } catch (AuthenticationException e) { } } } chain.doFilter(request, response); } TokenAuthenticationFilter(AuthenticationProvider authenticationProvider); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain);  }