Add Auth0 JWT authentication to a Spring Boot API with protected endpoints
This Quickstart is currently in Beta. We’d love to hear your feedback!
Use AI to integrate Auth0
If you use an AI coding assistant like Claude Code, Cursor, or GitHub Copilot, you can add Auth0 authentication automatically in minutes using agent skills.Install:
Add Auth0 JWT authentication to my Spring Boot API
Your AI assistant will automatically create your Auth0 API, fetch credentials, add the Auth0 Spring Boot API SDK dependency, configure application.yml, and implement a SecurityFilterChain with JWT validation and protected endpoints. Full agent skills documentation →
Prerequisites: Before you begin, ensure you have the following installed:
This quickstart demonstrates how to add Auth0 JWT authentication to a Spring Boot API. You’ll build a secure API with protected endpoints using the Auth0 Spring Boot API SDK.
1
Create a new project
Create a new Spring Boot API project for this quickstart:Using Spring Initializr:
Next up, you need to create a new API on your Auth0 tenant and add the configuration to your project.You can choose to do this automatically by running a CLI command or do it manually via the Dashboard:
CLI
Dashboard
Run the following shell command on your project’s root directory to create an Auth0 API and update your application.yml file:
Identifier: https://my-springboot-api (this becomes your Audience)
Signing Algorithm: RS256
Choose Create
Replace YOUR_AUTH0_DOMAIN in application.yml with your Domain from the Test tab (e.g., your-tenant.auth0.com)
Replace YOUR_AUTH0_API_IDENTIFIER in application.yml with your Identifier. For example https://my-springboot-api.
Your Domain should not include https://. Use only the domain and region. For example: your-tenant.auth0.com.The Audience (API Identifier) is a unique identifier for your API and can be any valid URI. It doesn’t need to be a publicly accessible URL.
4
Configure authentication
Create a security configuration class to enable Auth0 JWT authentication. Create src/main/java/com/example/auth0api/SecurityConfig.java:
Implement fine-grained access control using JWT scopes for enhanced security.1. Define scopes in your Auth0 API:In the Auth0 Dashboard → APIs → Your API → Permissions, add scopes:
Enable DPoP (Demonstration of Proof-of-Possession) for enhanced token security that binds access tokens to cryptographic keys.Configure DPoP support in application.yml:
Problem: API returns 401 even with valid tokens.Solution: Ensure auth0.audience exactly matches your Auth0 API identifier. The audience claim in the token must match this value.
Problem: Application fails to start with configuration errors.Solution: Verify application.yml structure and property names. Ensure the auth0 section contains Domain and Audience values.
Problem: Authentication not working despite correct configuration.Solution: Ensure Auth0AuthenticationFilter is properly integrated with Spring Security chain. The filter must be added before UsernamePasswordAuthenticationFilter.
Problem: JWKS retrieval failures or connection timeouts.Solution: Corporate firewall may be blocking Auth0 endpoints. Whitelist Auth0 domains for HTTPS access:
# Required firewall rules (outbound HTTPS/443)*.auth0.com*.us.auth0.com # For US region tenants*.eu.auth0.com # For EU region tenants*.au.auth0.com # For AU region tenants
Scopes not working in authorization policies
Problem: Scope-based authorization policies always fail.Solution: Ensure your access token includes the required scopes. When requesting a token, specify the scopes:
A complete sample application demonstrating all features is available in the SDK repository.
Playground Application
Includes public and protected endpoints, DPoP support, and comprehensive
examples
Clone and run:
git clone https://github.com/auth0/auth0-auth-java.gitcd auth0-auth-java/auth0-springboot-api-playground# Update src/main/resources/application.yml with your Auth0 configuration# Then run:./mvnw spring-boot:run
Testing with curl:
# Test public endpointcurl http://localhost:8080/api/public# Get access token (replace with your Auth0 credentials)curl -X POST https://YOUR_DOMAIN/oauth/token \ -H "Content-Type: application/json" \ -d '{ "client_id": "YOUR_CLIENT_ID", "client_secret": "YOUR_CLIENT_SECRET", "audience": "https://my-springboot-api", "grant_type": "client_credentials" }'# Test protected endpoint with Bearer tokencurl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ http://localhost:8080/api/private