Search in sources :

Example 1 with DefaultAuthenticationEventPublisher

use of org.springframework.security.authentication.DefaultAuthenticationEventPublisher in project spring-security by spring-projects.

the class AuthenticationManagerBeanDefinitionParserTests method eventsArePublishedByDefault.

@Test
public void eventsArePublishedByDefault() throws Exception {
    ConfigurableApplicationContext appContext = this.spring.context(CONTEXT).getContext();
    AuthListener listener = new AuthListener();
    appContext.addApplicationListener(listener);
    ProviderManager pm = (ProviderManager) appContext.getBeansOfType(ProviderManager.class).values().toArray()[0];
    Object eventPublisher = FieldUtils.getFieldValue(pm, "eventPublisher");
    assertThat(eventPublisher).isNotNull();
    assertThat(eventPublisher instanceof DefaultAuthenticationEventPublisher).isTrue();
    pm.authenticate(new UsernamePasswordAuthenticationToken("bob", "bobspassword"));
    assertThat(listener.events).hasSize(1);
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) DefaultAuthenticationEventPublisher(org.springframework.security.authentication.DefaultAuthenticationEventPublisher) ProviderManager(org.springframework.security.authentication.ProviderManager) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultAuthenticationEventPublisher

use of org.springframework.security.authentication.DefaultAuthenticationEventPublisher in project spring-security by spring-projects.

the class DefaultAuthenticationEventPublisherBearerTokenTests method publishAuthenticationFailureWhenInvalidBearerTokenExceptionThenMaps.

@Test
public void publishAuthenticationFailureWhenInvalidBearerTokenExceptionThenMaps() {
    ApplicationEventPublisher appPublisher = mock(ApplicationEventPublisher.class);
    Authentication authentication = new JwtAuthenticationToken(TestJwts.jwt().build());
    Exception cause = new Exception();
    this.publisher = new DefaultAuthenticationEventPublisher(appPublisher);
    this.publisher.publishAuthenticationFailure(new InvalidBearerTokenException("invalid"), authentication);
    this.publisher.publishAuthenticationFailure(new InvalidBearerTokenException("invalid", cause), authentication);
    verify(appPublisher, times(2)).publishEvent(isA(AuthenticationFailureBadCredentialsEvent.class));
}
Also used : DefaultAuthenticationEventPublisher(org.springframework.security.authentication.DefaultAuthenticationEventPublisher) JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) Authentication(org.springframework.security.core.Authentication) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) AuthenticationFailureBadCredentialsEvent(org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultAuthenticationEventPublisher

use of org.springframework.security.authentication.DefaultAuthenticationEventPublisher in project dhis2-core by dhis2.

the class WebTestConfiguration method authenticationEventPublisher.

@Bean
public DefaultAuthenticationEventPublisher authenticationEventPublisher() {
    DefaultAuthenticationEventPublisher defaultAuthenticationEventPublisher = new DefaultAuthenticationEventPublisher();
    defaultAuthenticationEventPublisher.setAdditionalExceptionMappings(ImmutableMap.of(OAuth2AuthenticationException.class, AuthenticationFailureBadCredentialsEvent.class));
    return defaultAuthenticationEventPublisher;
}
Also used : DefaultAuthenticationEventPublisher(org.springframework.security.authentication.DefaultAuthenticationEventPublisher) AuthenticationFailureBadCredentialsEvent(org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent) OAuth2AuthenticationException(org.springframework.security.oauth2.core.OAuth2AuthenticationException) Bean(org.springframework.context.annotation.Bean)

Example 4 with DefaultAuthenticationEventPublisher

use of org.springframework.security.authentication.DefaultAuthenticationEventPublisher in project spring-security by spring-projects.

the class GlobalMethodSecurityConfiguration method authenticationManager.

/**
 * Allows providing a custom {@link AuthenticationManager}. The default is to use any
 * authentication mechanisms registered by
 * {@link #configure(AuthenticationManagerBuilder)}. If
 * {@link #configure(AuthenticationManagerBuilder)} was not overridden, then an
 * {@link AuthenticationManager} is attempted to be autowired by type.
 * @return the {@link AuthenticationManager} to use
 */
protected AuthenticationManager authenticationManager() throws Exception {
    if (this.authenticationManager == null) {
        DefaultAuthenticationEventPublisher eventPublisher = this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher());
        this.auth = new AuthenticationManagerBuilder(this.objectPostProcessor);
        this.auth.authenticationEventPublisher(eventPublisher);
        configure(this.auth);
        this.authenticationManager = (this.disableAuthenticationRegistry) ? getAuthenticationConfiguration().getAuthenticationManager() : this.auth.build();
    }
    return this.authenticationManager;
}
Also used : DefaultAuthenticationEventPublisher(org.springframework.security.authentication.DefaultAuthenticationEventPublisher) AuthenticationManagerBuilder(org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder)

Example 5 with DefaultAuthenticationEventPublisher

use of org.springframework.security.authentication.DefaultAuthenticationEventPublisher in project dhis2-core by dhis2.

the class AuthenticationProviderConfig method authenticationEventPublisher.

@Bean
public DefaultAuthenticationEventPublisher authenticationEventPublisher() {
    DefaultAuthenticationEventPublisher defaultAuthenticationEventPublisher = new DefaultAuthenticationEventPublisher();
    defaultAuthenticationEventPublisher.setAdditionalExceptionMappings(ImmutableMap.of(OAuth2AuthenticationException.class, AuthenticationFailureBadCredentialsEvent.class));
    return defaultAuthenticationEventPublisher;
}
Also used : DefaultAuthenticationEventPublisher(org.springframework.security.authentication.DefaultAuthenticationEventPublisher) AuthenticationFailureBadCredentialsEvent(org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent) OAuth2AuthenticationException(org.springframework.security.oauth2.core.OAuth2AuthenticationException) Bean(org.springframework.context.annotation.Bean)

Aggregations

DefaultAuthenticationEventPublisher (org.springframework.security.authentication.DefaultAuthenticationEventPublisher)5 AuthenticationFailureBadCredentialsEvent (org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent)3 Test (org.junit.jupiter.api.Test)2 Bean (org.springframework.context.annotation.Bean)2 OAuth2AuthenticationException (org.springframework.security.oauth2.core.OAuth2AuthenticationException)2 ApplicationEventPublisher (org.springframework.context.ApplicationEventPublisher)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 ProviderManager (org.springframework.security.authentication.ProviderManager)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 AuthenticationManagerBuilder (org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder)1 Authentication (org.springframework.security.core.Authentication)1 JwtAuthenticationToken (org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken)1