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 937dea98f09..a2df060abf1 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 void publishAuthenticationSuccess(Authentication authentication) { 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 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) { @@ -139,6 +131,12 @@ else if (defaultAuthenticationFailureEventConstructor != null) { } } + 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 void setAdditionalExceptionMappings(Properties additionalExceptionMapping 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);