AmbariBasicAuthenticationFilter extends BasicAuthenticationFilter implements AmbariAuthenticationFilter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; if (auditLogger.isEnabled() && shouldApply(httpServletRequest) && (AuthorizationHelper.getAuthenticatedName() == null)) { AuditEvent loginFailedAuditEvent = LoginAuditEvent.builder() .withRemoteIp(RequestUtils.getRemoteAddress(httpServletRequest)) .withTimestamp(System.currentTimeMillis()) .withReasonOfFailure("Authentication required") .withUserName(null) .build(); auditLogger.log(loginFailedAuditEvent); } super.doFilter(servletRequest, servletResponse, chain); } AmbariBasicAuthenticationFilter(AuthenticationManager authenticationManager,
                                         AmbariEntryPoint ambariEntryPoint,
                                         AuditLogger auditLogger,
                                         PermissionHelper permissionHelper); @Override boolean shouldApply(HttpServletRequest httpServletRequest); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain);  }
@Test public void testDoFilter() throws IOException, ServletException { SecurityContextHolder.getContext().setAuthentication(null); HttpServletRequest request = createMock(HttpServletRequest.class); HttpServletResponse response = createMock(HttpServletResponse.class); FilterChain filterChain = createMock(FilterChain.class); expect(request.getHeader("Authorization")).andReturn("Basic ").andReturn(null); expect(request.getHeader("X-Forwarded-For")).andReturn("1.2.3.4").anyTimes(); expect(mockedAuditLogger.isEnabled()).andReturn(true).anyTimes(); mockedAuditLogger.log(anyObject(AuditEvent.class)); expectLastCall().times(1); filterChain.doFilter(request, response); expectLastCall(); replayAll(); underTest.doFilter(request, response, filterChain); verifyAll(); }