|
32 | 32 | import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; |
33 | 33 | import org.springframework.security.web.authentication.AuthenticationFailureHandler; |
34 | 34 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
| 35 | +import org.springframework.security.web.authentication.HasRequestCacheSetter; |
35 | 36 | import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; |
36 | 37 | import org.springframework.security.web.authentication.RememberMeServices; |
37 | 38 | import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; |
@@ -66,9 +67,9 @@ public abstract class AbstractAuthenticationFilterConfigurer<B extends HttpSecur |
66 | 67 |
|
67 | 68 | private AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource; |
68 | 69 |
|
69 | | - private SavedRequestAwareAuthenticationSuccessHandler defaultSuccessHandler = new SavedRequestAwareAuthenticationSuccessHandler(); |
| 70 | + private AuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); |
70 | 71 |
|
71 | | - private AuthenticationSuccessHandler successHandler = this.defaultSuccessHandler; |
| 72 | + private boolean trySetRequestCacheIntoSuccessHandler = true; |
72 | 73 |
|
73 | 74 | private LoginUrlAuthenticationEntryPoint authenticationEntryPoint; |
74 | 75 |
|
@@ -131,7 +132,7 @@ public final T defaultSuccessUrl(String defaultSuccessUrl, boolean alwaysUse) { |
131 | 132 | SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler(); |
132 | 133 | handler.setDefaultTargetUrl(defaultSuccessUrl); |
133 | 134 | handler.setAlwaysUseDefaultTargetUrl(alwaysUse); |
134 | | - this.defaultSuccessHandler = handler; |
| 135 | + this.successHandler = handler; |
135 | 136 | return successHandler(handler); |
136 | 137 | } |
137 | 138 |
|
@@ -183,6 +184,16 @@ public final T successHandler(AuthenticationSuccessHandler successHandler) { |
183 | 184 | return getSelf(); |
184 | 185 | } |
185 | 186 |
|
| 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 | + |
186 | 197 | /** |
187 | 198 | * Equivalent of invoking permitAll(true) |
188 | 199 | * @return the {@link FormLoginConfigurer} for additional customization |
@@ -273,9 +284,12 @@ public void configure(B http) { |
273 | 284 | if (portMapper != null) { |
274 | 285 | this.authenticationEntryPoint.setPortMapper(portMapper); |
275 | 286 | } |
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 | + } |
279 | 293 | } |
280 | 294 | this.authFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class)); |
281 | 295 | this.authFilter.setAuthenticationSuccessHandler(this.successHandler); |
|
0 commit comments