public void testAuthenticationFailureEvent() throws Exception {
    String userName = "******";
    String ip = "1.2.3.4";
    String sessionId = "it tastes just like our regular coffee";

    HttpServletRequest request = createMock(HttpServletRequest.class);
    HttpSession session = createMock(HttpSession.class);
    expect(request.getRemoteAddr()).andReturn(ip);
    expect(request.getSession(false)).andReturn(session);
    expect(session.getId()).andReturn(sessionId);

    replay(request, session);
    WebAuthenticationDetails details = new WebAuthenticationDetails(request);
    verify(request, session);

    org.springframework.security.core.Authentication authentication =
        new TestingDetailsAuthenticationToken(
            userName, "cheesiness", new GrantedAuthority[0], details);
    AuthenticationFailureBadCredentialsEvent authEvent =
        new AuthenticationFailureBadCredentialsEvent(
            authentication, new BadCredentialsException("you are bad!"));

    SecurityAuthenticationEventOnmsEventBuilder builder =
        new SecurityAuthenticationEventOnmsEventBuilder();
    builder.setEventProxy(m_eventProxy);
    builder.afterPropertiesSet();

    EventBuilder eventBuilder =
        new EventBuilder(SecurityAuthenticationEventOnmsEventBuilder.FAILURE_UEI, "OpenNMS.WebUI");
    eventBuilder.addParam("user", userName);
    eventBuilder.addParam("ip", ip);
    eventBuilder.addParam("exceptionName", authEvent.getException().getClass().getSimpleName());
    eventBuilder.addParam("exceptionMessage", authEvent.getException().getMessage());

    m_eventProxy.send(EventEquals.eqEvent(eventBuilder.getEvent()));

    m_mocks.replayAll();
    builder.onApplicationEvent(authEvent);
    m_mocks.verifyAll();
  }
コード例 #2
0
ファイル: AuditListener.java プロジェクト: rumale/uaa
  @Override
  public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof AbstractUaaEvent) {
      ((AbstractUaaEvent) event).process(uaaAuditService);
    } else if (event instanceof AuthenticationFailureBadCredentialsEvent) {
      AuthenticationFailureBadCredentialsEvent bce =
          (AuthenticationFailureBadCredentialsEvent) event;
      String principal = bce.getAuthentication().getName();
      UaaAuthenticationDetails details =
          (UaaAuthenticationDetails) bce.getAuthentication().getDetails();

      if (bce.getException() instanceof UsernameNotFoundException) {
        uaaAuditService.principalNotFound(principal, details);
      } else {
        uaaAuditService.principalAuthenticationFailure(principal, details);
      }
    }
  }