Skip to content

Commit 653400e

Browse files
committed
Polish DefaultAuthenticationEventPublisher
Simplified the constructor selection logic. Issue gh-7825
1 parent 51b9b2f commit 653400e

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

core/src/main/java/org/springframework/security/authentication/DefaultAuthenticationEventPublisher.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ public void publishAuthenticationSuccess(Authentication authentication) {
107107

108108
public void publishAuthenticationFailure(AuthenticationException exception,
109109
Authentication authentication) {
110-
Constructor<? extends AbstractAuthenticationEvent> constructor = exceptionMappings
111-
.get(exception.getClass().getName());
110+
Constructor<? extends AbstractAuthenticationEvent> constructor = getEventConstructor(exception);
112111
AbstractAuthenticationEvent event = null;
113112

114113
if (constructor != null) {
@@ -118,13 +117,6 @@ public void publishAuthenticationFailure(AuthenticationException exception,
118117
catch (IllegalAccessException | InvocationTargetException | InstantiationException ignored) {
119118
}
120119
}
121-
else if (defaultAuthenticationFailureEventConstructor != null) {
122-
try {
123-
event = defaultAuthenticationFailureEventConstructor.newInstance(authentication, exception);
124-
}
125-
catch (IllegalAccessException | InvocationTargetException | InstantiationException ignored) {
126-
}
127-
}
128120

129121
if (event != null) {
130122
if (applicationEventPublisher != null) {
@@ -139,6 +131,12 @@ else if (defaultAuthenticationFailureEventConstructor != null) {
139131
}
140132
}
141133

134+
private Constructor<? extends AbstractAuthenticationEvent> getEventConstructor(AuthenticationException exception) {
135+
Constructor<? extends AbstractAuthenticationEvent> eventConstructor =
136+
this.exceptionMappings.get(exception.getClass().getName());
137+
return (eventConstructor == null ? this.defaultAuthenticationFailureEventConstructor : eventConstructor);
138+
}
139+
142140
public void setApplicationEventPublisher(
143141
ApplicationEventPublisher applicationEventPublisher) {
144142
this.applicationEventPublisher = applicationEventPublisher;
@@ -181,7 +179,7 @@ public void setAdditionalExceptionMappings(Properties additionalExceptionMapping
181179
public void setDefaultAuthenticationFailureEvent(
182180
Class<? extends AbstractAuthenticationFailureEvent> defaultAuthenticationFailureEventClass) {
183181
Assert.notNull(defaultAuthenticationFailureEventClass,
184-
"The defaultAuthenticationFailureEventClass must not be null");
182+
"defaultAuthenticationFailureEventClass must not be null");
185183
try {
186184
this.defaultAuthenticationFailureEventConstructor = defaultAuthenticationFailureEventClass
187185
.getConstructor(Authentication.class, AuthenticationException.class);

0 commit comments

Comments
 (0)