Prerequisites: Before you begin, ensure you have the following installed:
- Java Development Kit (JDK): Version 8 or higher
- Build Tool: Maven 3.6+ or Gradle 6.0+
- Application Server: Apache Tomcat 9.0+ or any servlet container
- Auth0 Account: Sign up for free if you don’t have one
Get Started
This quickstart demonstrates how to add Auth0 authentication to a Java servlet application. You’ll build a secure web application with login, logout, and user profile features using the Auth0 Java MVC Commons SDK.1
Create a New Java Web Project
Create a new Java web application project for this quickstart.Navigate to your project directory:
- Maven
- Gradle
2
Install the Auth0 Java MVC Commons SDK
Add the Auth0 dependency to your project build file.
- Maven
- Gradle
Add the following dependency to your
pom.xml:3
Setup Your Auth0 Application
Next, you need to create a new application on your Auth0 tenant and add the configuration to your project.Allowed Logout URLs:Allowed Web Origins:
- Head to the Auth0 Dashboard
- Click on Applications > Applications > Create Application
- In the popup, enter a name for your app, select Regular Web Application as the app type and click Create
- Switch to the Settings tab on the Application Details page
- Note down the Domain, Client ID, and Client Secret values from the dashboard
- Finally, on the Settings tab of your Application Details page, configure the following URLs:
- Allowed Callback URLs are a critical security measure to ensure users are safely returned to your application after authentication. Without a matching URL, the login process will fail, and users will be blocked by an Auth0 error page instead of accessing your app.
- Allowed Logout URLs are essential for providing a seamless user experience upon signing out. Without a matching URL, users will not be redirected back to your application after logout and will instead be left on a generic Auth0 page.
- Allowed Web Origins is critical for silent authentication. Without it, users will be logged out when they refresh the page or return to your app later.
4
Configure the Auth0 SDK
Configure your servlet application to use the Auth0 SDK by setting up the web.xml configuration with the Auth0 credentials generated above.Create or update Important: Replace
src/main/webapp/WEB-INF/web.xml and replace the placeholder values with your actual Auth0 application settings:YOUR_AUTH0_DOMAIN, YOUR_AUTH0_CLIENT_ID, and YOUR_AUTH0_CLIENT_SECRET with the actual values from your Auth0 application settings.5
Create Authentication Components and Filter
Create the necessary Java classes to handle authentication flows and protect secured pages.
6
Create User Interface Pages
Create the JSP pages and HTML files for your application.
7
Build and Run Your Application
Now you’re ready to build and run your application.Start Tomcat (or your preferred servlet container):
- Maven
- Gradle
Build the application:Deploy to Tomcat:
CheckpointYou should now have a fully functional Auth0-integrated servlet application running at http://localhost:8080/Test your implementation:
- Navigate to your application URL
- Click “Login with Auth0”
- Complete the Auth0 login process
- You should be redirected to your profile page showing the tokens
- Click “Logout” to clear the session
Advanced Usage
Enhance Your Application
Enhance Your Application
Now that you have basic authentication working, consider these enhancements:
- User Profile Information: Decode the ID token to display user information
- API Calls: Use the access token to call Auth0’s Management API or your own APIs
- Role-Based Access: Implement authorization using Auth0 roles and permissions
- Single Sign-On: Configure SSO across multiple applications
Additional Resources
Additional Resources
- Auth0 Java MVC Commons Documentation - Auth0 Dashboard - Manage your Auth0 applications - Auth0 Servlet Sample Repository - Complete examples - Auth0 Java SDK Documentation - Advanced SDK usage
Troubleshooting
Troubleshooting
Common IssuesAuthentication fails with “Invalid callback URL”
- Verify that the callback URL in your Auth0 application settings matches exactly:
http://localhost:8080/callback
- Check that your
web.xmlconfiguration has the correct Auth0 application values - Ensure the parameter names match exactly:
com.auth0.domain,com.auth0.clientId,com.auth0.clientSecret
- Verify all required dependencies are in your classpath
- Check that your servlet container supports Servlet API 3.0+
- Review server logs for specific error messages
- Ensure your servlet container is configured for session management
- Check that cookies are enabled in your browser
- Verify HTTPS is used in production environments