http://localhost:3000/callback
.http://localhost:3000/
.build.gradle
:pom.xml
:src/main/webapp/WEB-INF/web.xml
, but you could store
them anywhere else.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 library’s documentation.domain
,
clientId
and clientSecret
attributes will be populated for you. You should verify that
the values are correct, especially if you have multiple Auth0 applications in your account.home.jsp
which will display the tokens associated with the
user after a successful login and provide the option to logout.The project contains a WebFilter: the Auth0Filter.java
which will check for existing tokens before
giving the user access to our protected /portal/*
path. If the tokens don’t exist, the request will
be redirected to the LoginServlet
.The project contains also four servlets:LoginServlet.java
: Invoked when the user attempts to log in. The servlet uses the
client_id
and domain
parameters to create a valid Authorize URL and redirects the
user there.CallbackServlet.java
: The servlet captures requests to our Callback URL and processes the data
to obtain the credentials. After a successful login, the credentials are then saved to the request’s
HttpSession.HomeServlet.java
: The servlet reads the previously saved tokens and shows them on the
home.jsp
resource.LogoutServlet.java
: Invoked when the user clicks the logout link. The servlet invalidates the
user session and redirects the user to the login page, handled by the LoginServlet
.AuthenticationControllerProvider.java
: Responsible to create and manage a single instance of the
AuthenticationController
AuthenticationController
provided by the
auth0-java-mvc-commons
SDK using the domain
, clientId
, and
clientSecret
. The sample shows how to configure the component for use with tokens signed using the
RS256 asymmetric signing algorithm, by specifying 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 you
are using HS256, there is no need to configure the JwkProvider
.AuthenticationController
does not store any context, and is inteded to be reused.
Unneccessary creation may result in additonal resources being created which could impact performance.AuthenticationController
instance, you can generate the redirect URL by calling the
buildAuthorizeUrl(HttpServletRequest request
, HttpServletResponse response
,
String redirectUrl)
method. The redirect URL must be the URL that was added to the Allowed
Callback URLs of your Auth0 application.CallbackServlet
via either a GET or POST
HTTP request. Because we are using the Authorization Code Flow (the default), a GET request will be sent. If you
have configured the library for the Implicit Flow, a POST request will be sent instead.The request holds the call context that the library previously set by generating the Authorize URL with the
AuthenticationController
. When passed to the controller, you get back either a valid
Tokens
instance or an Exception indicating what went wrong. In the case of a successful call, you
need to save the credentials somewhere to access them later. You can use the HttpSession
of the
request by using the SessionsUtils
class included in the library.expiresIn
value, so that the next time when we are going to use the token we can check if
it has already expired or if it’s still valid. For the sake of this sample, we will skip that
validation.Auth0Filter
will allow them to access
our protected resources. In the HomeServlet
we obtain the tokens from the request’s session and set
them as the userId
attribute so they can be used from the JSP code.LogoutServlet
of our sample application.First, we clear the session by calling request.getSession().invalidate()
. We 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, we redirect the response to our logout URL.http://localhost:3000/
. Try to access the
protected resource http://localhost:3000/portal/home and note how you’re redirected by the
Auth0Filter
to the Auth0 Login Page. The widget displays all the social and database connections that
you have defined for this application in the dashboard.