53
53
import org .springframework .security .oauth2 .client .registration .ClientRegistration ;
54
54
import org .springframework .security .oauth2 .client .registration .ClientRegistrationRepository ;
55
55
import org .springframework .security .oauth2 .client .registration .InMemoryClientRegistrationRepository ;
56
+ import org .springframework .security .oauth2 .client .web .HttpSessionOAuth2AuthorizedClientRepository ;
56
57
import org .springframework .security .oauth2 .client .web .OAuth2AuthorizationRequestRedirectFilter ;
58
+ import org .springframework .security .oauth2 .client .web .OAuth2AuthorizedClientRepository ;
57
59
import org .springframework .security .oauth2 .core .AuthorizationGrantType ;
58
60
import org .springframework .security .oauth2 .core .OAuth2AuthenticationException ;
59
61
import org .springframework .security .oauth2 .core .OAuth2Error ;
71
73
import net .minidev .json .JSONArray ;
72
74
import net .minidev .json .parser .JSONParser ;
73
75
import net .minidev .json .parser .ParseException ;
76
+ import org .springframework .web .context .request .RequestContextHolder ;
77
+ import org .springframework .web .context .request .ServletRequestAttributes ;
74
78
import org .springframework .web .servlet .support .ServletUriComponentsBuilder ;
75
79
76
80
public class OpenIDAuthenticationBackend implements IAuthenticationBackend {
@@ -82,7 +86,7 @@ public class OpenIDAuthenticationBackend implements IAuthenticationBackend {
82
86
83
87
private Logger log = LogManager .getLogger (OpenIDAuthenticationBackend .class );
84
88
85
- private OAuth2AuthorizedClientService authorizedClientService ;
89
+ private OAuth2AuthorizedClientRepository oAuth2AuthorizedClientRepository ;
86
90
87
91
@ Inject
88
92
private Environment environment ;
@@ -100,15 +104,15 @@ public boolean hasAuthorization() {
100
104
@ Override
101
105
public void configureHttpSecurity (HttpSecurity http , AuthorizedUrl anyRequestConfigurer ) throws Exception {
102
106
ClientRegistrationRepository clientRegistrationRepo = createClientRepo ();
103
- authorizedClientService = new InMemoryOAuth2AuthorizedClientService ( clientRegistrationRepo );
104
-
107
+ oAuth2AuthorizedClientRepository = new HttpSessionOAuth2AuthorizedClientRepository ( );
108
+
105
109
anyRequestConfigurer .authenticated ();
106
110
107
111
http
108
112
.oauth2Login ()
109
113
.loginPage ("/login" )
110
114
.clientRegistrationRepository (clientRegistrationRepo )
111
- .authorizedClientService ( authorizedClientService )
115
+ .authorizedClientRepository ( oAuth2AuthorizedClientRepository )
112
116
.authorizationEndpoint ()
113
117
.authorizationRequestResolver (new FixedDefaultOAuth2AuthorizationRequestResolver (clientRegistrationRepo , OAuth2AuthorizationRequestRedirectFilter .DEFAULT_AUTHORIZATION_REQUEST_BASE_URI ))
114
118
.and ()
@@ -151,7 +155,8 @@ public void customizeContainerEnv(List<String> env) {
151
155
if (auth == null ) return ;
152
156
153
157
OidcUser user = (OidcUser ) auth .getPrincipal ();
154
- OAuth2AuthorizedClient client = authorizedClientService .loadAuthorizedClient (REG_ID , user .getName ());
158
+ HttpServletRequest request = ((ServletRequestAttributes ) RequestContextHolder .getRequestAttributes ()).getRequest ();
159
+ OAuth2AuthorizedClient client = oAuth2AuthorizedClientRepository .loadAuthorizedClient (REG_ID , auth , request );
155
160
if (client == null || client .getAccessToken () == null ) return ;
156
161
157
162
env .add (ENV_TOKEN_NAME + "=" + client .getAccessToken ().getTokenValue ());
0 commit comments