AI Prompt
AI Prompt
Using AI to integrate Auth0? Add this prompt to Cursor, Windsurf, Copilot, Claude Code or your favourite AI-powered IDE to speed up development.
Prerequisites:
- Java Development Kit (JDK) 11 or newer
- Apache Maven 3.x
- A Java EE 8 compatible application server (e.g., WildFly 14+, Payara 5+, or GlassFish 5+)
- An Auth0 account — sign up for free
Get Started
Auth0 allows you to quickly add authentication and access user profile information in your application. This guide demonstrates how to integrate Auth0 with any new or existing Java EE application using theauth0-java-mvc-common SDK and the Java EE 8 Security API.
Create a new project
Generate a new Maven WAR project:Navigate into the project directory:Create the Java source directories:
Install the Auth0 SDK
Replace the contents of your The
pom.xml with the following:pom.xml
javaee-api and javax.security.enterprise-api dependencies are provided because the Java EE 8 application server supplies their implementations at runtime.Setup your Auth0 Application
- Dashboard
- Go to the Auth0 Dashboard and navigate to Applications > Applications > Create Application.
- Enter a name for your application (e.g., “My Java EE App”).
- Select Regular Web Applications as the application type.
- Click Create.
- Open the Settings tab.
- Note the Domain, Client ID, and Client Secret values.
- Scroll down to Application URIs and set:
- Allowed Callback URLs:
http://localhost:8080/callback - Allowed Logout URLs:
http://localhost:8080/
- Allowed Callback URLs:
- Click Save Changes.
Make sure to set up connections for your application so users can log in using their preferred identity provider.
Configure authentication
Update your
web.xml to store Auth0 configuration as JNDI environment entries. Replace the placeholder values with the Domain, Client ID, and Client Secret from your Auth0 application settings. Create a jboss-web.xml to configure the JASPIC security domain required by the Java EE 8 Security API, an Auth0AuthenticationConfig.java CDI bean to read the configuration from JNDI, and an Auth0AuthenticationProvider.java CDI producer to build the AuthenticationController.Do not include
https:// in your auth0.domain value. Use only the domain, e.g., dev-abc123.us.auth0.com.Implement Java EE Security
The Java EE 8 Security API uses
HttpAuthenticationMechanism to handle authentication. You need to provide custom implementations of several security interfaces. The @AutoApplySession annotation enables the container to create a session for the authenticated user, so they remain logged in across requests.Add login and logout functionality
Create the servlets for login, callback, and logout. The
LoginServlet builds the Auth0 authorization URL and redirects the user. The CallbackServlet handles the redirect after authentication — the Auth0AuthenticationMechanism intercepts this request first to exchange the authorization code for tokens, so the servlet only needs to redirect. The LogoutServlet clears the session and redirects to the Auth0 logout endpoint.Create the user interface
Create the servlets and JSP pages for the home and profile views. The
HomeServlet checks for an authenticated principal and sets the profile claims on the request. The ProfileServlet displays the user’s profile and JWT claims, or redirects to login if not authenticated.Run your application
Build and run the application using the WildFly Maven plugin:Your application should start and display the URL it’s listening on:Open your browser to http://localhost:8080. Click the Login link in the navigation bar. You will be redirected to the Auth0 login page. After authenticating, you will be redirected to the profile page where your user information and JWT claims are displayed.
The sample uses JSP and is tested with the WildFly application server. You may need to adjust some steps if you are using a different Java EE 8 compatible container.
CheckpointYou should now have a fully functional Auth0-protected Java EE application running on http://localhost:8080. Users can log in, view their profile, and log out.
Advanced Usage
Access User Profile Information
Access User Profile Information
The Common claims available in the ID token:
Auth0JwtPrincipal is available via request.getUserPrincipal() in any servlet. The ProfileServlet demonstrates how to access the decoded ID token claims:name— user’s full display nameemail— user’s email addresspicture— URL to the user’s profile picturesub— unique identifier for the user (Auth0 user ID)
Customize Login Parameters
Customize Login Parameters
Add custom parameters to the authorization URL when building it in your Use
LoginServlet:src/main/java/com/auth0/example/web/LoginServlet.java
.withAudience() to request an access token for a specific API. Use .withParameter() for any additional authorization parameters supported by Auth0.Store Tokens for API Calls
Store Tokens for API Calls
To access the raw tokens for API calls, modify the Then retrieve the access token when calling a protected API:
Auth0AuthenticationMechanism to store tokens in the session:src/main/java/com/auth0/example/security/Auth0AuthenticationMechanism.java
Log In to an Organization
Log In to an Organization
Configure the The SDK automatically validates the
AuthenticationController with an organization ID or name to restrict login to a specific Auth0 Organization:src/main/java/com/auth0/example/security/Auth0AuthenticationProvider.java
org_id or org_name claim in the ID token to ensure it matches the configured organization.Additional Resources
Auth0 Java MVC SDK
Source code and issue tracker
API Reference (JavaDoc)
Detailed API documentation
Community Forum
Get help from the Auth0 community
Java EE Sample App
Complete sample application on GitHub
Common Issues
State mismatch error on callback
State mismatch error on callback
If you receive an
a0.invalid_state error after login, the state cookie was not found or does not match the state returned from Auth0.Verify that:- Your callback URL in the Auth0 Dashboard exactly matches the URL your application constructs, including the port number and protocol
- Your browser is not blocking third-party cookies
- No reverse proxy or middleware is stripping
Set-Cookieheaders from responses
buildAuthorizeUrl and handle, which uses cookie-based state storage:CDI beans not discovered
CDI beans not discovered
If you see errors about injection failures or beans not being found, ensure that:The
- Your application server supports CDI 2.0 (part of Java EE 8)
- All security classes (
Auth0AuthenticationConfig,Auth0AuthenticationProvider,Auth0JwtIdentityStore,Auth0AuthenticationMechanism) are annotated with@ApplicationScoped src/main/webapp/WEB-INF/jboss-web.xmlexists with thejaspitestsecurity domain configured
src/main/webapp/WEB-INF/jboss-web.xml
jaspitest security domain is required for WildFly to enable the JASPIC (Java Authentication SPI for Containers) integration that the Java EE 8 Security API depends on.Application server compatibility
Application server compatibility
This quickstart uses the
javax namespace (Java EE 8). If you are using a server that has migrated to the jakarta namespace (Jakarta EE 9+), such as WildFly 27+ or Payara 6+, the code will not compile or run.Use a Java EE 8 compatible server:- WildFly 14 through 26
- Payara 5
- GlassFish 5
- Open Liberty with Java EE 8 features
Sample Application
A sample Java EE application integrated with Auth0 is available on GitHub:Java EE Sample Application
Includes login, logout, user profile and other examples.
src/main/webapp/WEB-INF/web.xml, then run: