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.
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.
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.
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.
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.
Spring Boot provides a spring-boot-starter-oauth2-client
starter, which provides all the Spring Security dependencies needed to add authentication to your web application.
If you're using Gradle, you can include these dependencies as shown below.
plugins {
id 'java'
id 'org.springframework.boot' version '2.5.12'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
}
Was this helpful?
If you are using Maven:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.12</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
</dependencies>
Was this helpful?
Spring Security makes it easy to configure your application for authentication with OIDC providers such as Auth0. In your application's configuration, configure the OAuth2 client and provider. The sample to the right shows an application.yml
file, though you can also use properties files or any of the other supported externalization mechanisms.
To enable user login with Auth0, create a class that will provide an instance of SecurityFilterChain, and add the @EnableWebSecurity
annotation.
Later in this quickstart, you will overwrite this file with SecurityConfigWithLogout.java
to provide extra configurations to support the logout feature.
Spring Security uses the client configuration you defined earlier to handle login when a user visits the /oauth2/authorization/auth0
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.
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.
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.
Now that users can log into your application, they need a way to log out. By default, Spring Security logs user out of your application and clears the session when you enable logout. To enable successful Auth0 logout, extend the SecurityContextLogoutHandler
class to redirect users to your Auth0 logout endpoint (https://{yourDomain}/v2/logout
) and then immediately redirect them to your application.
Next, update your implementation of SecurityFilterChain
to register your logout handler and specify the request path that should trigger logout (/logout
in the example below).
You can remove the SecurityConfig.java
and replace it with SecurityConfigWithLogout.java
or update the contents from the one file to another.
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.
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:
Sign up for an or to your existing account to integrate directly with your own tenant.