Parcourir la source

Polish DefaultAuthenticationEventPublisher

Simplified the constructor selection logic.

Issue gh-7825
Josh Cummings il y a 5 ans
Parent
commit
653400edfa

+ 8 - 10
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<? 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);