This tutorial uses Spring MVC. If you are using Spring WebFlux, the steps to add authentication are
similar, but some implementation details are different. Refer to the Spring Boot WebFlux Sample Code to see how to integrate
Auth0 with your Spring Boot WebFlux application.
1
Configure Auth0
To use Auth0 services, you’ll need to have an application set up in the Auth0 Dashboard. The Auth0 application is
where you will configure how you want authentication to work for the project you are developing.
Configure an application
Use the interactive selector to create a new Auth0 application or select an existing application that represents the project you want to integrate with. Every application in Auth0 is assigned an alphanumeric, unique client ID that your application code will use to call Auth0 APIs through the SDK.Any settings you configure using this quickstart will automatically update for your Application in the Dashboard, which is where you can manage your Applications in the future.If you would rather explore a complete configuration, you can view a sample application instead.Configure Callback URLs
A callback URL is a URL in your application that you would like Auth0 to redirect users to after they have authenticated. If not set, users will not be returned to your application after they log in.If you are following along with our sample project, set this to
http://localhost:3000/login/oauth2/code/okta
.Configure Logout URLs
A logout URL is a URL in your application that you would like Auth0 to redirect users to after they have logged out. If not set, users will not be able to log out from your application and will receive an error.If you are following along with our sample project, set this to
http://localhost:3000
.2
Configure Sprint Boot application
Add Spring dependencies
To integrate your Spring Boot application with Auth0, include the Okta Spring Boot Starter in your application’s dependencies.This guide uses Thymeleaf and the Spring Security integration module for the view layer. If you are using
a different view technology, the Spring Security configuration and components remain the same.
3
Configure Spring Security
The Okta Spring Boot Starter makes it easy to configure your application with Auth0. The sample below uses an
application.yml
file, though you can also use properties files or any of the other supported externalization mechanisms.4
Add login to your application
To enable user login with Auth0, create a class that will register a SecurityFilterChain, and add the
@Configuration
annotation.You can configure the HttpSecurity instance to require authentication on all
or certain paths. For example, to require authentication on all paths except the home page:
5
Add front page
The Okta Spring Boot Starter will use the client configuration you defined earlier to handle login when a user
visits the
/oauth2/authorization/okta
path of your application. You can use this to create a login
link in your application.This page returns the user attributes when the user authentications. You will use the /logout
link
in the template to implement the logout feature.6
Add controller
Create a controller to handle the incoming request. This controller renders the
index.html
page.
When the user authenticates, the application retrieves the users’s profile information attributes to render the
page.Checkpoint
When you click the login link, verify the application redirects you to the Auth0 Universal Login page and that you can now log in or sign up using a username and password or a social provider.Auth0 enables the Google social provider by default on new tenants and offers you developer keys to
test logging in with social identity providers. However, these developer keys have some limitations
that may cause your application to behave differently. For more details on what this behavior may look
like and how to fix it, consult the Test Social Connections with Auth0 Developer Keys document.
7
Add logout to your application
Now that users can log into your application, they need a way to log out. By default, when
logout is enabled, Spring Security will log the user out of your application and clear the session. To enable
successful logout of Auth0, you can provide a
LogoutHandler
to redirect users to your Auth0 logout endpoint
(https://{yourDomain}/v2/logout
) and then immediately redirect them to your application.In the SecurityConfig
class, provide a LogoutHandler
that redirects to the Auth0 logout
endpoint, and configure the HttpSecurity
to add the logout handlerCheckpoint
When you click logout link, the application should redirect you to the address you specified as one of the “Allowed Logout URLs” in the “Settings” and you are no longer logged in to your application.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
- Okta Spring Boot Starter SDK - Explore the SDK used in this tutorial more fully
- Auth0 Marketplace - Discover integrations you can enable to extend Auth0’s functionality