44
44
import com .nimbusds .jwt .JWTParser ;
45
45
import com .nimbusds .jwt .PlainJWT ;
46
46
import com .nimbusds .jwt .SignedJWT ;
47
+ import com .nimbusds .jwt .proc .ConfigurableJWTProcessor ;
47
48
import com .nimbusds .jwt .proc .DefaultJWTProcessor ;
48
49
import com .nimbusds .jwt .proc .JWTProcessor ;
49
50
import reactor .core .publisher .Flux ;
@@ -245,10 +246,12 @@ public static final class JwkSetUriReactiveJwtDecoderBuilder {
245
246
private final String jwkSetUri ;
246
247
private Set <SignatureAlgorithm > signatureAlgorithms = new HashSet <>();
247
248
private WebClient webClient = WebClient .create ();
249
+ private Consumer <ConfigurableJWTProcessor <JWKSecurityContext >> jwtProcessorCustomizer ;
248
250
249
251
private JwkSetUriReactiveJwtDecoderBuilder (String jwkSetUri ) {
250
252
Assert .hasText (jwkSetUri , "jwkSetUri cannot be empty" );
251
253
this .jwkSetUri = jwkSetUri ;
254
+ this .jwtProcessorCustomizer = (processor ) -> {};
252
255
}
253
256
254
257
/**
@@ -294,6 +297,20 @@ public JwkSetUriReactiveJwtDecoderBuilder webClient(WebClient webClient) {
294
297
return this ;
295
298
}
296
299
300
+ /**
301
+ * Use the given {@link Consumer} to customize the {@link JWTProcessor ConfigurableJWTProcessor} before
302
+ * passing it to the build {@link NimbusReactiveJwtDecoder}.
303
+ *
304
+ * @param jwtProcessorCustomizer the callback used to alter the processor
305
+ * @return a {@link JwkSetUriReactiveJwtDecoderBuilder} for further configurations
306
+ * @since 5.4
307
+ */
308
+ public JwkSetUriReactiveJwtDecoderBuilder jwtProcessorCustomizer (Consumer <ConfigurableJWTProcessor <JWKSecurityContext >> jwtProcessorCustomizer ) {
309
+ Assert .notNull (jwtProcessorCustomizer , "jwtProcessorCustomizer cannot be null" );
310
+ this .jwtProcessorCustomizer = jwtProcessorCustomizer ;
311
+ return this ;
312
+ }
313
+
297
314
/**
298
315
* Build the configured {@link NimbusReactiveJwtDecoder}.
299
316
*
@@ -323,6 +340,8 @@ Converter<JWT, Mono<JWTClaimsSet>> processor() {
323
340
jwtProcessor .setJWSKeySelector (jwsKeySelector );
324
341
jwtProcessor .setJWTClaimsSetVerifier ((claims , context ) -> {});
325
342
343
+ this .jwtProcessorCustomizer .accept (jwtProcessor );
344
+
326
345
ReactiveRemoteJWKSource source = new ReactiveRemoteJWKSource (this .jwkSetUri );
327
346
source .setWebClient (this .webClient );
328
347
@@ -360,11 +379,13 @@ private JWKSelector createSelector(Function<JWSAlgorithm, Boolean> expectedJwsAl
360
379
public static final class PublicKeyReactiveJwtDecoderBuilder {
361
380
private final RSAPublicKey key ;
362
381
private JWSAlgorithm jwsAlgorithm ;
382
+ private Consumer <ConfigurableJWTProcessor <SecurityContext >> jwtProcessorCustomizer ;
363
383
364
384
private PublicKeyReactiveJwtDecoderBuilder (RSAPublicKey key ) {
365
385
Assert .notNull (key , "key cannot be null" );
366
386
this .key = key ;
367
387
this .jwsAlgorithm = JWSAlgorithm .RS256 ;
388
+ this .jwtProcessorCustomizer = (processor ) -> {};
368
389
}
369
390
370
391
/**
@@ -382,6 +403,20 @@ public PublicKeyReactiveJwtDecoderBuilder signatureAlgorithm(SignatureAlgorithm
382
403
return this ;
383
404
}
384
405
406
+ /**
407
+ * Use the given {@link Consumer} to customize the {@link JWTProcessor ConfigurableJWTProcessor} before
408
+ * passing it to the build {@link NimbusReactiveJwtDecoder}.
409
+ *
410
+ * @param jwtProcessorCustomizer the callback used to alter the processor
411
+ * @return a {@link PublicKeyReactiveJwtDecoderBuilder} for further configurations
412
+ * @since 5.4
413
+ */
414
+ public PublicKeyReactiveJwtDecoderBuilder jwtProcessorCustomizer (Consumer <ConfigurableJWTProcessor <SecurityContext >> jwtProcessorCustomizer ) {
415
+ Assert .notNull (jwtProcessorCustomizer , "jwtProcessorCustomizer cannot be null" );
416
+ this .jwtProcessorCustomizer = jwtProcessorCustomizer ;
417
+ return this ;
418
+ }
419
+
385
420
/**
386
421
* Build the configured {@link NimbusReactiveJwtDecoder}.
387
422
*
@@ -406,6 +441,8 @@ Converter<JWT, Mono<JWTClaimsSet>> processor() {
406
441
// Spring Security validates the claim set independent from Nimbus
407
442
jwtProcessor .setJWTClaimsSetVerifier ((claims , context ) -> { });
408
443
444
+ this .jwtProcessorCustomizer .accept (jwtProcessor );
445
+
409
446
return jwt -> Mono .just (createClaimsSet (jwtProcessor , jwt , null ));
410
447
}
411
448
}
@@ -418,10 +455,12 @@ Converter<JWT, Mono<JWTClaimsSet>> processor() {
418
455
public static final class SecretKeyReactiveJwtDecoderBuilder {
419
456
private final SecretKey secretKey ;
420
457
private JWSAlgorithm jwsAlgorithm = JWSAlgorithm .HS256 ;
458
+ private Consumer <ConfigurableJWTProcessor <SecurityContext >> jwtProcessorCustomizer ;
421
459
422
460
private SecretKeyReactiveJwtDecoderBuilder (SecretKey secretKey ) {
423
461
Assert .notNull (secretKey , "secretKey cannot be null" );
424
462
this .secretKey = secretKey ;
463
+ this .jwtProcessorCustomizer = (processor ) -> {};
425
464
}
426
465
427
466
/**
@@ -441,6 +480,20 @@ public SecretKeyReactiveJwtDecoderBuilder macAlgorithm(MacAlgorithm macAlgorithm
441
480
return this ;
442
481
}
443
482
483
+ /**
484
+ * Use the given {@link Consumer} to customize the {@link JWTProcessor ConfigurableJWTProcessor} before
485
+ * passing it to the build {@link NimbusReactiveJwtDecoder}.
486
+ *
487
+ * @param jwtProcessorCustomizer the callback used to alter the processor
488
+ * @return a {@link SecretKeyReactiveJwtDecoderBuilder} for further configurations
489
+ * @since 5.4
490
+ */
491
+ public SecretKeyReactiveJwtDecoderBuilder jwtProcessorCustomizer (Consumer <ConfigurableJWTProcessor <SecurityContext >> jwtProcessorCustomizer ) {
492
+ Assert .notNull (jwtProcessorCustomizer , "jwtProcessorCustomizer cannot be null" );
493
+ this .jwtProcessorCustomizer = jwtProcessorCustomizer ;
494
+ return this ;
495
+ }
496
+
444
497
/**
445
498
* Build the configured {@link NimbusReactiveJwtDecoder}.
446
499
*
@@ -459,6 +512,8 @@ Converter<JWT, Mono<JWTClaimsSet>> processor() {
459
512
// Spring Security validates the claim set independent from Nimbus
460
513
jwtProcessor .setJWTClaimsSetVerifier ((claims , context ) -> { });
461
514
515
+ this .jwtProcessorCustomizer .accept (jwtProcessor );
516
+
462
517
return jwt -> Mono .just (createClaimsSet (jwtProcessor , jwt , null ));
463
518
}
464
519
}
@@ -471,10 +526,12 @@ Converter<JWT, Mono<JWTClaimsSet>> processor() {
471
526
public static final class JwkSourceReactiveJwtDecoderBuilder {
472
527
private final Function <SignedJWT , Flux <JWK >> jwkSource ;
473
528
private JWSAlgorithm jwsAlgorithm = JWSAlgorithm .RS256 ;
529
+ private Consumer <ConfigurableJWTProcessor <JWKSecurityContext >> jwtProcessorCustomizer ;
474
530
475
531
private JwkSourceReactiveJwtDecoderBuilder (Function <SignedJWT , Flux <JWK >> jwkSource ) {
476
532
Assert .notNull (jwkSource , "jwkSource cannot be null" );
477
533
this .jwkSource = jwkSource ;
534
+ this .jwtProcessorCustomizer = (processor ) -> {};
478
535
}
479
536
480
537
/**
@@ -490,6 +547,20 @@ public JwkSourceReactiveJwtDecoderBuilder jwsAlgorithm(JwsAlgorithm jwsAlgorithm
490
547
return this ;
491
548
}
492
549
550
+ /**
551
+ * Use the given {@link Consumer} to customize the {@link JWTProcessor ConfigurableJWTProcessor} before
552
+ * passing it to the build {@link NimbusReactiveJwtDecoder}.
553
+ *
554
+ * @param jwtProcessorCustomizer the callback used to alter the processor
555
+ * @return a {@link JwkSourceReactiveJwtDecoderBuilder} for further configurations
556
+ * @since 5.4
557
+ */
558
+ public JwkSourceReactiveJwtDecoderBuilder jwtProcessorCustomizer (Consumer <ConfigurableJWTProcessor <JWKSecurityContext >> jwtProcessorCustomizer ) {
559
+ Assert .notNull (jwtProcessorCustomizer , "jwtProcessorCustomizer cannot be null" );
560
+ this .jwtProcessorCustomizer = jwtProcessorCustomizer ;
561
+ return this ;
562
+ }
563
+
493
564
/**
494
565
* Build the configured {@link NimbusReactiveJwtDecoder}.
495
566
*
@@ -507,6 +578,8 @@ Converter<JWT, Mono<JWTClaimsSet>> processor() {
507
578
jwtProcessor .setJWSKeySelector (jwsKeySelector );
508
579
jwtProcessor .setJWTClaimsSetVerifier ((claims , context ) -> {});
509
580
581
+ this .jwtProcessorCustomizer .accept (jwtProcessor );
582
+
510
583
return jwt -> {
511
584
if (jwt instanceof SignedJWT ) {
512
585
return this .jwkSource .apply ((SignedJWT ) jwt )
0 commit comments