|
57 | 57 | import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; |
58 | 58 | import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest; |
59 | 59 | import org.springframework.security.oauth2.client.oidc.authentication.OidcAuthorizationCodeAuthenticationProvider; |
60 | | -import org.springframework.security.oauth2.client.oidc.authentication.RefreshOidcIdTokenHandler; |
| 60 | +import org.springframework.security.oauth2.client.oidc.authentication.OidcAuthorizedClientRefreshedEventListener; |
61 | 61 | import org.springframework.security.oauth2.client.oidc.session.InMemoryOidcSessionRegistry; |
62 | 62 | import org.springframework.security.oauth2.client.oidc.session.OidcSessionInformation; |
63 | 63 | import org.springframework.security.oauth2.client.oidc.session.OidcSessionRegistry; |
|
91 | 91 | import org.springframework.security.web.authentication.session.SessionAuthenticationException; |
92 | 92 | import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy; |
93 | 93 | import org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter; |
| 94 | +import org.springframework.security.web.context.SecurityContextRepository; |
94 | 95 | import org.springframework.security.web.csrf.CsrfToken; |
95 | 96 | import org.springframework.security.web.savedrequest.RequestCache; |
96 | 97 | import org.springframework.security.web.util.matcher.AndRequestMatcher; |
@@ -387,23 +388,26 @@ public void init(B http) throws Exception { |
387 | 388 | OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService = getOidcUserService(); |
388 | 389 | OidcAuthorizationCodeAuthenticationProvider oidcAuthorizationCodeAuthenticationProvider = new OidcAuthorizationCodeAuthenticationProvider( |
389 | 390 | accessTokenResponseClient, oidcUserService); |
| 391 | + OidcAuthorizedClientRefreshedEventListener oidcAuthorizedClientRefreshedEventListener = new OidcAuthorizedClientRefreshedEventListener(); |
| 392 | + oidcAuthorizedClientRefreshedEventListener.setUserService(oidcUserService); |
| 393 | + oidcAuthorizedClientRefreshedEventListener |
| 394 | + .setApplicationEventPublisher(http.getSharedObject(ApplicationContext.class)); |
| 395 | + |
390 | 396 | JwtDecoderFactory<ClientRegistration> jwtDecoderFactory = this.getJwtDecoderFactoryBean(); |
391 | 397 | if (jwtDecoderFactory != null) { |
392 | 398 | oidcAuthorizationCodeAuthenticationProvider.setJwtDecoderFactory(jwtDecoderFactory); |
| 399 | + oidcAuthorizedClientRefreshedEventListener.setJwtDecoderFactory(jwtDecoderFactory); |
393 | 400 | } |
394 | 401 | if (userAuthoritiesMapper != null) { |
395 | 402 | oidcAuthorizationCodeAuthenticationProvider.setAuthoritiesMapper(userAuthoritiesMapper); |
| 403 | + oidcAuthorizedClientRefreshedEventListener.setAuthoritiesMapper(userAuthoritiesMapper); |
396 | 404 | } |
397 | | - http.authenticationProvider(this.postProcess(oidcAuthorizationCodeAuthenticationProvider)); |
| 405 | + oidcAuthorizationCodeAuthenticationProvider = this.postProcess(oidcAuthorizationCodeAuthenticationProvider); |
| 406 | + http.authenticationProvider(oidcAuthorizationCodeAuthenticationProvider); |
398 | 407 |
|
399 | | - RefreshOidcIdTokenHandler refreshOidcIdTokenHandler = new RefreshOidcIdTokenHandler(); |
400 | | - if (this.getSecurityContextHolderStrategy() != null) { |
401 | | - refreshOidcIdTokenHandler.setSecurityContextHolderStrategy(this.getSecurityContextHolderStrategy()); |
402 | | - } |
403 | | - if (jwtDecoderFactory != null) { |
404 | | - refreshOidcIdTokenHandler.setJwtDecoderFactory(jwtDecoderFactory); |
405 | | - } |
406 | | - registerDelegateApplicationListener(refreshOidcIdTokenHandler); |
| 408 | + oidcAuthorizedClientRefreshedEventListener = this.postProcess(oidcAuthorizedClientRefreshedEventListener); |
| 409 | + registerDelegateApplicationListener(oidcAuthorizedClientRefreshedEventListener); |
| 410 | + configureOidcUserRefreshedEventListener(http); |
407 | 411 | } |
408 | 412 | else { |
409 | 413 | http.authenticationProvider(new OidcAuthenticationRequestChecker()); |
@@ -631,6 +635,16 @@ private void configureOidcSessionRegistry(B http) { |
631 | 635 | registerDelegateApplicationListener(listener); |
632 | 636 | } |
633 | 637 |
|
| 638 | + private void configureOidcUserRefreshedEventListener(B http) { |
| 639 | + OidcUserRefreshedEventListener oidcUserRefreshedEventListener = new OidcUserRefreshedEventListener(); |
| 640 | + oidcUserRefreshedEventListener.setSecurityContextHolderStrategy(this.getSecurityContextHolderStrategy()); |
| 641 | + SecurityContextRepository securityContextRepository = http.getSharedObject(SecurityContextRepository.class); |
| 642 | + if (securityContextRepository != null) { |
| 643 | + oidcUserRefreshedEventListener.setSecurityContextRepository(securityContextRepository); |
| 644 | + } |
| 645 | + registerDelegateApplicationListener(oidcUserRefreshedEventListener); |
| 646 | + } |
| 647 | + |
634 | 648 | private void registerDelegateApplicationListener(ApplicationListener<?> delegate) { |
635 | 649 | DelegatingApplicationListener delegating = getBeanOrNull( |
636 | 650 | ResolvableType.forType(DelegatingApplicationListener.class)); |
|
0 commit comments