Skip to content

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

Open
JFCote wants to merge 9 commits into
masterfrom
features/add-pre-authorize-in-controller
Open

[spring-boot]: generate PreAuthorize from OpenAPI security scopes#24380
JFCote wants to merge 9 commits into
masterfrom
features/add-pre-authorize-in-controller

Conversation

@JFCote

@JFCote JFCote commented Jul 21, 2026

Copy link
Copy Markdown
Member

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).

Written for commit 34d4b4c. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed against the latest diff

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread samples/server/others/springboot-pre-authorize/README.md Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 11 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread samples/server/others/springboot-pre-authorize/pom.xml Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 7 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

@JFCote

JFCote commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

Let me know if there is anything else to fix and I will take care of it.

Comment thread bin/configs/spring-boot-pre-authorize.yaml
@JFCote

JFCote commented Jul 27, 2026

Copy link
Copy Markdown
Member Author

@wing328 I added the requested lines. Let me know if something else is missing.

JFCote added 6 commits July 27, 2026 15:18
Add opt-in Spring Boot support to translate OAuth2/OIDC security requirements
into @PreAuthorize expressions, preserving OpenAPI AND/OR semantics.
Also:
enable method security with @EnableMethodSecurity
add Spring Security dependency when enabled
support configurable authority prefix (SCOPE_ by default)
add documentation, tests, and a generated sample
@JFCote
JFCote force-pushed the features/add-pre-authorize-in-controller branch from 9deb48a to 8c4aeb3 Compare July 27, 2026 19:18
@wing328

wing328 commented Jul 28, 2026

Copy link
Copy Markdown
Member

updated samples via cc7aa3e

@wing328

wing328 commented Jul 28, 2026

Copy link
Copy Markdown
Member

@JFCote did you have a chance to review the feedback from cubic-dev-ai? are those valid?

@wing328

wing328 commented Jul 28, 2026

Copy link
Copy Markdown
Member

circleci failure ignored as it's not related to this PR.

@JFCote

JFCote commented Jul 31, 2026

Copy link
Copy Markdown
Member Author

@wing328 I did another pass. Let's see how it goes. I'm leaving on vacation but will check this when I come back.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 42 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Comment thread JavaSpring/libraries/spring-boot/openapi2SpringBoot.mustache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants