26
26
import org .springframework .security .config .annotation .web .configurers .ExceptionHandlingConfigurer ;
27
27
import org .springframework .security .crypto .key .CryptoKeySource ;
28
28
import org .springframework .security .oauth2 .jose .jws .NimbusJwsEncoder ;
29
+ import org .springframework .security .oauth2 .jwt .JwtEncoder ;
29
30
import org .springframework .security .oauth2 .server .authorization .InMemoryOAuth2AuthorizationService ;
30
31
import org .springframework .security .oauth2 .server .authorization .OAuth2AuthorizationService ;
31
32
import org .springframework .security .oauth2 .server .authorization .authentication .OAuth2AuthorizationCodeAuthenticationProvider ;
@@ -166,7 +167,7 @@ public void init(B builder) {
166
167
getAuthorizationService (builder ));
167
168
builder .authenticationProvider (postProcess (clientAuthenticationProvider ));
168
169
169
- NimbusJwsEncoder jwtEncoder = new NimbusJwsEncoder ( getKeySource ( builder ) );
170
+ JwtEncoder jwtEncoder = getJwtEncoder ( builder );
170
171
171
172
OAuth2AuthorizationCodeAuthenticationProvider authorizationCodeAuthenticationProvider =
172
173
new OAuth2AuthorizationCodeAuthenticationProvider (
@@ -253,23 +254,29 @@ public void configure(B builder) {
253
254
builder .addFilterAfter (postProcess (tokenRevocationEndpointFilter ), OAuth2TokenEndpointFilter .class );
254
255
}
255
256
257
+ private static void validateProviderSettings (ProviderSettings providerSettings ) {
258
+ if (providerSettings .issuer () != null ) {
259
+ try {
260
+ new URI (providerSettings .issuer ()).toURL ();
261
+ } catch (Exception ex ) {
262
+ throw new IllegalArgumentException ("issuer must be a valid URL" , ex );
263
+ }
264
+ }
265
+ }
266
+
256
267
private static <B extends HttpSecurityBuilder <B >> RegisteredClientRepository getRegisteredClientRepository (B builder ) {
257
268
RegisteredClientRepository registeredClientRepository = builder .getSharedObject (RegisteredClientRepository .class );
258
269
if (registeredClientRepository == null ) {
259
- registeredClientRepository = getRegisteredClientRepositoryBean (builder );
270
+ registeredClientRepository = getBean (builder , RegisteredClientRepository . class );
260
271
builder .setSharedObject (RegisteredClientRepository .class , registeredClientRepository );
261
272
}
262
273
return registeredClientRepository ;
263
274
}
264
275
265
- private static <B extends HttpSecurityBuilder <B >> RegisteredClientRepository getRegisteredClientRepositoryBean (B builder ) {
266
- return builder .getSharedObject (ApplicationContext .class ).getBean (RegisteredClientRepository .class );
267
- }
268
-
269
276
private static <B extends HttpSecurityBuilder <B >> OAuth2AuthorizationService getAuthorizationService (B builder ) {
270
277
OAuth2AuthorizationService authorizationService = builder .getSharedObject (OAuth2AuthorizationService .class );
271
278
if (authorizationService == null ) {
272
- authorizationService = getAuthorizationServiceBean (builder );
279
+ authorizationService = getOptionalBean (builder , OAuth2AuthorizationService . class );
273
280
if (authorizationService == null ) {
274
281
authorizationService = new InMemoryOAuth2AuthorizationService ();
275
282
}
@@ -278,34 +285,28 @@ private static <B extends HttpSecurityBuilder<B>> OAuth2AuthorizationService get
278
285
return authorizationService ;
279
286
}
280
287
281
- private static <B extends HttpSecurityBuilder <B >> OAuth2AuthorizationService getAuthorizationServiceBean (B builder ) {
282
- Map <String , OAuth2AuthorizationService > authorizationServiceMap = BeanFactoryUtils .beansOfTypeIncludingAncestors (
283
- builder .getSharedObject (ApplicationContext .class ), OAuth2AuthorizationService .class );
284
- if (authorizationServiceMap .size () > 1 ) {
285
- throw new NoUniqueBeanDefinitionException (OAuth2AuthorizationService .class , authorizationServiceMap .size (),
286
- "Expected single matching bean of type '" + OAuth2AuthorizationService .class .getName () + "' but found " +
287
- authorizationServiceMap .size () + ": " + StringUtils .collectionToCommaDelimitedString (authorizationServiceMap .keySet ()));
288
+ private static <B extends HttpSecurityBuilder <B >> JwtEncoder getJwtEncoder (B builder ) {
289
+ JwtEncoder jwtEncoder = getOptionalBean (builder , JwtEncoder .class );
290
+ if (jwtEncoder == null ) {
291
+ CryptoKeySource keySource = getKeySource (builder );
292
+ jwtEncoder = new NimbusJwsEncoder (keySource );
288
293
}
289
- return (! authorizationServiceMap . isEmpty () ? authorizationServiceMap . values (). iterator (). next () : null ) ;
294
+ return jwtEncoder ;
290
295
}
291
296
292
297
private static <B extends HttpSecurityBuilder <B >> CryptoKeySource getKeySource (B builder ) {
293
298
CryptoKeySource keySource = builder .getSharedObject (CryptoKeySource .class );
294
299
if (keySource == null ) {
295
- keySource = getKeySourceBean (builder );
300
+ keySource = getBean (builder , CryptoKeySource . class );
296
301
builder .setSharedObject (CryptoKeySource .class , keySource );
297
302
}
298
303
return keySource ;
299
304
}
300
305
301
- private static <B extends HttpSecurityBuilder <B >> CryptoKeySource getKeySourceBean (B builder ) {
302
- return builder .getSharedObject (ApplicationContext .class ).getBean (CryptoKeySource .class );
303
- }
304
-
305
306
private static <B extends HttpSecurityBuilder <B >> ProviderSettings getProviderSettings (B builder ) {
306
307
ProviderSettings providerSettings = builder .getSharedObject (ProviderSettings .class );
307
308
if (providerSettings == null ) {
308
- providerSettings = getProviderSettingsBean (builder );
309
+ providerSettings = getOptionalBean (builder , ProviderSettings . class );
309
310
if (providerSettings == null ) {
310
311
providerSettings = new ProviderSettings ();
311
312
}
@@ -314,24 +315,18 @@ private static <B extends HttpSecurityBuilder<B>> ProviderSettings getProviderSe
314
315
return providerSettings ;
315
316
}
316
317
317
- private static <B extends HttpSecurityBuilder <B >> ProviderSettings getProviderSettingsBean (B builder ) {
318
- Map <String , ProviderSettings > providerSettingsMap = BeanFactoryUtils .beansOfTypeIncludingAncestors (
319
- builder .getSharedObject (ApplicationContext .class ), ProviderSettings .class );
320
- if (providerSettingsMap .size () > 1 ) {
321
- throw new NoUniqueBeanDefinitionException (ProviderSettings .class , providerSettingsMap .size (),
322
- "Expected single matching bean of type '" + ProviderSettings .class .getName () + "' but found " +
323
- providerSettingsMap .size () + ": " + StringUtils .collectionToCommaDelimitedString (providerSettingsMap .keySet ()));
324
- }
325
- return (!providerSettingsMap .isEmpty () ? providerSettingsMap .values ().iterator ().next () : null );
318
+ private static <B extends HttpSecurityBuilder <B >, T > T getBean (B builder , Class <T > type ) {
319
+ return builder .getSharedObject (ApplicationContext .class ).getBean (type );
326
320
}
327
321
328
- private void validateProviderSettings ( ProviderSettings providerSettings ) {
329
- if ( providerSettings . issuer () != null ) {
330
- try {
331
- new URI ( providerSettings . issuer ()). toURL ();
332
- } catch ( Exception ex ) {
333
- throw new IllegalArgumentException ( "issuer must be a valid URL" , ex );
334
- }
322
+ private static < B extends HttpSecurityBuilder < B >, T > T getOptionalBean ( B builder , Class < T > type ) {
323
+ Map < String , T > beansMap = BeanFactoryUtils . beansOfTypeIncludingAncestors (
324
+ builder . getSharedObject ( ApplicationContext . class ), type );
325
+ if ( beansMap . size () > 1 ) {
326
+ throw new NoUniqueBeanDefinitionException ( type , beansMap . size (),
327
+ "Expected single matching bean of type '" + type . getName () + "' but found " +
328
+ beansMap . size () + ": " + StringUtils . collectionToCommaDelimitedString ( beansMap . keySet ()));
335
329
}
330
+ return (!beansMap .isEmpty () ? beansMap .values ().iterator ().next () : null );
336
331
}
337
332
}
0 commit comments