[spring-boot]: generate PreAuthorize from OpenAPI security scopes
Merged
JF
2mo ago+908+908+012345678901234567890123456789-39-39-01234567890123456789
Add opt-in Spring Boot support to translate OAuth2/OIDC security requirements
into @PreAuthorize expressions, preserving OpenAPI AND/OR semantics.
@cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08) @KannaKim (2026/07)
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/configs/java*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
Summary by cubic
Adds opt-in generation of Spring Security @PreAuthorize annotations from OpenAPI OAuth2/OIDC scopes for Spring Boot. Preserves OpenAPI AND/OR semantics, enables method security, and includes a CI-built sample.
- New Features
- Option:
useSpringSecurityPreAuthorize(Spring Boot only; errors if used with other libraries). - Builds
@PreAuthorizewithhasAuthority()per scope; OR across requirements, AND within an entry; empty scopes →isAuthenticated(); escapes quotes in scope names. - Configurable authority prefix via
springSecurityAuthorityPrefix(defaultSCOPE_). - Uses operation-level
security, falling back to top-levelsecuritywhen missing. - Auto-adds
@EnableMethodSecurity,@PreAuthorizeimport, andspring-boot-starter-securityin the generatedpom; docs and tests added; sample atsamples/server/others/springboot-pre-authorizewith configbin/configs/spring-boot-pre-authorize.yaml(built in CI).
- Option: