Skip to main content

Spring Boot Prompt to Configure Spring Security OAuth2 & RBAC

Configure Spring Security with OAuth2, RBAC, method-level security, and multi-tenant authorization — with integration tests for every rule.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt asks the AI to configure Spring Security for a Spring Boot [spring_version] API, covering OAuth2, RBAC, and multi-tenant authorization with a test for every rule. It sets up an OAuth2 Resource Server validating JWTs from [auth_provider], role-based access control for [roles], permission-based authorization via custom @PreAuthorize expressions, method-level security with @Secured and @RolesAllowed, multi-tenant isolation so users only reach their [tenant_entity] data, API-key auth for service-to-service calls, per-tier rate limiting, CORS for [frontend_origins], security-event logging, and integration tests for every rule using @WithMockUser and MockMvc — including SecurityConfig, a custom UserDetailsService, and a permission evaluator.

The structure works because authorization is where audits and breaches originate, and getting @PreAuthorize, tenant isolation, and the test-per-rule discipline right early prevents painful rework. The prompt treats a test for each security rule as non-negotiable, which is what keeps the rules from silently regressing. Separating service-to-service API-key auth from user OAuth2 is another deliberate choice, since machine callers and human users have different trust models and should not be forced through the same JWT flow.

When to use it

  • You are securing a Spring Boot API with OAuth2 and need JWT validation against [auth_provider].
  • You need role and permission-based access with custom @PreAuthorize expressions, not just URL rules.
  • You require multi-tenant isolation so users cannot reach another tenant's [tenant_entity] data.
  • You have service-to-service calls that need API-key authentication separate from user auth.
  • You want a test for every security rule so authorization changes cannot silently regress.
  • You need CORS configured precisely for known [frontend_origins].

Example output

Expect a SecurityConfig wiring the OAuth2 Resource Server, a custom UserDetailsService, a permission evaluator, @PreAuthorize expressions for your [roles], tenant-scoping logic for [tenant_entity], API-key filter, rate-limiting setup, CORS config for [frontend_origins], security-event logging, and a suite of MockMvc integration tests using @WithMockUser that assert both allowed and denied access for each rule. The security-event logging is wired in alongside, so login attempts and permission denials leave a trail you can audit rather than disappearing silently when a request is rejected.

Pro tips

  • Define [roles] as a real hierarchy so the AI can express role inheritance correctly in the access rules.
  • Match [auth_provider] (for example, Keycloak) so JWT issuer, audience, and key validation are configured for your actual provider.
  • Make tenant scoping on [tenant_entity] explicit at the query level too; method security alone does not stop a crafted ID from reading another tenant's data.
  • List exact [frontend_origins] rather than wildcards, since permissive CORS undermines the rest of the auth setup.
  • Keep the test-per-rule step; assert the denied path, not just the allowed one, because missing negative tests hide broken rules.
  • Iterate by asking for a @PreAuthorize expression for one tricky permission and its matching allow/deny tests.

Frequently Asked Questions

Does it enforce multi-tenant data isolation?
Yes, it scopes access so users only reach their own `[tenant_entity]` data. Method-level security helps, but the prompt should also enforce tenant scoping at the query level, since checking a role alone will not stop a user from requesting another tenant's record by ID.
Which OAuth2 provider does it work with?
Whatever you set in `[auth_provider]`, defaulting to Keycloak. The configuration validates JWTs by issuer, audience, and signing keys, so specifying your real provider ensures the token validation matches the tokens your identity service actually issues.
Does it include tests for the security rules?
Yes, and it treats this as non-negotiable. The prompt generates MockMvc integration tests with `@WithMockUser` for every rule, asserting both allowed and denied access so authorization logic cannot silently break during later changes.
Can it handle service-to-service authentication?
Yes. Alongside user OAuth2, the prompt configures API-key authentication for service-to-service calls. This separates machine clients from human users, so internal callers can authenticate without going through the JWT user flow.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Java & Spring Boot Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support