135
135
public final class OAuth2LoginConfigurer <B extends HttpSecurityBuilder <B >> extends
136
136
AbstractAuthenticationFilterConfigurer <B , OAuth2LoginConfigurer <B >, OAuth2LoginAuthenticationFilter > {
137
137
138
+ private final ApplicationContext context ;
138
139
private final AuthorizationEndpointConfig authorizationEndpointConfig = new AuthorizationEndpointConfig ();
139
140
private final TokenEndpointConfig tokenEndpointConfig = new TokenEndpointConfig ();
140
141
private final RedirectionEndpointConfig redirectionEndpointConfig = new RedirectionEndpointConfig ();
141
142
private final UserInfoEndpointConfig userInfoEndpointConfig = new UserInfoEndpointConfig ();
142
143
private String loginPage ;
143
144
private String loginProcessingUrl = OAuth2LoginAuthenticationFilter .DEFAULT_FILTER_PROCESSES_URI ;
144
145
146
+ public OAuth2LoginConfigurer (ApplicationContext context ) {
147
+ Assert .notNull (context , "context cannot be null" );
148
+ this .context = context ;
149
+ }
150
+
145
151
/**
146
152
* Sets the repository of client registrations.
147
153
*
@@ -506,18 +512,7 @@ public void init(B http) throws Exception {
506
512
accessTokenResponseClient = new DefaultAuthorizationCodeTokenResponseClient ();
507
513
}
508
514
509
- OAuth2UserService <OAuth2UserRequest , OAuth2User > oauth2UserService = this .userInfoEndpointConfig .userService ;
510
- if (oauth2UserService == null ) {
511
- if (!this .userInfoEndpointConfig .customUserTypes .isEmpty ()) {
512
- List <OAuth2UserService <OAuth2UserRequest , OAuth2User >> userServices = new ArrayList <>();
513
- userServices .add (new CustomUserTypesOAuth2UserService (this .userInfoEndpointConfig .customUserTypes ));
514
- userServices .add (new DefaultOAuth2UserService ());
515
- oauth2UserService = new DelegatingOAuth2UserService <>(userServices );
516
- } else {
517
- oauth2UserService = new DefaultOAuth2UserService ();
518
- }
519
- }
520
-
515
+ OAuth2UserService <OAuth2UserRequest , OAuth2User > oauth2UserService = getOAuth2UserService ();
521
516
OAuth2LoginAuthenticationProvider oauth2LoginAuthenticationProvider =
522
517
new OAuth2LoginAuthenticationProvider (accessTokenResponseClient , oauth2UserService );
523
518
GrantedAuthoritiesMapper userAuthoritiesMapper = this .getGrantedAuthoritiesMapper ();
@@ -530,11 +525,7 @@ public void init(B http) throws Exception {
530
525
"org.springframework.security.oauth2.jwt.JwtDecoder" , this .getClass ().getClassLoader ());
531
526
532
527
if (oidcAuthenticationProviderEnabled ) {
533
- OAuth2UserService <OidcUserRequest , OidcUser > oidcUserService = this .userInfoEndpointConfig .oidcUserService ;
534
- if (oidcUserService == null ) {
535
- oidcUserService = new OidcUserService ();
536
- }
537
-
528
+ OAuth2UserService <OidcUserRequest , OidcUser > oidcUserService = getOidcUserService ();
538
529
OidcAuthorizationCodeAuthenticationProvider oidcAuthorizationCodeAuthenticationProvider =
539
530
new OidcAuthorizationCodeAuthenticationProvider (accessTokenResponseClient , oidcUserService );
540
531
JwtDecoderFactory <ClientRegistration > jwtDecoderFactory = this .getJwtDecoderFactoryBean ();
@@ -627,6 +618,47 @@ private GrantedAuthoritiesMapper getGrantedAuthoritiesMapperBean() {
627
618
return (!grantedAuthoritiesMapperMap .isEmpty () ? grantedAuthoritiesMapperMap .values ().iterator ().next () : null );
628
619
}
629
620
621
+ private OAuth2UserService <OidcUserRequest , OidcUser > getOidcUserService () {
622
+ if (this .userInfoEndpointConfig .oidcUserService != null ) {
623
+ return this .userInfoEndpointConfig .oidcUserService ;
624
+ }
625
+ ResolvableType type = ResolvableType .forClassWithGenerics (OAuth2UserService .class , OidcUserRequest .class , OidcUser .class );
626
+ OAuth2UserService <OidcUserRequest , OidcUser > bean = getBeanOrNull (type );
627
+ if (bean == null ) {
628
+ return new OidcUserService ();
629
+ }
630
+
631
+ return bean ;
632
+ }
633
+
634
+ private OAuth2UserService <OAuth2UserRequest , OAuth2User > getOAuth2UserService () {
635
+ if (this .userInfoEndpointConfig .userService != null ) {
636
+ return this .userInfoEndpointConfig .userService ;
637
+ }
638
+ ResolvableType type = ResolvableType .forClassWithGenerics (OAuth2UserService .class , OAuth2UserRequest .class , OAuth2User .class );
639
+ OAuth2UserService <OAuth2UserRequest , OAuth2User > bean = getBeanOrNull (type );
640
+ if (bean == null ) {
641
+ if (!this .userInfoEndpointConfig .customUserTypes .isEmpty ()) {
642
+ List <OAuth2UserService <OAuth2UserRequest , OAuth2User >> userServices = new ArrayList <>();
643
+ userServices .add (new CustomUserTypesOAuth2UserService (this .userInfoEndpointConfig .customUserTypes ));
644
+ userServices .add (new DefaultOAuth2UserService ());
645
+ return new DelegatingOAuth2UserService <>(userServices );
646
+ } else {
647
+ return new DefaultOAuth2UserService ();
648
+ }
649
+ }
650
+
651
+ return bean ;
652
+ }
653
+
654
+ private <T > T getBeanOrNull (ResolvableType type ) {
655
+ String [] names = this .context .getBeanNamesForType (type );
656
+ if (names .length == 1 ) {
657
+ return (T ) this .context .getBean (names [0 ]);
658
+ }
659
+ return null ;
660
+ }
661
+
630
662
private void initDefaultLoginFilter (B http ) {
631
663
DefaultLoginPageGeneratingFilter loginPageGeneratingFilter = http .getSharedObject (DefaultLoginPageGeneratingFilter .class );
632
664
if (loginPageGeneratingFilter == null || this .isCustomLoginPage ()) {
0 commit comments