This tutorial demonstrates how to add user login to a Java EE web application. We recommend that you log in to
follow this quickstart with examples configured for your account.
You need the following information:If you are using Gradle, add them to your Your Java EE application needs some information in order to authenticate users with your Auth0 application. The
deployment descriptor
1
System Requirements
This tutorial and sample project have been tested with the following:
- Java 11
2
Configure Auth0
Get Your Application Keys
When you signed up for Auth0, a new application was created for you, or you could have created a new one. You will need some details about that application to communicate with Auth0. You can get these details from the Application Settings section in the Auth0 dashboard.
- Domain
- Client ID
- Client Secret
If you download the sample from the top of this page, these details are filled out for you.
Configure Callback URLs
A callback URL is a URL in your application where Auth0 redirects the user after they have authenticated. The callback URL for your app must be added to the Allowed Callback URLs field in your Application Settings. If this field is not set, users will be unable to log in to the application and will get an error.If you are following along with the sample project you downloaded from the top of this page, the
callback URL you need to add to the Allowed Callback URLs field is
http://localhost:3000/callback
.Configure Logout URLs
A logout URL is a URL in your application that Auth0 can return to after the user has been logged out of the authorization server. This is specified in thereturnTo
query parameter. The logout URL for your app
must be added to the Allowed Logout URLs field in your Application Settings. If this field is not set, users will be
unable to log out from the application and will get an error.If you are following along with the sample project you downloaded from the top of this page, the logout
URL you need to add to the Allowed Logout URLs field is
http://localhost:3000/
.3
Configure Java EE to use Auth0
Set up dependencies
To integrate your Java EE application with Auth0, add the following dependencies:- javax.javaee-api: The Java EE 8 API necessary to write applications using Java EE 8. The actual implementation is provided by the application container, so it does not need to be included in the WAR file.
- javax.security.enterprise: The Java EE 8 Security API that enables handling security concerns in an EE
application. Like the
javax.javaee-api
dependency, the implementation is provided by the application container, so is not included in the WAR file. - auth0-java-mvc-commons: The Auth0 Java MVC SDK allows you to use Auth0 with Java for server-side MVC web applications. It generates the Authorize URL that your application needs to call in order to authenticate a user using Auth0.
pom.xml
:build.gradle
:Configure your Java EE application
The sample that accompanies this tutorial is written using JSP and tested with the WildFly application server.
You may need to adjust some of the steps if you working with a different application container or
technologies.
web.xml
file can be used to store this information, though you could store them
in a different secured location.This information will be used to configure the auth0-java-mvc-commons library to enable users to login to
your application. To learn more about the library, including its various configuration options, see the README of the library.4
Configure Java EE Security
The Java EE 8 Security API introduced the You can now implement a custom You now have defined the classes that represent a calling principal and credential. Next, create a custom
implementation of If the The sample below shows how to configure the Finally, implement a custom The class overrides the
HttpAuthenticationMechanism
interface to enable
applications to obtain a user’s credentials. Default implementations exist for Basic and form-based
authentication, and it provides an easy way to configure a custom authentication strategy.To authenticate with Auth0, provide custom implementations of the following interfaces:- HttpAuthenticationMechanism: Responsible for handling the authentication workflow for users returning from Auth0. (JavaDoc).
- IdentityStore: Responsible for validating the user’s credentials (JavaDoc).
- CallerPrincipal: Represents the caller principal of the current HTTP request (JavaDoc).
- Credential: Represents the credential the caller will use to authenticate (JavaDoc).
CallerPrincipal
that represents the caller of the current request:Credential
that will be used to represent the user’s credentials. It
will hold information about the principal:IdentityStore
. This class will be responsible for validating the user’s
credentials:credential
is an Auth0Credential
, the calling user is authenticated and valid,
so a CredentialValidationResult
created with the credential is returned to indicate success. If it is
not an Auth0Credential
, return CredentialValidationResult.NOT_VALIDATED_RESULT
.Before implementing the HttpAuthenticationMechanism
interface that will use all these collaborators,
create a bean that will provide a configured instance of the AuthenticationController
from the Auth0
Java MVC SDK. The AuthenticationController
is used to build the authorization URL where users will
login, and handle the token exchange to authenticate users.- If your Auth0 Application is configured to use the **RS256 signing algorithm **(the default when creating
a new Auth0 Application), you need to configure a
JwkProvider
to fetch the public key used to verify the token’s signature. See the jwks-rsa-java repository to learn about additional configuration options. - If your Auth0 Application is configured to use the HS256 signing algorithm, there is no need to
configure the
JwkProvider
.
To learn more about the available signing algorithms, refer to the documentation.
AuthenticationController
for use with the RS256
signing algorithm:HttpAuthenticationMechanism
“validateRequest
method, which is called on every request to our application,
and is responsible for notifying the container of the authentication status.This sample uses the Authorization Code
Flow to exchange an Authorization Code for a token during the authentication flow. If this request is to the
/callback
endpoint and contains the code
request parameter, it does a few important
things:- Calls the
handle
method of theAuthenticationController
to exchange the Authorization Code for an ID token and an access token. - Uses the ID token to create a new
Auth0Credential
. - Calls the
validate
method of the customIdentityStore
implementation to obtain the validation result. - Notifies the application container of the login status.
/callback
, return httpMessageContext.doNothing()
to
allow the request processing to continue. You will see shortly how to use the authentication information when
triggering authentication and displaying web views.Finally, note that the @AutoApplySession
annotation has been added to allow the container to create
a session for the authenticated user.5
Trigger authentication
To enable a user to log in, create a Servlet that will handle requests to the
/login
path.The LoginController
is responsible for redirecting the request to the proper authorization URL,
where the user can authenticate with Auth0. It uses the AuthenticationController
from the Auth0 Java
MVC SDK to build the correct authorization URL, using the configuration values injected via
Auth0AuthenticationConfig
. By default, this sample requests the "openid profile email"
scopes, to allow the application to retrieve basic profile information from the authenticated user. You can read
more about these scopes in the OpenID
Connect Scopes documentation.Once the user has entered their credentials and authorized the requested permissions, Auth0 will issue a request
to the callbackUrl
, and include a code
query parameter which can be exchanged for an ID
token and an access token. Recall that the Auth0HttpAuthenticationMechanism
created above handles
this exchange so that it can notify the application container of authentication status. This allows the Servlet
that handles requests to the /callback
path to simply forward the request on to the originally
requested resource prior to logging in, or simply redirect to the home page:6
Display user information
You can use the
Auth0JwtPrincipal
to get profile information for the authenticated user. The
HomeServlet.java
code sample demonstrates how to use the claims on the ID token to set profile data as a request
attribute.You can then use that profile information in your view to display information about the user:7
Handle logout
To log a user out, you should clear the application session and log the user out of Auth0. This is handled in the
LogoutServlet
.First, clear the session by calling request.getSession().invalidate()
. Then construct the logout
URL, being sure to include the returnTo
query parameter, which is where the user will be redirected
to after logging out. Finally, redirect the response to the application’s logout URL.8
Run the sample
To build and run the sample, execute the wildfly:run Maven goal to start an embedded WildFly application server
with this application deployed to it. See the WildFly Maven Plugin documentation for more information.If you are using Linux or MacOS:Windows:Point your browser to
http:``//localhost:3000.
Follow the Log In link to log in or
sign up to your Auth0 tenant.Upon successful login, you will see the user’s profile picture and a drop-down menu where the Log In link was.
You can then view the user’s profile page by clicking the Profile link. You can log out by clicking the
Logout link in the drop-down menu.Next Steps
Excellent work! If you made it this far, you should now have login, logout, and user profile information running in your application.This concludes our quickstart tutorial, but there is so much more to explore. To learn more about what you can do with Auth0, check out:- Auth0 Dashboard - Learn how to configure and manage your Auth0 tenant and applications
- auth0-java-mvc-common SDK - Explore the SDK used in this tutorial more fully
- Auth0 Marketplace - Discover integrations you can enable to extend Auth0’s functionality