AmbariBasicAuthenticationFilter extends BasicAuthenticationFilter implements AmbariAuthenticationFilter { @Override protected void onSuccessfulAuthentication(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Authentication authResult) throws IOException { if (auditLogger.isEnabled()) { AuditEvent loginSucceededAuditEvent = LoginAuditEvent.builder() .withRemoteIp(RequestUtils.getRemoteAddress(servletRequest)) .withUserName(authResult.getName()) .withTimestamp(System.currentTimeMillis()) .withRoles(permissionHelper.getPermissionLabels(authResult)) .build(); auditLogger.log(loginSucceededAuditEvent); } } 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 testOnSuccessfulAuthentication() throws IOException, ServletException { HttpServletRequest request = createMock(HttpServletRequest.class); HttpServletResponse response = createMock(HttpServletResponse.class); Authentication authentication = createMock(Authentication.class); Map<String, List<String>> roles = new HashMap<>(); roles.put("a", Arrays.asList("r1", "r2", "r3")); expect(permissionHelper.getPermissionLabels(authentication)) .andReturn(roles); expect(request.getHeader("X-Forwarded-For")).andReturn("1.2.3.4"); expect(authentication.getName()).andReturn("admin"); expect(mockedAuditLogger.isEnabled()).andReturn(true); mockedAuditLogger.log(anyObject(AuditEvent.class)); expectLastCall().times(1); replayAll(); underTest.onSuccessfulAuthentication(request, response, authentication); verifyAll(); }