15
15
*/
16
16
package org .springframework .security .config .http ;
17
17
18
- import java .security .SecureRandom ;
19
- import java .util .ArrayList ;
20
- import java .util .Collections ;
21
- import java .util .List ;
22
- import java .util .Map ;
23
- import java .util .function .Function ;
24
- import javax .servlet .http .HttpServletRequest ;
25
-
26
18
import org .apache .commons .logging .Log ;
27
19
import org .apache .commons .logging .LogFactory ;
28
- import org .w3c .dom .Element ;
29
-
30
20
import org .springframework .beans .BeanMetadataElement ;
31
21
import org .springframework .beans .factory .config .BeanDefinition ;
32
22
import org .springframework .beans .factory .config .BeanReference ;
63
53
import org .springframework .security .web .authentication .www .BasicAuthenticationFilter ;
64
54
import org .springframework .security .web .csrf .CsrfToken ;
65
55
import org .springframework .util .Assert ;
56
+ import org .springframework .util .ClassUtils ;
66
57
import org .springframework .util .StringUtils ;
67
58
import org .springframework .util .xml .DomUtils ;
59
+ import org .w3c .dom .Element ;
60
+
61
+ import javax .servlet .http .HttpServletRequest ;
62
+ import java .security .SecureRandom ;
63
+ import java .util .ArrayList ;
64
+ import java .util .Collections ;
65
+ import java .util .List ;
66
+ import java .util .Map ;
67
+ import java .util .function .Function ;
68
68
69
69
import static org .springframework .security .config .http .SecurityFilters .ANONYMOUS_FILTER ;
70
70
import static org .springframework .security .config .http .SecurityFilters .BASIC_AUTH_FILTER ;
@@ -160,12 +160,16 @@ final class AuthenticationConfigBuilder {
160
160
161
161
private String openIDLoginPage ;
162
162
163
+ private boolean oauth2LoginEnabled ;
164
+ private boolean defaultAuthorizedClientRepositoryRegistered ;
163
165
private String oauth2LoginFilterId ;
164
166
private BeanDefinition oauth2AuthorizationRequestRedirectFilter ;
165
167
private BeanDefinition oauth2LoginEntryPoint ;
166
168
private BeanReference oauth2LoginAuthenticationProviderRef ;
167
169
private BeanReference oauth2LoginOidcAuthenticationProviderRef ;
168
170
private BeanDefinition oauth2LoginLinks ;
171
+
172
+ private boolean oauth2ClientEnabled ;
169
173
private BeanDefinition authorizationRequestRedirectFilter ;
170
174
private BeanDefinition authorizationCodeGrantFilter ;
171
175
private BeanReference authorizationCodeAuthenticationProviderRef ;
@@ -196,8 +200,7 @@ final class AuthenticationConfigBuilder {
196
200
createBasicFilter (authenticationManager );
197
201
createBearerTokenAuthenticationFilter (authenticationManager );
198
202
createFormLoginFilter (sessionStrategy , authenticationManager );
199
- createOAuth2LoginFilter (sessionStrategy , authenticationManager );
200
- createOAuth2ClientFilter (requestCache , authenticationManager );
203
+ createOAuth2ClientFilters (sessionStrategy , requestCache , authenticationManager );
201
204
createOpenIDLoginFilter (sessionStrategy , authenticationManager );
202
205
createX509Filter (authenticationManager );
203
206
createJeeFilter (authenticationManager );
@@ -274,15 +277,27 @@ void createFormLoginFilter(BeanReference sessionStrategy, BeanReference authMana
274
277
}
275
278
}
276
279
280
+ void createOAuth2ClientFilters (BeanReference sessionStrategy , BeanReference requestCache ,
281
+ BeanReference authenticationManager ) {
282
+ createOAuth2LoginFilter (sessionStrategy , authenticationManager );
283
+ createOAuth2ClientFilter (requestCache , authenticationManager );
284
+ registerOAuth2ClientPostProcessors ();
285
+ }
286
+
277
287
void createOAuth2LoginFilter (BeanReference sessionStrategy , BeanReference authManager ) {
278
288
Element oauth2LoginElt = DomUtils .getChildElementByTagName (this .httpElt , Elements .OAUTH2_LOGIN );
279
289
if (oauth2LoginElt == null ) {
280
290
return ;
281
291
}
292
+ this .oauth2LoginEnabled = true ;
282
293
283
294
OAuth2LoginBeanDefinitionParser parser = new OAuth2LoginBeanDefinitionParser (requestCache , portMapper ,
284
295
portResolver , sessionStrategy , allowSessionCreation );
285
296
BeanDefinition oauth2LoginFilterBean = parser .parse (oauth2LoginElt , this .pc );
297
+
298
+ BeanDefinition defaultAuthorizedClientRepository = parser .getDefaultAuthorizedClientRepository ();
299
+ registerDefaultAuthorizedClientRepositoryIfNecessary (defaultAuthorizedClientRepository );
300
+
286
301
oauth2LoginFilterBean .getPropertyValues ().addPropertyValue ("authenticationManager" , authManager );
287
302
288
303
// retrieve the other bean result
@@ -319,11 +334,15 @@ void createOAuth2ClientFilter(BeanReference requestCache, BeanReference authenti
319
334
if (oauth2ClientElt == null ) {
320
335
return ;
321
336
}
337
+ this .oauth2ClientEnabled = true ;
322
338
323
339
OAuth2ClientBeanDefinitionParser parser = new OAuth2ClientBeanDefinitionParser (
324
340
requestCache , authenticationManager );
325
341
parser .parse (oauth2ClientElt , this .pc );
326
342
343
+ BeanDefinition defaultAuthorizedClientRepository = parser .getDefaultAuthorizedClientRepository ();
344
+ registerDefaultAuthorizedClientRepositoryIfNecessary (defaultAuthorizedClientRepository );
345
+
327
346
this .authorizationRequestRedirectFilter = parser .getAuthorizationRequestRedirectFilter ();
328
347
String authorizationRequestRedirectFilterId = pc .getReaderContext ()
329
348
.generateBeanName (this .authorizationRequestRedirectFilter );
@@ -344,6 +363,28 @@ void createOAuth2ClientFilter(BeanReference requestCache, BeanReference authenti
344
363
this .authorizationCodeAuthenticationProviderRef = new RuntimeBeanReference (authorizationCodeAuthenticationProviderId );
345
364
}
346
365
366
+ void registerDefaultAuthorizedClientRepositoryIfNecessary (BeanDefinition defaultAuthorizedClientRepository ) {
367
+ if (!this .defaultAuthorizedClientRepositoryRegistered && defaultAuthorizedClientRepository != null ) {
368
+ String authorizedClientRepositoryId = pc .getReaderContext ()
369
+ .generateBeanName (defaultAuthorizedClientRepository );
370
+ this .pc .registerBeanComponent (new BeanComponentDefinition (
371
+ defaultAuthorizedClientRepository , authorizedClientRepositoryId ));
372
+ this .defaultAuthorizedClientRepositoryRegistered = true ;
373
+ }
374
+ }
375
+
376
+ private void registerOAuth2ClientPostProcessors () {
377
+ if (!this .oauth2LoginEnabled && !this .oauth2ClientEnabled ) {
378
+ return ;
379
+ }
380
+
381
+ boolean webmvcPresent = ClassUtils .isPresent ("org.springframework.web.servlet.DispatcherServlet" , getClass ().getClassLoader ());
382
+ if (webmvcPresent ) {
383
+ this .pc .getReaderContext ().registerWithGeneratedName (
384
+ new RootBeanDefinition (OAuth2ClientWebMvcSecurityPostProcessor .class ));
385
+ }
386
+ }
387
+
347
388
void createOpenIDLoginFilter (BeanReference sessionStrategy , BeanReference authManager ) {
348
389
Element openIDLoginElt = DomUtils .getChildElementByTagName (httpElt ,
349
390
Elements .OPENID_LOGIN );
0 commit comments