Search in sources :

Example 1 with ApplicationEventPublisher

use of org.springframework.context.ApplicationEventPublisher in project spring-security by spring-projects.

the class DefaultAuthenticationEventPublisherTests method additionalExceptionMappingsAreSupported.

@Test
public void additionalExceptionMappingsAreSupported() {
    publisher = new DefaultAuthenticationEventPublisher();
    Properties p = new Properties();
    p.put(MockAuthenticationException.class.getName(), AuthenticationFailureDisabledEvent.class.getName());
    publisher.setAdditionalExceptionMappings(p);
    ApplicationEventPublisher appPublisher = mock(ApplicationEventPublisher.class);
    publisher.setApplicationEventPublisher(appPublisher);
    publisher.publishAuthenticationFailure(new MockAuthenticationException("test"), mock(Authentication.class));
    verify(appPublisher).publishEvent(isA(AuthenticationFailureDisabledEvent.class));
}
Also used : Authentication(org.springframework.security.core.Authentication) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) AuthenticationFailureDisabledEvent(org.springframework.security.authentication.event.AuthenticationFailureDisabledEvent)

Example 2 with ApplicationEventPublisher

use of org.springframework.context.ApplicationEventPublisher in project spring-security by spring-projects.

the class DefaultAuthenticationEventPublisherTests method authenticationSuccessIsPublished.

@Test
public void authenticationSuccessIsPublished() {
    publisher = new DefaultAuthenticationEventPublisher();
    ApplicationEventPublisher appPublisher = mock(ApplicationEventPublisher.class);
    publisher.setApplicationEventPublisher(appPublisher);
    publisher.publishAuthenticationSuccess(mock(Authentication.class));
    verify(appPublisher).publishEvent(isA(AuthenticationSuccessEvent.class));
    publisher.setApplicationEventPublisher(null);
    // Should be ignored with null app publisher
    publisher.publishAuthenticationSuccess(mock(Authentication.class));
}
Also used : Authentication(org.springframework.security.core.Authentication) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) AuthenticationSuccessEvent(org.springframework.security.authentication.event.AuthenticationSuccessEvent)

Example 3 with ApplicationEventPublisher

use of org.springframework.context.ApplicationEventPublisher in project spring-security by spring-projects.

the class DefaultAuthenticationEventPublisherTests method expectedDefaultMappingsAreSatisfied.

@Test
public void expectedDefaultMappingsAreSatisfied() throws Exception {
    publisher = new DefaultAuthenticationEventPublisher();
    ApplicationEventPublisher appPublisher = mock(ApplicationEventPublisher.class);
    publisher.setApplicationEventPublisher(appPublisher);
    Authentication a = mock(Authentication.class);
    Exception cause = new Exception();
    Object extraInfo = new Object();
    publisher.publishAuthenticationFailure(new BadCredentialsException(""), a);
    publisher.publishAuthenticationFailure(new BadCredentialsException("", cause), a);
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureBadCredentialsEvent.class));
    reset(appPublisher);
    publisher.publishAuthenticationFailure(new UsernameNotFoundException(""), a);
    publisher.publishAuthenticationFailure(new UsernameNotFoundException("", cause), a);
    publisher.publishAuthenticationFailure(new AccountExpiredException(""), a);
    publisher.publishAuthenticationFailure(new AccountExpiredException("", cause), a);
    publisher.publishAuthenticationFailure(new ProviderNotFoundException(""), a);
    publisher.publishAuthenticationFailure(new DisabledException(""), a);
    publisher.publishAuthenticationFailure(new DisabledException("", cause), a);
    publisher.publishAuthenticationFailure(new LockedException(""), a);
    publisher.publishAuthenticationFailure(new LockedException("", cause), a);
    publisher.publishAuthenticationFailure(new AuthenticationServiceException(""), a);
    publisher.publishAuthenticationFailure(new AuthenticationServiceException("", cause), a);
    publisher.publishAuthenticationFailure(new CredentialsExpiredException(""), a);
    publisher.publishAuthenticationFailure(new CredentialsExpiredException("", cause), a);
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureBadCredentialsEvent.class));
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureExpiredEvent.class));
    verify(appPublisher).publishEvent(isA(AuthenticationFailureProviderNotFoundEvent.class));
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureDisabledEvent.class));
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureLockedEvent.class));
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureServiceExceptionEvent.class));
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureCredentialsExpiredEvent.class));
    verifyNoMoreInteractions(appPublisher);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationFailureProviderNotFoundEvent(org.springframework.security.authentication.event.AuthenticationFailureProviderNotFoundEvent) AuthenticationFailureBadCredentialsEvent(org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationException(org.springframework.security.core.AuthenticationException) AuthenticationFailureServiceExceptionEvent(org.springframework.security.authentication.event.AuthenticationFailureServiceExceptionEvent) AuthenticationFailureLockedEvent(org.springframework.security.authentication.event.AuthenticationFailureLockedEvent) Authentication(org.springframework.security.core.Authentication) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) AuthenticationFailureCredentialsExpiredEvent(org.springframework.security.authentication.event.AuthenticationFailureCredentialsExpiredEvent) AuthenticationFailureDisabledEvent(org.springframework.security.authentication.event.AuthenticationFailureDisabledEvent) AuthenticationFailureExpiredEvent(org.springframework.security.authentication.event.AuthenticationFailureExpiredEvent)

Example 4 with ApplicationEventPublisher

use of org.springframework.context.ApplicationEventPublisher in project spring-framework by spring-projects.

the class StompSubProtocolHandlerTests method eventPublicationWithExceptions.

@Test
public void eventPublicationWithExceptions() {
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    this.protocolHandler.setApplicationEventPublisher(publisher);
    this.protocolHandler.afterSessionStarted(this.session, this.channel);
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.CONNECT);
    Message<byte[]> message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    TextMessage textMessage = new TextMessage(new StompEncoder().encode(message));
    this.protocolHandler.handleMessageFromClient(this.session, textMessage, this.channel);
    verify(this.channel).send(this.messageCaptor.capture());
    Message<?> actual = this.messageCaptor.getValue();
    assertNotNull(actual);
    assertEquals(StompCommand.CONNECT, StompHeaderAccessor.wrap(actual).getCommand());
    reset(this.channel);
    headers = StompHeaderAccessor.create(StompCommand.CONNECTED);
    message = MessageBuilder.createMessage(EMPTY_PAYLOAD, headers.getMessageHeaders());
    this.protocolHandler.handleMessageToClient(this.session, message);
    assertEquals(1, this.session.getSentMessages().size());
    textMessage = (TextMessage) this.session.getSentMessages().get(0);
    assertEquals("CONNECTED\n" + "user-name:joe\n" + "\n" + "", textMessage.getPayload());
    this.protocolHandler.afterSessionEnded(this.session, CloseStatus.BAD_DATA, this.channel);
    verify(this.channel).send(this.messageCaptor.capture());
    actual = this.messageCaptor.getValue();
    assertNotNull(actual);
    StompHeaderAccessor accessor = StompHeaderAccessor.wrap(actual);
    assertEquals(StompCommand.DISCONNECT, accessor.getCommand());
    assertEquals("s1", accessor.getSessionId());
    assertEquals("joe", accessor.getUser().getName());
}
Also used : ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) StompEncoder(org.springframework.messaging.simp.stomp.StompEncoder) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.Test)

Example 5 with ApplicationEventPublisher

use of org.springframework.context.ApplicationEventPublisher in project spring-security by spring-projects.

the class DefaultAuthenticationEventPublisherTests method unknownFailureExceptionIsIgnored.

@Test
public void unknownFailureExceptionIsIgnored() throws Exception {
    publisher = new DefaultAuthenticationEventPublisher();
    Properties p = new Properties();
    p.put(MockAuthenticationException.class.getName(), AuthenticationFailureDisabledEvent.class.getName());
    publisher.setAdditionalExceptionMappings(p);
    ApplicationEventPublisher appPublisher = mock(ApplicationEventPublisher.class);
    publisher.setApplicationEventPublisher(appPublisher);
    publisher.publishAuthenticationFailure(new AuthenticationException("") {
    }, mock(Authentication.class));
    verifyZeroInteractions(appPublisher);
}
Also used : AuthenticationException(org.springframework.security.core.AuthenticationException) Authentication(org.springframework.security.core.Authentication) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) AuthenticationFailureDisabledEvent(org.springframework.security.authentication.event.AuthenticationFailureDisabledEvent)

Aggregations

ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)7 Authentication (org.springframework.security.core.Authentication)6 Test (org.junit.Test)3 AuthenticationFailureDisabledEvent (org.springframework.security.authentication.event.AuthenticationFailureDisabledEvent)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpSession (javax.servlet.http.HttpSession)2 ApplicationEvent (org.springframework.context.ApplicationEvent)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 AuthenticationException (org.springframework.security.core.AuthenticationException)2 SessionFixationProtectionEvent (org.springframework.security.web.authentication.session.SessionFixationProtectionEvent)2 SessionFixationProtectionStrategy (org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy)2 StompEncoder (org.springframework.messaging.simp.stomp.StompEncoder)1 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)1 AuthenticationFailureBadCredentialsEvent (org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent)1 AuthenticationFailureCredentialsExpiredEvent (org.springframework.security.authentication.event.AuthenticationFailureCredentialsExpiredEvent)1 AuthenticationFailureExpiredEvent (org.springframework.security.authentication.event.AuthenticationFailureExpiredEvent)1 AuthenticationFailureLockedEvent (org.springframework.security.authentication.event.AuthenticationFailureLockedEvent)1 AuthenticationFailureProviderNotFoundEvent (org.springframework.security.authentication.event.AuthenticationFailureProviderNotFoundEvent)1 AuthenticationFailureServiceExceptionEvent (org.springframework.security.authentication.event.AuthenticationFailureServiceExceptionEvent)1