31
31
import org .springframework .context .MessageSourceAware ;
32
32
import org .springframework .context .support .MessageSourceAccessor ;
33
33
import org .springframework .core .log .LogMessage ;
34
+ import org .springframework .security .authentication .AuthenticationCredentialsNotFoundException ;
34
35
import org .springframework .security .authentication .AuthenticationDetailsSource ;
35
36
import org .springframework .security .authentication .AuthenticationManager ;
36
37
import org .springframework .security .authentication .InternalAuthenticationServiceException ;
@@ -122,6 +123,11 @@ public abstract class AbstractAuthenticationProcessingFilter extends GenericFilt
122
123
123
124
protected AuthenticationDetailsSource <HttpServletRequest , ?> authenticationDetailsSource = new WebAuthenticationDetailsSource ();
124
125
126
+ private AuthenticationConverter authenticationConverter = (request ) -> {
127
+ throw new AuthenticationCredentialsNotFoundException (
128
+ "Please either configure an AuthenticationConverter or override attemptAuthentication when extending AbstractAuthenticationProcessingFilter" );
129
+ };
130
+
125
131
private AuthenticationManager authenticationManager ;
126
132
127
133
protected MessageSourceAccessor messages = SpringSecurityMessageSource .getAccessor ();
@@ -132,6 +138,8 @@ public abstract class AbstractAuthenticationProcessingFilter extends GenericFilt
132
138
133
139
private boolean continueChainBeforeSuccessfulAuthentication = false ;
134
140
141
+ private boolean continueChainWhenNoAuthenticationResult ;
142
+
135
143
private SessionAuthenticationStrategy sessionStrategy = new NullAuthenticatedSessionStrategy ();
136
144
137
145
private boolean allowSessionCreation = true ;
@@ -230,6 +238,10 @@ private void doFilter(HttpServletRequest request, HttpServletResponse response,
230
238
try {
231
239
Authentication authenticationResult = attemptAuthentication (request , response );
232
240
if (authenticationResult == null ) {
241
+ if (this .continueChainWhenNoAuthenticationResult ) {
242
+ chain .doFilter (request , response );
243
+ return ;
244
+ }
233
245
// return immediately as subclass has indicated that it hasn't completed
234
246
return ;
235
247
}
@@ -292,8 +304,18 @@ protected boolean requiresAuthentication(HttpServletRequest request, HttpServlet
292
304
* @return the authenticated user token, or null if authentication is incomplete.
293
305
* @throws AuthenticationException if authentication fails.
294
306
*/
295
- public abstract Authentication attemptAuthentication (HttpServletRequest request , HttpServletResponse response )
296
- throws AuthenticationException , IOException , ServletException ;
307
+ public Authentication attemptAuthentication (HttpServletRequest request , HttpServletResponse response )
308
+ throws AuthenticationException , IOException , ServletException {
309
+ Authentication authentication = this .authenticationConverter .convert (request );
310
+ if (authentication == null ) {
311
+ return null ;
312
+ }
313
+ Authentication result = this .authenticationManager .authenticate (authentication );
314
+ if (result == null ) {
315
+ throw new ServletException ("AuthenticationManager should not return null Authentication object." );
316
+ }
317
+ return result ;
318
+ }
297
319
298
320
/**
299
321
* Default behaviour for successful authentication.
@@ -354,6 +376,12 @@ protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServle
354
376
this .failureHandler .onAuthenticationFailure (request , response , failed );
355
377
}
356
378
379
+ public void setAuthenticationConverter (AuthenticationConverter authenticationConverter ) {
380
+ Assert .notNull (authenticationConverter , "authenticationConverter cannot be null" );
381
+ this .authenticationConverter = authenticationConverter ;
382
+ this .continueChainWhenNoAuthenticationResult = true ;
383
+ }
384
+
357
385
protected AuthenticationManager getAuthenticationManager () {
358
386
return this .authenticationManager ;
359
387
}
0 commit comments