@Test public void testNoHeader() throws Exception { filter.doFilter(request, response, chain); verify(authenticationManager, never()).authenticate(any(Authentication.class)); verify(chain).doFilter(request, response); assertEquals(null, SecurityContextHolder.getContext().getAuthentication()); }
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; if (skipIfAlreadyAuthenticated) { Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication(); if (existingAuth != null && existingAuth.isAuthenticated() && (existingAuth instanceof AnonymousAuthenticationToken) == false) { chain.doFilter(request, response); return; } } String header = request.getHeader("Authorization"); if ((header != null) && header.startsWith("Negotiate ")) { if (logger.isDebugEnabled()) { logger.debug("Received Negotiate Header for request " + request.getRequestURL() + ": " + header); } byte[] base64Token = header.substring(10).getBytes("UTF-8"); byte[] kerberosTicket = Base64.decode(base64Token); KerberosServiceRequestToken authenticationRequest = new KerberosServiceRequestToken(kerberosTicket); Authentication authentication; try { authentication = authenticationManager.authenticate(authenticationRequest); } catch (AuthenticationException e) { logger.warn("Negotiate Header was invalid: " + header, e); SecurityContextHolder.clearContext(); if (failureHandler != null) { failureHandler.onAuthenticationFailure(request, response, e); } else { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.flushBuffer(); } return; } if (successHandler != null) { successHandler.onAuthenticationSuccess(request, response, authentication); } SecurityContextHolder.getContext().setAuthentication(authentication); } chain.doFilter(request, response); }
SpnegoAuthenticationProcessingFilter extends GenericFilterBean { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; if (skipIfAlreadyAuthenticated) { Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication(); if (existingAuth != null && existingAuth.isAuthenticated() && (existingAuth instanceof AnonymousAuthenticationToken) == false) { chain.doFilter(request, response); return; } } String header = request.getHeader("Authorization"); if ((header != null) && header.startsWith("Negotiate ")) { if (logger.isDebugEnabled()) { logger.debug("Received Negotiate Header for request " + request.getRequestURL() + ": " + header); } byte[] base64Token = header.substring(10).getBytes("UTF-8"); byte[] kerberosTicket = Base64.decode(base64Token); KerberosServiceRequestToken authenticationRequest = new KerberosServiceRequestToken(kerberosTicket); Authentication authentication; try { authentication = authenticationManager.authenticate(authenticationRequest); } catch (AuthenticationException e) { logger.warn("Negotiate Header was invalid: " + header, e); SecurityContextHolder.clearContext(); if (failureHandler != null) { failureHandler.onAuthenticationFailure(request, response, e); } else { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.flushBuffer(); } return; } if (successHandler != null) { successHandler.onAuthenticationSuccess(request, response, authentication); } SecurityContextHolder.getContext().setAuthentication(authentication); } chain.doFilter(request, response); } }
SpnegoAuthenticationProcessingFilter extends GenericFilterBean { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; if (skipIfAlreadyAuthenticated) { Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication(); if (existingAuth != null && existingAuth.isAuthenticated() && (existingAuth instanceof AnonymousAuthenticationToken) == false) { chain.doFilter(request, response); return; } } String header = request.getHeader("Authorization"); if ((header != null) && header.startsWith("Negotiate ")) { if (logger.isDebugEnabled()) { logger.debug("Received Negotiate Header for request " + request.getRequestURL() + ": " + header); } byte[] base64Token = header.substring(10).getBytes("UTF-8"); byte[] kerberosTicket = Base64.decode(base64Token); KerberosServiceRequestToken authenticationRequest = new KerberosServiceRequestToken(kerberosTicket); Authentication authentication; try { authentication = authenticationManager.authenticate(authenticationRequest); } catch (AuthenticationException e) { logger.warn("Negotiate Header was invalid: " + header, e); SecurityContextHolder.clearContext(); if (failureHandler != null) { failureHandler.onAuthenticationFailure(request, response, e); } else { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.flushBuffer(); } return; } if (successHandler != null) { successHandler.onAuthenticationSuccess(request, response, authentication); } SecurityContextHolder.getContext().setAuthentication(authentication); } chain.doFilter(request, response); }  }
SpnegoAuthenticationProcessingFilter extends GenericFilterBean { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; if (skipIfAlreadyAuthenticated) { Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication(); if (existingAuth != null && existingAuth.isAuthenticated() && (existingAuth instanceof AnonymousAuthenticationToken) == false) { chain.doFilter(request, response); return; } } String header = request.getHeader("Authorization"); if ((header != null) && header.startsWith("Negotiate ")) { if (logger.isDebugEnabled()) { logger.debug("Received Negotiate Header for request " + request.getRequestURL() + ": " + header); } byte[] base64Token = header.substring(10).getBytes("UTF-8"); byte[] kerberosTicket = Base64.decode(base64Token); KerberosServiceRequestToken authenticationRequest = new KerberosServiceRequestToken(kerberosTicket); Authentication authentication; try { authentication = authenticationManager.authenticate(authenticationRequest); } catch (AuthenticationException e) { logger.warn("Negotiate Header was invalid: " + header, e); SecurityContextHolder.clearContext(); if (failureHandler != null) { failureHandler.onAuthenticationFailure(request, response, e); } else { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.flushBuffer(); } return; } if (successHandler != null) { successHandler.onAuthenticationSuccess(request, response, authentication); } SecurityContextHolder.getContext().setAuthentication(authentication); } chain.doFilter(request, response); }  void doFilter(ServletRequest req, ServletResponse res, FilterChain chain); void setAuthenticationManager(AuthenticationManager authenticationManager); void setSuccessHandler(AuthenticationSuccessHandler successHandler); void setFailureHandler(AuthenticationFailureHandler failureHandler); void setSkipIfAlreadyAuthenticated(boolean skipIfAlreadyAuthenticated); @Override void afterPropertiesSet(); }
SpnegoAuthenticationProcessingFilter extends GenericFilterBean { public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; if (skipIfAlreadyAuthenticated) { Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication(); if (existingAuth != null && existingAuth.isAuthenticated() && (existingAuth instanceof AnonymousAuthenticationToken) == false) { chain.doFilter(request, response); return; } } String header = request.getHeader("Authorization"); if ((header != null) && header.startsWith("Negotiate ")) { if (logger.isDebugEnabled()) { logger.debug("Received Negotiate Header for request " + request.getRequestURL() + ": " + header); } byte[] base64Token = header.substring(10).getBytes("UTF-8"); byte[] kerberosTicket = Base64.decode(base64Token); KerberosServiceRequestToken authenticationRequest = new KerberosServiceRequestToken(kerberosTicket); Authentication authentication; try { authentication = authenticationManager.authenticate(authenticationRequest); } catch (AuthenticationException e) { logger.warn("Negotiate Header was invalid: " + header, e); SecurityContextHolder.clearContext(); if (failureHandler != null) { failureHandler.onAuthenticationFailure(request, response, e); } else { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.flushBuffer(); } return; } if (successHandler != null) { successHandler.onAuthenticationSuccess(request, response, authentication); } SecurityContextHolder.getContext().setAuthentication(authentication); } chain.doFilter(request, response); }  void doFilter(ServletRequest req, ServletResponse res, FilterChain chain); void setAuthenticationManager(AuthenticationManager authenticationManager); void setSuccessHandler(AuthenticationSuccessHandler successHandler); void setFailureHandler(AuthenticationFailureHandler failureHandler); void setSkipIfAlreadyAuthenticated(boolean skipIfAlreadyAuthenticated); @Override void afterPropertiesSet();  }