15
15
*/
16
16
package org .springframework .security .config .annotation .web .configurers .oauth2 .client ;
17
17
18
+ import org .springframework .context .ApplicationContext ;
18
19
import org .springframework .security .config .annotation .web .HttpSecurityBuilder ;
19
20
import org .springframework .security .config .annotation .web .configurers .AbstractAuthenticationFilterConfigurer ;
20
21
import org .springframework .security .core .authority .mapping .GrantedAuthoritiesMapper ;
22
+ import org .springframework .security .jose .jws .JwsAlgorithm ;
21
23
import org .springframework .security .jwt .JwtDecoder ;
22
24
import org .springframework .security .jwt .nimbus .NimbusJwtDecoderJwkSupport ;
23
25
import org .springframework .security .oauth2 .client .authentication .AuthorizationCodeAuthenticationProcessingFilter ;
31
33
import org .springframework .security .oauth2 .client .registration .ClientRegistrationRepository ;
32
34
import org .springframework .security .oauth2 .client .user .OAuth2UserService ;
33
35
import org .springframework .security .oauth2 .client .user .nimbus .NimbusOAuth2UserService ;
36
+ import org .springframework .security .oauth2 .core .http .HttpClientConfig ;
34
37
import org .springframework .security .oauth2 .core .provider .DefaultProviderMetadata ;
35
38
import org .springframework .security .oauth2 .core .provider .ProviderMetadata ;
36
39
import org .springframework .security .oauth2 .core .user .OAuth2User ;
@@ -113,7 +116,7 @@ String getLoginFailureUrl() {
113
116
@ Override
114
117
public void init (H http ) throws Exception {
115
118
AuthorizationCodeAuthenticationProvider authenticationProvider = new AuthorizationCodeAuthenticationProvider (
116
- this .getAuthorizationCodeTokenExchanger (), this .getProviderJwtDecoderRegistry (), this .getUserInfoService ());
119
+ this .getAuthorizationCodeTokenExchanger (http ), this .getProviderJwtDecoderRegistry (http ), this .getUserInfoService (http ));
117
120
if (this .userAuthoritiesMapper != null ) {
118
121
authenticationProvider .setAuthoritiesMapper (this .userAuthoritiesMapper );
119
122
}
@@ -134,14 +137,20 @@ protected RequestMatcher createLoginProcessingUrlMatcher(String loginProcessingU
134
137
return this .getAuthenticationFilter ().getAuthorizeRequestMatcher ();
135
138
}
136
139
137
- private AuthorizationGrantTokenExchanger <AuthorizationCodeAuthenticationToken > getAuthorizationCodeTokenExchanger () {
140
+ private AuthorizationGrantTokenExchanger <AuthorizationCodeAuthenticationToken > getAuthorizationCodeTokenExchanger (H http ) {
138
141
if (this .authorizationCodeTokenExchanger == null ) {
139
- this .authorizationCodeTokenExchanger = new NimbusAuthorizationCodeTokenExchanger ();
142
+ NimbusAuthorizationCodeTokenExchanger nimbusAuthorizationCodeTokenExchanger = new NimbusAuthorizationCodeTokenExchanger ();
143
+ HttpClientConfig httpClientConfig = this .getHttpClientConfig (http );
144
+ if (httpClientConfig != null ) {
145
+ nimbusAuthorizationCodeTokenExchanger .setHttpClientConfig (httpClientConfig );
146
+ }
147
+ this .authorizationCodeTokenExchanger = nimbusAuthorizationCodeTokenExchanger ;
140
148
}
141
149
return this .authorizationCodeTokenExchanger ;
142
150
}
143
151
144
- private ProviderJwtDecoderRegistry getProviderJwtDecoderRegistry () {
152
+ private ProviderJwtDecoderRegistry getProviderJwtDecoderRegistry (H http ) {
153
+ HttpClientConfig httpClientConfig = this .getHttpClientConfig (http );
145
154
Map <ProviderMetadata , JwtDecoder > jwtDecoders = new HashMap <>();
146
155
ClientRegistrationRepository clientRegistrationRepository = OAuth2LoginConfigurer .getClientRegistrationRepository (this .getBuilder ());
147
156
clientRegistrationRepository .getRegistrations ().stream ().forEach (registration -> {
@@ -159,25 +168,38 @@ private ProviderJwtDecoderRegistry getProviderJwtDecoderRegistry() {
159
168
providerMetadata .setTokenEndpoint (this .toURL (providerDetails .getTokenUri ()));
160
169
providerMetadata .setUserInfoEndpoint (this .toURL (providerDetails .getUserInfoUri ()));
161
170
providerMetadata .setJwkSetUri (this .toURL (providerDetails .getJwkSetUri ()));
162
- jwtDecoders .put (providerMetadata , new NimbusJwtDecoderJwkSupport (providerDetails .getJwkSetUri ()));
171
+ NimbusJwtDecoderJwkSupport nimbusJwtDecoderJwkSupport = new NimbusJwtDecoderJwkSupport (
172
+ providerDetails .getJwkSetUri (), JwsAlgorithm .RS256 , httpClientConfig );
173
+ jwtDecoders .put (providerMetadata , nimbusJwtDecoderJwkSupport );
163
174
}
164
175
});
165
176
return new DefaultProviderJwtDecoderRegistry (jwtDecoders );
166
177
}
167
178
168
- private OAuth2UserService getUserInfoService () {
179
+ private OAuth2UserService getUserInfoService (H http ) {
169
180
if (this .userInfoService == null ) {
170
- this . userInfoService = new NimbusOAuth2UserService ();
181
+ NimbusOAuth2UserService nimbusOAuth2UserService = new NimbusOAuth2UserService ();
171
182
if (!this .customUserTypes .isEmpty ()) {
172
- (( NimbusOAuth2UserService ) this . userInfoService ) .setCustomUserTypes (this .customUserTypes );
183
+ nimbusOAuth2UserService .setCustomUserTypes (this .customUserTypes );
173
184
}
174
185
if (!this .userNameAttributeNames .isEmpty ()) {
175
- ((NimbusOAuth2UserService )this .userInfoService ).setUserNameAttributeNames (this .userNameAttributeNames );
186
+ nimbusOAuth2UserService .setUserNameAttributeNames (this .userNameAttributeNames );
187
+ }
188
+ HttpClientConfig httpClientConfig = this .getHttpClientConfig (http );
189
+ if (httpClientConfig != null ) {
190
+ nimbusOAuth2UserService .setHttpClientConfig (httpClientConfig );
176
191
}
192
+ this .userInfoService = nimbusOAuth2UserService ;
177
193
}
178
194
return this .userInfoService ;
179
195
}
180
196
197
+ private HttpClientConfig getHttpClientConfig (H http ) {
198
+ Map <String , HttpClientConfig > httpClientConfigs =
199
+ http .getSharedObject (ApplicationContext .class ).getBeansOfType (HttpClientConfig .class );
200
+ return (!httpClientConfigs .isEmpty () ? httpClientConfigs .values ().iterator ().next () : null );
201
+ }
202
+
181
203
private URL toURL (String urlStr ) {
182
204
if (!StringUtils .hasText (urlStr )) {
183
205
return null ;
0 commit comments