Skip to content

Commit 3f6aeb3

Browse files
committed
Improve AbstractAuthenticationFilterConfigurer by only using one instance of successHandler
Instead of keeping 2 instances of successHandler (defaultSuccessHandler) only one is kept and the request cache is only set if the instance implements `HasRequestCacheSetter` and the `trySetRequestCacheIntoSuccessHandler` flag is set to true (default). This way it's also easier for devs to customize the corresponding code as RequestCache must no longer be set manually.
1 parent 5fcde78 commit 3f6aeb3

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

config/src/main/java/org/springframework/security/config/annotation/web/configurers/AbstractAuthenticationFilterConfigurer.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
3333
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
3434
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
35+
import org.springframework.security.web.authentication.HasRequestCacheSetter;
3536
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
3637
import org.springframework.security.web.authentication.RememberMeServices;
3738
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
@@ -66,9 +67,9 @@ public abstract class AbstractAuthenticationFilterConfigurer<B extends HttpSecur
6667

6768
private AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource;
6869

69-
private SavedRequestAwareAuthenticationSuccessHandler defaultSuccessHandler = new SavedRequestAwareAuthenticationSuccessHandler();
70+
private AuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
7071

71-
private AuthenticationSuccessHandler successHandler = this.defaultSuccessHandler;
72+
private boolean trySetRequestCacheIntoSuccessHandler = true;
7273

7374
private LoginUrlAuthenticationEntryPoint authenticationEntryPoint;
7475

@@ -131,7 +132,7 @@ public final T defaultSuccessUrl(String defaultSuccessUrl, boolean alwaysUse) {
131132
SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
132133
handler.setDefaultTargetUrl(defaultSuccessUrl);
133134
handler.setAlwaysUseDefaultTargetUrl(alwaysUse);
134-
this.defaultSuccessHandler = handler;
135+
this.successHandler = handler;
135136
return successHandler(handler);
136137
}
137138

@@ -183,6 +184,16 @@ public final T successHandler(AuthenticationSuccessHandler successHandler) {
183184
return getSelf();
184185
}
185186

187+
/**
188+
* Should the {@link RequestCache} be set into the {@link AuthenticationSuccessHandler} if possible?
189+
* @param trySetRequestCacheIntoSuccessHandler true if the {@code RequestCache} should be tried to be set
190+
* @return the {@link AbstractAuthenticationFilterConfigurer} for additional customization
191+
*/
192+
public final T trySetRequestCacheIntoSuccessHandler(boolean trySetRequestCacheIntoSuccessHandler) {
193+
this.trySetRequestCacheIntoSuccessHandler = trySetRequestCacheIntoSuccessHandler;
194+
return getSelf();
195+
}
196+
186197
/**
187198
* Equivalent of invoking permitAll(true)
188199
* @return the {@link FormLoginConfigurer} for additional customization
@@ -273,9 +284,12 @@ public void configure(B http) {
273284
if (portMapper != null) {
274285
this.authenticationEntryPoint.setPortMapper(portMapper);
275286
}
276-
RequestCache requestCache = http.getSharedObject(RequestCache.class);
277-
if (requestCache != null) {
278-
this.defaultSuccessHandler.setRequestCache(requestCache);
287+
if(trySetRequestCacheIntoSuccessHandler
288+
&& successHandler instanceof HasRequestCacheSetter hasRequestCacheSetter) {
289+
RequestCache requestCache = http.getSharedObject(RequestCache.class);
290+
if (requestCache != null) {
291+
hasRequestCacheSetter.setRequestCache(requestCache);
292+
}
279293
}
280294
this.authFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
281295
this.authFilter.setAuthenticationSuccessHandler(this.successHandler);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.springframework.security.web.authentication;
2+
3+
import org.springframework.security.web.savedrequest.RequestCache;
4+
5+
public interface HasRequestCacheSetter {
6+
7+
void setRequestCache(RequestCache requestCache);
8+
}

web/src/main/java/org/springframework/security/web/authentication/SavedRequestAwareAuthenticationSuccessHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
* @author Luke Taylor
6464
* @since 3.0
6565
*/
66-
public class SavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
66+
public class SavedRequestAwareAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler
67+
implements HasRequestCacheSetter {
6768

6869
protected final Log logger = LogFactory.getLog(this.getClass());
6970

@@ -90,6 +91,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
9091
getRedirectStrategy().sendRedirect(request, response, targetUrl);
9192
}
9293

94+
@Override
9395
public void setRequestCache(RequestCache requestCache) {
9496
this.requestCache = requestCache;
9597
}

0 commit comments

Comments
 (0)