Skip to main content

Claude/ChatGPT Prompt to Build Spring Security JWT with Refresh Tokens

Configure Spring Security 6 JWT authentication with access and refresh-token rotation, a logout blacklist, and CORS, returning working config and controllers.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt makes the model configure Spring Security 6 JWT authentication with refresh-token rotation, returning working config and controllers rather than disconnected snippets. It covers the detail most tutorials skip: rotating the refresh token on every use and revoking tokens on logout, so a stolen long-lived refresh token can't quietly keep a hijacked session alive. The filter chain, endpoints, blacklist, and CORS arrive as a connected set you can run, not isolated examples you have to stitch.

The [app_profile] variable sets the app and Spring version so the filter-chain syntax matches. [access_ttl] and [refresh_ttl] define the lifetimes of the access and refresh tokens, which shapes the rotation and expiry logic. [token_store] is where revoked tokens are tracked — the blacklist backing store — and influences how logout revocation and cleanup of expired entries are implemented. Together these produce a stateless filter chain, login/refresh/logout endpoints, role-based method security, and CORS for your frontend origin.

When to use it

  • Building a stateless JWT-authenticated Spring Boot 3 API and wanting rotation done right.
  • Adding a /refresh flow that invalidates the old refresh token on each use.
  • Implementing real logout revocation rather than treating it as optional.
  • Locking down endpoints with role-based @PreAuthorize method security.
  • Configuring CORS correctly for a separate frontend origin.
  • Choosing a blacklist store that can expire revoked entries automatically.

Example output

You get the SecurityConfig, the auth controller, the JWT service, and the blacklist component. The filter chain runs stateless sessions with JWT validation and separates public from protected routes. The controller exposes /login to issue a token pair, /refresh to rotate, and /logout to revoke, with the blacklist backed by [token_store] and cleanup of expired entries. Role-based @PreAuthorize and CORS for the frontend origin complete it. It's a connected set of components built around the [access_ttl] and [refresh_ttl] you specify, not isolated examples, and the pieces reference each other the way they would in a real service.

Pro tips

  • Rotate on every refresh — invalidating the old token each time is what limits the damage from a leaked refresh token.
  • Store only token IDs in the blacklist, never the raw tokens, so the revocation store doesn't become a secret to leak.
  • Set [access_ttl] short and [refresh_ttl] longer; the short access lifetime limits exposure while refresh keeps users logged in.
  • Pick [token_store] with cleanup in mind — something like Redis with TTLs lets expired blacklist entries age out automatically.
  • Match [app_profile] to your exact Spring Security version so the filter-chain configuration uses the current, non-deprecated API.
  • Treat /logout revocation as non-negotiable; without it a token stays valid until natural expiry no matter what the user does.

Frequently Asked Questions

What is refresh-token rotation and why does it matter here?
Rotation means each call to `/refresh` issues a new refresh token and invalidates the old one. This limits the window in which a stolen refresh token is usable, which matters because long-lived refresh tokens are a common way sessions get hijacked when they aren't rotated.
Does logout actually revoke the token?
Yes. The `/logout` endpoint revokes the token via a blacklist backed by `[token_store]`, and the prompt treats this as non-negotiable rather than optional. Without server-side revocation a JWT stays valid until it naturally expires, regardless of the user logging out.
Where are revoked tokens stored?
Revoked tokens are tracked in the store named by `[token_store]`, such as Redis. The recommended approach stores only token IDs rather than raw tokens, and a TTL-based store lets expired blacklist entries clean themselves up automatically over time.
Can I control access and refresh token lifetimes?
Yes. The `[access_ttl]` and `[refresh_ttl]` variables set each token's lifetime. A short access TTL limits exposure if a token leaks, while a longer refresh TTL keeps users signed in, and rotation on each refresh keeps the longer-lived token safer.
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