[spring-boot]: generate PreAuthorize from OpenAPI security scopes

Merged
JF
2mo ago
+908-39

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:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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 @PreAuthorize with hasAuthority() per scope; OR across requirements, AND within an entry; empty scopes → isAuthenticated(); escapes quotes in scope names.
    • Configurable authority prefix via springSecurityAuthorityPrefix (default SCOPE_).
    • Uses operation-level security, falling back to top-level security when missing.
    • Auto-adds @EnableMethodSecurity, @PreAuthorize import, and spring-boot-starter-security in the generated pom; docs and tests added; sample at samples/server/others/springboot-pre-authorize with config bin/configs/spring-boot-pre-authorize.yaml (built in CI).