Polish DefaultAuthenticationEventPublisher

Simplified the constructor selection logic.

Issue gh-7825
This commit is contained in:
Josh Cummings 2020-02-06 14:11:18 -07:00
parent 51b9b2f693
commit 653400edfa
No known key found for this signature in database
GPG Key ID: 49EF60DD7FF83443
1 changed files with 8 additions and 10 deletions

View File

@ -107,8 +107,7 @@ public class DefaultAuthenticationEventPublisher implements AuthenticationEventP
public void publishAuthenticationFailure(AuthenticationException exception,
Authentication authentication) {
Constructor<? extends AbstractAuthenticationEvent> constructor = exceptionMappings
.get(exception.getClass().getName());
Constructor<? extends AbstractAuthenticationEvent> constructor = getEventConstructor(exception);
AbstractAuthenticationEvent event = null;
if (constructor != null) {
@ -118,13 +117,6 @@ public class DefaultAuthenticationEventPublisher implements AuthenticationEventP
catch (IllegalAccessException | InvocationTargetException | InstantiationException ignored) {
}
}
else if (defaultAuthenticationFailureEventConstructor != null) {
try {
event = defaultAuthenticationFailureEventConstructor.newInstance(authentication, exception);
}
catch (IllegalAccessException | InvocationTargetException | InstantiationException ignored) {
}
}
if (event != null) {
if (applicationEventPublisher != null) {
@ -139,6 +131,12 @@ public class DefaultAuthenticationEventPublisher implements AuthenticationEventP
}
}
private Constructor<? extends AbstractAuthenticationEvent> getEventConstructor(AuthenticationException exception) {
Constructor<? extends AbstractAuthenticationEvent> eventConstructor =
this.exceptionMappings.get(exception.getClass().getName());
return (eventConstructor == null ? this.defaultAuthenticationFailureEventConstructor : eventConstructor);
}
public void setApplicationEventPublisher(
ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
@ -181,7 +179,7 @@ public class DefaultAuthenticationEventPublisher implements AuthenticationEventP
public void setDefaultAuthenticationFailureEvent(
Class<? extends AbstractAuthenticationFailureEvent> defaultAuthenticationFailureEventClass) {
Assert.notNull(defaultAuthenticationFailureEventClass,
"The defaultAuthenticationFailureEventClass must not be null");
"defaultAuthenticationFailureEventClass must not be null");
try {
this.defaultAuthenticationFailureEventConstructor = defaultAuthenticationFailureEventClass
.getConstructor(Authentication.class, AuthenticationException.class);