From 653400edfabf52760bdda3c30c8b51283b256b15 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Thu, 6 Feb 2020 14:11:18 -0700 Subject: [PATCH] Polish DefaultAuthenticationEventPublisher Simplified the constructor selection logic. Issue gh-7825 --- .../DefaultAuthenticationEventPublisher.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/springframework/security/authentication/DefaultAuthenticationEventPublisher.java b/core/src/main/java/org/springframework/security/authentication/DefaultAuthenticationEventPublisher.java index 937dea98f0..a2df060abf 100644 --- a/core/src/main/java/org/springframework/security/authentication/DefaultAuthenticationEventPublisher.java +++ b/core/src/main/java/org/springframework/security/authentication/DefaultAuthenticationEventPublisher.java @@ -107,8 +107,7 @@ public class DefaultAuthenticationEventPublisher implements AuthenticationEventP public void publishAuthenticationFailure(AuthenticationException exception, Authentication authentication) { - Constructor constructor = exceptionMappings - .get(exception.getClass().getName()); + Constructor 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 getEventConstructor(AuthenticationException exception) { + Constructor 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 defaultAuthenticationFailureEventClass) { Assert.notNull(defaultAuthenticationFailureEventClass, - "The defaultAuthenticationFailureEventClass must not be null"); + "defaultAuthenticationFailureEventClass must not be null"); try { this.defaultAuthenticationFailureEventConstructor = defaultAuthenticationFailureEventClass .getConstructor(Authentication.class, AuthenticationException.class);