34
34
import org .springframework .security .core .context .SecurityContextHolder ;
35
35
import org .springframework .security .web .WebAttributes ;
36
36
import org .springframework .security .web .authentication .*;
37
+ import org .springframework .security .web .util .matcher .RequestMatcher ;
37
38
import org .springframework .util .Assert ;
38
39
import org .springframework .web .filter .GenericFilterBean ;
39
40
73
74
* @author Luke Taylor
74
75
* @author Ruud Senden
75
76
* @author Rob Winch
77
+ * @author Tadaya Tsuyukubo
76
78
* @since 2.0
77
79
*/
78
80
public abstract class AbstractPreAuthenticatedProcessingFilter extends GenericFilterBean
@@ -86,6 +88,7 @@ public abstract class AbstractPreAuthenticatedProcessingFilter extends GenericFi
86
88
private boolean invalidateSessionOnPrincipalChange = true ;
87
89
private AuthenticationSuccessHandler authenticationSuccessHandler = null ;
88
90
private AuthenticationFailureHandler authenticationFailureHandler = null ;
91
+ private RequestMatcher requiresAuthenticationRequestMatcher = new PreAuthenticatedProcessingRequestMatcher ();
89
92
90
93
/**
91
94
* Check whether all required properties have been set.
@@ -114,7 +117,7 @@ public void doFilter(ServletRequest request, ServletResponse response,
114
117
+ SecurityContextHolder .getContext ().getAuthentication ());
115
118
}
116
119
117
- if (requiresAuthentication ((HttpServletRequest ) request )) {
120
+ if (requiresAuthenticationRequestMatcher . matches ((HttpServletRequest ) request )) {
118
121
doAuthenticate ((HttpServletRequest ) request , (HttpServletResponse ) response );
119
122
}
120
123
@@ -193,39 +196,6 @@ private void doAuthenticate(HttpServletRequest request, HttpServletResponse resp
193
196
}
194
197
}
195
198
196
- private boolean requiresAuthentication (HttpServletRequest request ) {
197
- Authentication currentUser = SecurityContextHolder .getContext ()
198
- .getAuthentication ();
199
-
200
- if (currentUser == null ) {
201
- return true ;
202
- }
203
-
204
- if (!checkForPrincipalChanges ) {
205
- return false ;
206
- }
207
-
208
- if (!principalChanged (request , currentUser )) {
209
- return false ;
210
- }
211
-
212
- logger .debug ("Pre-authenticated principal has changed and will be reauthenticated" );
213
-
214
- if (invalidateSessionOnPrincipalChange ) {
215
- SecurityContextHolder .clearContext ();
216
-
217
- HttpSession session = request .getSession (false );
218
-
219
- if (session != null ) {
220
- logger .debug ("Invalidating existing session" );
221
- session .invalidate ();
222
- request .getSession ();
223
- }
224
- }
225
-
226
- return true ;
227
- }
228
-
229
199
/**
230
200
* Puts the <code>Authentication</code> instance returned by the authentication
231
201
* manager into the secure context.
@@ -348,6 +318,14 @@ public void setAuthenticationFailureHandler(AuthenticationFailureHandler authent
348
318
this .authenticationFailureHandler = authenticationFailureHandler ;
349
319
}
350
320
321
+ /**
322
+ * Sets the request matcher to check whether to proceed the request further.
323
+ */
324
+ public void setRequiresAuthenticationRequestMatcher (RequestMatcher requiresAuthenticationRequestMatcher ) {
325
+ Assert .notNull (requiresAuthenticationRequestMatcher , "requestMatcher cannot be null" );
326
+ this .requiresAuthenticationRequestMatcher = requiresAuthenticationRequestMatcher ;
327
+ }
328
+
351
329
/**
352
330
* Override to extract the principal information from the current request
353
331
*/
@@ -359,4 +337,46 @@ public void setAuthenticationFailureHandler(AuthenticationFailureHandler authent
359
337
* return a dummy value.
360
338
*/
361
339
protected abstract Object getPreAuthenticatedCredentials (HttpServletRequest request );
340
+
341
+ /**
342
+ * Request matcher for default auth check logic
343
+ */
344
+ private class PreAuthenticatedProcessingRequestMatcher implements RequestMatcher {
345
+
346
+ @ Override
347
+ public boolean matches (HttpServletRequest request ) {
348
+
349
+ Authentication currentUser = SecurityContextHolder .getContext ().getAuthentication ();
350
+
351
+ if (currentUser == null ) {
352
+ return true ;
353
+ }
354
+
355
+ if (!checkForPrincipalChanges ) {
356
+ return false ;
357
+ }
358
+
359
+ if (!principalChanged (request , currentUser )) {
360
+ return false ;
361
+ }
362
+
363
+ logger .debug ("Pre-authenticated principal has changed and will be reauthenticated" );
364
+
365
+ if (invalidateSessionOnPrincipalChange ) {
366
+ SecurityContextHolder .clearContext ();
367
+
368
+ HttpSession session = request .getSession (false );
369
+
370
+ if (session != null ) {
371
+ logger .debug ("Invalidating existing session" );
372
+ session .invalidate ();
373
+ request .getSession ();
374
+ }
375
+ }
376
+
377
+ return true ;
378
+ }
379
+
380
+ }
381
+
362
382
}
0 commit comments