Skip to content

Commit fd85f96

Browse files
committed
AuthenticationEventPublisher DSL Lookup
Fixes gh-4400
1 parent 2fa31cf commit fd85f96

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configuration/WebSecurityConfigurerAdapter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ protected final HttpSecurity getHttp() throws Exception {
201201

202202
AuthenticationManager authenticationManager = authenticationManager();
203203
authenticationBuilder.parentAuthenticationManager(authenticationManager);
204-
authenticationBuilder.authenticationEventPublisher(eventPublisher);
205204
Map<Class<?>, Object> sharedObjects = createSharedObjects();
206205

207206
http = new HttpSecurity(objectPostProcessor, authenticationBuilder,
@@ -383,6 +382,11 @@ public AuthenticationManagerBuilder eraseCredentials(boolean eraseCredentials) {
383382
return super.eraseCredentials(eraseCredentials);
384383
}
385384

385+
@Override
386+
public AuthenticationManagerBuilder authenticationEventPublisher(AuthenticationEventPublisher eventPublisher) {
387+
authenticationBuilder.authenticationEventPublisher(eventPublisher);
388+
return super.authenticationEventPublisher(eventPublisher);
389+
}
386390
};
387391
}
388392

config/src/test/java/org/springframework/security/config/annotation/web/WebSecurityConfigurerAdapterTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
4343
import org.springframework.security.config.test.SpringTestRule;
4444
import org.springframework.security.core.Authentication;
45+
import org.springframework.security.core.AuthenticationException;
4546
import org.springframework.security.core.userdetails.PasswordEncodedUser;
4647
import org.springframework.security.core.userdetails.UserDetailsService;
4748
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@@ -398,4 +399,30 @@ public AuthenticationEventPublisher authenticationEventPublisher() {
398399
return mock(AuthenticationEventPublisher.class);
399400
}
400401
}
402+
403+
// gh-4400
404+
@Test
405+
public void performWhenUsingAuthenticationEventPublisherInDslThenUses() throws Exception {
406+
this.spring.register(CustomAuthenticationEventPublisherDsl.class).autowire();
407+
408+
AuthenticationEventPublisher authenticationEventPublisher =
409+
CustomAuthenticationEventPublisherDsl.EVENT_PUBLISHER;
410+
411+
this.mockMvc.perform(get("/")
412+
.with(httpBasic("user", "password"))); // fails since no providers configured
413+
414+
verify(authenticationEventPublisher).publishAuthenticationFailure(
415+
any(AuthenticationException.class),
416+
any(Authentication.class));
417+
}
418+
419+
@EnableWebSecurity
420+
static class CustomAuthenticationEventPublisherDsl extends WebSecurityConfigurerAdapter {
421+
static AuthenticationEventPublisher EVENT_PUBLISHER = mock(AuthenticationEventPublisher.class);
422+
423+
@Override
424+
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
425+
auth.authenticationEventPublisher(EVENT_PUBLISHER);
426+
}
427+
}
401428
}

0 commit comments

Comments
 (0)