http://localhost:3000/callback
.returnTo
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.http://localhost:3000/
.javax.javaee-api
dependency, the implementation is provided by the
application container, so is not included in the WAR file.pom.xml
:build.gradle
: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.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: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.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.JwkProvider
.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:handle
method of the AuthenticationController
to exchange the
Authorization Code for an ID token and an access token.Auth0Credential
.validate
method of the custom IdentityStore
implementation to obtain the
validation result./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./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: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: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.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.