AmbariBasicAuthenticationFilter extends BasicAuthenticationFilter implements AmbariAuthenticationFilter { @Override protected void onUnsuccessfulAuthentication(HttpServletRequest servletRequest, HttpServletResponse servletResponse, AuthenticationException authExecption) throws IOException { String header = servletRequest.getHeader("Authorization"); String username = null; try { username = getUsernameFromAuth(header, getCredentialsCharset(servletRequest)); } catch (Exception e) { LOG.warn("Error occurred during decoding authorization header.", e); } if (auditLogger.isEnabled()) { AuditEvent loginFailedAuditEvent = LoginAuditEvent.builder() .withRemoteIp(RequestUtils.getRemoteAddress(servletRequest)) .withTimestamp(System.currentTimeMillis()) .withReasonOfFailure("Invalid username/password combination") .withUserName(username) .build(); auditLogger.log(loginFailedAuditEvent); } } 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 testOnUnsuccessfulAuthentication() throws IOException, ServletException { HttpServletRequest request = createMock(HttpServletRequest.class); HttpServletResponse response = createMock(HttpServletResponse.class); AuthenticationException authEx = createMock(AuthenticationException.class); expect(request.getHeader("X-Forwarded-For")).andReturn("1.2.3.4"); expect(request.getHeader("Authorization")).andReturn( "Basic " + new String(Base64.encode("admin:admin".getBytes("UTF-8")))); expect(mockedAuditLogger.isEnabled()).andReturn(true); mockedAuditLogger.log(anyObject(AuditEvent.class)); expectLastCall().times(1); replayAll(); underTest.onUnsuccessfulAuthentication(request, response, authEx); verifyAll(); }