@@ -430,7 +430,8 @@ This is handy when deeper configuration, like <<oauth2resourceserver-jwt-validat
430
430
[[oauth2resourceserver-jwt-decoder-bean]]
431
431
=== Exposing a `JwtDecoder` `@Bean`
432
432
433
- Or, exposing a <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>> `@Bean` has the same effect as `decoder()`:
433
+ Or, exposing a <<oauth2resourceserver-jwt-architecture-jwtdecoder,`JwtDecoder`>> `@Bean` has the same effect as `decoder()`.
434
+ You can construct one with a `jwkSetUri` like so:
434
435
435
436
====
436
437
.Java
@@ -452,6 +453,50 @@ fun jwtDecoder(): JwtDecoder {
452
453
----
453
454
====
454
455
456
+ or you can use the issuer and have `NimbusJwtDecoder` look up the `jwkSetUri` when `build()` is invoked, like the following:
457
+
458
+ ====
459
+ .Java
460
+ [source,java,role="primary"]
461
+ ----
462
+ @Bean
463
+ public JwtDecoder jwtDecoder() {
464
+ return NimbusJwtDecoder.withIssuerLocation(issuer).build();
465
+ }
466
+ ----
467
+
468
+ .Kotlin
469
+ [source,kotlin,role="secondary"]
470
+ ----
471
+ @Bean
472
+ fun jwtDecoder(): JwtDecoder {
473
+ return NimbusJwtDecoder.withIssuerLocation(issuer).build()
474
+ }
475
+ ----
476
+ ====
477
+
478
+ Or, if the defaults work for you, you can also use `JwtDecoders`, which does the above in addition to configuring the decoder's validator:
479
+
480
+ ====
481
+ .Java
482
+ [source,java,role="primary"]
483
+ ----
484
+ @Bean
485
+ public JwtDecoders jwtDecoder() {
486
+ return JwtDecoders.fromIssuerLocation(issuer);
487
+ }
488
+ ----
489
+
490
+ .Kotlin
491
+ [source,kotlin,role="secondary"]
492
+ ----
493
+ @Bean
494
+ fun jwtDecoder(): JwtDecoders {
495
+ return JwtDecoders.fromIssuerLocation(issuer)
496
+ }
497
+ ----
498
+ ====
499
+
455
500
[[oauth2resourceserver-jwt-decoder-algorithm]]
456
501
== Configuring Trusted Algorithms
457
502
@@ -486,7 +531,7 @@ For greater power, though, we can use a builder that ships with `NimbusJwtDecode
486
531
----
487
532
@Bean
488
533
JwtDecoder jwtDecoder() {
489
- return NimbusJwtDecoder.withJwkSetUri (this.jwkSetUri )
534
+ return NimbusJwtDecoder.withIssuerLocation (this.issuer )
490
535
.jwsAlgorithm(RS512).build();
491
536
}
492
537
----
@@ -496,7 +541,7 @@ JwtDecoder jwtDecoder() {
496
541
----
497
542
@Bean
498
543
fun jwtDecoder(): JwtDecoder {
499
- return NimbusJwtDecoder.withJwkSetUri (this.jwkSetUri )
544
+ return NimbusJwtDecoder.withIssuerLocation (this.issuer )
500
545
.jwsAlgorithm(RS512).build()
501
546
}
502
547
----
@@ -510,7 +555,7 @@ Calling `jwsAlgorithm` more than once will configure `NimbusJwtDecoder` to trust
510
555
----
511
556
@Bean
512
557
JwtDecoder jwtDecoder() {
513
- return NimbusJwtDecoder.withJwkSetUri (this.jwkSetUri )
558
+ return NimbusJwtDecoder.withIssuerLocation (this.issuer )
514
559
.jwsAlgorithm(RS512).jwsAlgorithm(ES512).build();
515
560
}
516
561
----
@@ -520,7 +565,7 @@ JwtDecoder jwtDecoder() {
520
565
----
521
566
@Bean
522
567
fun jwtDecoder(): JwtDecoder {
523
- return NimbusJwtDecoder.withJwkSetUri (this.jwkSetUri )
568
+ return NimbusJwtDecoder.withIssuerLocation (this.issuer )
524
569
.jwsAlgorithm(RS512).jwsAlgorithm(ES512).build()
525
570
}
526
571
----
@@ -534,7 +579,7 @@ Or, you can call `jwsAlgorithms`:
534
579
----
535
580
@Bean
536
581
JwtDecoder jwtDecoder() {
537
- return NimbusJwtDecoder.withJwkSetUri (this.jwkSetUri )
582
+ return NimbusJwtDecoder.withIssuerLocation (this.issuer )
538
583
.jwsAlgorithms(algorithms -> {
539
584
algorithms.add(RS512);
540
585
algorithms.add(ES512);
@@ -547,7 +592,7 @@ JwtDecoder jwtDecoder() {
547
592
----
548
593
@Bean
549
594
fun jwtDecoder(): JwtDecoder {
550
- return NimbusJwtDecoder.withJwkSetUri (this.jwkSetUri )
595
+ return NimbusJwtDecoder.withIssuerLocation (this.issuer )
551
596
.jwsAlgorithms {
552
597
it.add(RS512)
553
598
it.add(ES512)
@@ -1207,7 +1252,7 @@ An individual claim's conversion strategy can be configured using `MappedJwtClai
1207
1252
----
1208
1253
@Bean
1209
1254
JwtDecoder jwtDecoder() {
1210
- NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri ).build();
1255
+ NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(issuer ).build();
1211
1256
1212
1257
MappedJwtClaimSetConverter converter = MappedJwtClaimSetConverter
1213
1258
.withDefaults(Collections.singletonMap("sub", this::lookupUserIdBySub));
@@ -1222,7 +1267,7 @@ JwtDecoder jwtDecoder() {
1222
1267
----
1223
1268
@Bean
1224
1269
fun jwtDecoder(): JwtDecoder {
1225
- val jwtDecoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri ).build()
1270
+ val jwtDecoder = NimbusJwtDecoder.withIssuerLocation(issuer ).build()
1226
1271
1227
1272
val converter = MappedJwtClaimSetConverter
1228
1273
.withDefaults(mapOf("sub" to this::lookupUserIdBySub))
@@ -1319,7 +1364,7 @@ And then, the instance can be supplied like normal:
1319
1364
----
1320
1365
@Bean
1321
1366
JwtDecoder jwtDecoder() {
1322
- NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri ).build();
1367
+ NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(issuer ).build();
1323
1368
jwtDecoder.setClaimSetConverter(new UsernameSubClaimAdapter());
1324
1369
return jwtDecoder;
1325
1370
}
@@ -1330,7 +1375,7 @@ JwtDecoder jwtDecoder() {
1330
1375
----
1331
1376
@Bean
1332
1377
fun jwtDecoder(): JwtDecoder {
1333
- val jwtDecoder: NimbusJwtDecoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri ).build()
1378
+ val jwtDecoder: NimbusJwtDecoder = NimbusJwtDecoder.withIssuerLocation(issuer ).build()
1334
1379
jwtDecoder.setClaimSetConverter(UsernameSubClaimAdapter())
1335
1380
return jwtDecoder
1336
1381
}
@@ -1358,7 +1403,7 @@ public JwtDecoder jwtDecoder(RestTemplateBuilder builder) {
1358
1403
.setReadTimeout(Duration.ofSeconds(60))
1359
1404
.build();
1360
1405
1361
- NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withJwkSetUri(jwkSetUri ).restOperations(rest).build();
1406
+ NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(issuer ).restOperations(rest).build();
1362
1407
return jwtDecoder;
1363
1408
}
1364
1409
----
@@ -1372,7 +1417,7 @@ fun jwtDecoder(builder: RestTemplateBuilder): JwtDecoder {
1372
1417
.setConnectTimeout(Duration.ofSeconds(60))
1373
1418
.setReadTimeout(Duration.ofSeconds(60))
1374
1419
.build()
1375
- return NimbusJwtDecoder.withJwkSetUri(jwkSetUri ).restOperations(rest).build()
1420
+ return NimbusJwtDecoder.withIssuerLocation(issuer ).restOperations(rest).build()
1376
1421
}
1377
1422
----
1378
1423
====
@@ -1388,7 +1433,7 @@ To adjust the way in which Resource Server caches the JWK set, `NimbusJwtDecoder
1388
1433
----
1389
1434
@Bean
1390
1435
public JwtDecoder jwtDecoder(CacheManager cacheManager) {
1391
- return NimbusJwtDecoder.withJwkSetUri(jwkSetUri )
1436
+ return NimbusJwtDecoder.withIssuerLocation(issuer )
1392
1437
.cache(cacheManager.getCache("jwks"))
1393
1438
.build();
1394
1439
}
@@ -1399,7 +1444,7 @@ public JwtDecoder jwtDecoder(CacheManager cacheManager) {
1399
1444
----
1400
1445
@Bean
1401
1446
fun jwtDecoder(cacheManager: CacheManager): JwtDecoder {
1402
- return NimbusJwtDecoder.withJwkSetUri(jwkSetUri )
1447
+ return NimbusJwtDecoder.withIssuerLocation(issuer )
1403
1448
.cache(cacheManager.getCache("jwks"))
1404
1449
.build()
1405
1450
}
0 commit comments