spring-projects / spring-projects/spring-security
`NimbusJwtDecoder` should remove its setters in favor of its builders
@sjohnr is already working on this.
Since Jun 21, 2023.
- Dominant language
- Java
- Stars
- 9.6k
- Forks
- 6.3k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 52
Description
This is perhaps a little controversial, but I've sometimes wondered about the possibility of converting some of the more commonly used setters to return this rather than void.
Case in point, I recently wanted to be able to do something like this:
@Bean
ReactiveJwtDecoder jwtDecoder(/* ... */) {
return NimbusReactiveJwtDecoder
.withIssuerLocation(...)
.webClient(...)
.jwtValidator(...)
.build();
}
But I was thwarted by the limitations of the JwkSetUriReactiveJwtDecoderBuilder not allowing me to specify a jwtValidator, so I had to resort to an ugly detour via a variable:
@Bean
ReactiveJwtDecoder jwtDecoder(/* ... */) {
var jwtDecoder = NimbusReactiveJwtDecoder
.withIssuerLocation(...)
.webClient(...)
.build();
jwtDecoder.setJwtValidator(...);
return jwtDecoder;
}
For a little while I was considering submitting a PR to add jwtValidator(...) to the builder, but soon realized that this class contains 4 such builders, and I'd have to duplicate the functionality between them all (and then double that for the non-reactive variant.)
An option, which I believe is becoming more and more common, is to enable developers to chain setters by returning this. If the setJwtValidator(...) method I used above returned the NimbusReactiveJwtDecoder instance, I could at the very least get a compromise going:
@Bean
ReactiveJwtDecoder jwtDecoder(/* ... */) {
return NimbusReactiveJwtDecoder
.withIssuerLocation(...)
.webClient(...)
.build()
.setJwtValidator(...);
}
It should be backwards compatible, and as an added benefit, the validation that already exists within setJwtValidator(...) will be applied so we wouldn't have to duplicate that as well.
This is just one example, but it's a common theme perhaps especially in Spring Security.
Thanks for listening to my TED talk! 😄
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.