Skip to content

Commit 250dc4b

Browse files
committed
DefaultAuthenticationEventPublisher is now configurablAe via a Map
1 parent 7d84544 commit 250dc4b

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

core/src/main/java/org/springframework/security/authentication/DefaultAuthenticationEventPublisher.java

+23
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ public void setApplicationEventPublisher(
149149
* @param additionalExceptionMappings where keys are the fully-qualified string name
150150
* of the exception class and the values are the fully-qualified string name of the
151151
* event class to fire.
152+
*
153+
* @deprecated use {@link #setAdditionalExceptionMappings(Map)}
152154
*/
155+
@Deprecated
153156
@SuppressWarnings({ "unchecked" })
154157
public void setAdditionalExceptionMappings(Properties additionalExceptionMappings) {
155158
Assert.notNull(additionalExceptionMappings,
@@ -169,6 +172,26 @@ public void setAdditionalExceptionMappings(Properties additionalExceptionMapping
169172
}
170173
}
171174

175+
/**
176+
* Sets additional exception to event mappings. These are automatically merged with
177+
* the default exception to event mappings that <code>ProviderManager</code> defines.
178+
*
179+
* @param mappings where keys are exception classes and values are event classes.
180+
* @since 5.3
181+
*/
182+
public void setAdditionalExceptionMappings(Map<Class<? extends AuthenticationException>,
183+
Class<? extends AbstractAuthenticationFailureEvent>> mappings){
184+
Assert.notEmpty(mappings, "The mappings Map must not be empty nor null");
185+
for (Map.Entry<Class<? extends AuthenticationException>, Class<? extends AbstractAuthenticationFailureEvent>> entry
186+
: mappings.entrySet()) {
187+
Class<?> exceptionClass = entry.getKey();
188+
Class<?> eventClass = entry.getValue();
189+
Assert.notNull(exceptionClass, "exceptionClass cannot be null");
190+
Assert.notNull(eventClass, "eventClass cannot be null");
191+
addMapping(exceptionClass.getName(), (Class<? extends AbstractAuthenticationFailureEvent>) eventClass);
192+
}
193+
}
194+
172195
/**
173196
* Sets a default authentication failure event as a fallback event for any unmapped
174197
* exceptions not mapped in the exception mappings.

core/src/test/java/org/springframework/security/authentication/DefaultAuthenticationEventPublisherTests.java

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919

2020
import org.junit.*;
2121
import org.springframework.context.ApplicationEventPublisher;
22+
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
2223
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
2324
import org.springframework.security.authentication.event.AuthenticationFailureCredentialsExpiredEvent;
2425
import org.springframework.security.authentication.event.AuthenticationFailureDisabledEvent;
@@ -138,6 +139,47 @@ public void unknownFailureExceptionIsIgnored() {
138139
verifyZeroInteractions(appPublisher);
139140
}
140141

142+
@Test(expected = IllegalArgumentException.class)
143+
public void emptyMapCausesException() {
144+
Map<Class<? extends AuthenticationException>,
145+
Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>();
146+
publisher = new DefaultAuthenticationEventPublisher();
147+
publisher.setAdditionalExceptionMappings(mappings);
148+
}
149+
150+
@Test(expected = IllegalArgumentException.class)
151+
public void missingExceptionClassCausesException() {
152+
Map<Class<? extends AuthenticationException>,
153+
Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>();
154+
mappings.put(null, AuthenticationFailureLockedEvent.class);
155+
publisher = new DefaultAuthenticationEventPublisher();
156+
publisher.setAdditionalExceptionMappings(mappings);
157+
}
158+
159+
@Test(expected = IllegalArgumentException.class)
160+
public void missingEventClassAsMapValueCausesException() {
161+
Map<Class<? extends AuthenticationException>,
162+
Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>();
163+
mappings.put(LockedException.class, null);
164+
publisher = new DefaultAuthenticationEventPublisher();
165+
publisher.setAdditionalExceptionMappings(mappings);
166+
}
167+
168+
@Test
169+
public void additionalExceptionMappingsUsingMapAreSupported() {
170+
publisher = new DefaultAuthenticationEventPublisher();
171+
Map<Class<? extends AuthenticationException>,
172+
Class<? extends AbstractAuthenticationFailureEvent>> mappings = new HashMap<>();
173+
mappings.put(MockAuthenticationException.class,AuthenticationFailureDisabledEvent.class);
174+
publisher.setAdditionalExceptionMappings(mappings);
175+
ApplicationEventPublisher appPublisher = mock(ApplicationEventPublisher.class);
176+
177+
publisher.setApplicationEventPublisher(appPublisher);
178+
publisher.publishAuthenticationFailure(new MockAuthenticationException("test"),
179+
mock(Authentication.class));
180+
verify(appPublisher).publishEvent(isA(AuthenticationFailureDisabledEvent.class));
181+
}
182+
141183
@Test(expected = IllegalArgumentException.class)
142184
public void defaultAuthenticationFailureEventClassSetNullThen() {
143185
publisher = new DefaultAuthenticationEventPublisher();

0 commit comments

Comments
 (0)