Search in sources :

Example 1 with AuthenticationFailureBadCredentialsEvent

use of org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent in project opennms by OpenNMS.

the class SecurityAuthenticationEventOnmsEventBuilderTest method testAuthenticationFailureEvent.

public void testAuthenticationFailureEvent() throws Exception {
    String userName = "bar";
    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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) HttpSession(javax.servlet.http.HttpSession) WebAuthenticationDetails(org.springframework.security.web.authentication.WebAuthenticationDetails) AuthenticationFailureBadCredentialsEvent(org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 2 with AuthenticationFailureBadCredentialsEvent

use of org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent in project GMM by Katharsas.

the class EventSendingAuthenticationFailureHandler method onAuthenticationFailure.

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    if (exception instanceof BadCredentialsException) {
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        Authentication auth = new UsernamePasswordAuthenticationToken(name, password);
        eventPublisher.publishEvent(new AuthenticationFailureBadCredentialsEvent(auth, exception));
    }
    super.onAuthenticationFailure(request, response, exception);
}
Also used : Authentication(org.springframework.security.core.Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AuthenticationFailureBadCredentialsEvent(org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Aggregations

BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 AuthenticationFailureBadCredentialsEvent (org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 WebAuthenticationDetails (org.springframework.security.web.authentication.WebAuthenticationDetails)1