Skip to content

Commit

Permalink
Polish DefaultAuthenticationEventPublisher
Browse files Browse the repository at this point in the history
Simplified the constructor selection logic.

Issue gh-7825
  • Loading branch information
jzheaux committed Feb 6, 2020
1 parent 51b9b2f commit 653400e
Showing 1 changed file with 8 additions and 10 deletions.
Expand Up @@ -107,8 +107,7 @@ public void publishAuthenticationSuccess(Authentication authentication) {

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) {
Expand All @@ -118,13 +117,6 @@ public void publishAuthenticationFailure(AuthenticationException exception,
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) {
Expand All @@ -139,6 +131,12 @@ else if (defaultAuthenticationFailureEventConstructor != null) {
}
}

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;
Expand Down Expand Up @@ -181,7 +179,7 @@ public void setAdditionalExceptionMappings(Properties additionalExceptionMapping
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);
Expand Down

0 comments on commit 653400e

Please sign in to comment.