VOOZH about

URL: https://www.geeksforgeeks.org/advance-java/redirect-to-different-pages-after-login-with-spring-security/

⇱ Redirect to Different Pages After Login With Spring Security - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Redirect to Different Pages After Login With Spring Security

Last Updated : 29 May, 2026

Spring Security allows developers to control authentication and authorization in Spring Boot applications. In many applications, different users need different dashboards after login. It provides role-based redirection using a custom authentication success handler. After successful login, users can be redirected to different pages based on their roles and authorities.

  • Redirect users to different pages after successful login.
  • Handle role-based access using ROLE_ADMIN and ROLE_USER.
  • Improve user experience with personalized dashboards.

Implementation to Redirect to Different Pages After Login

Below are the implementation steps to redirect to different pages after login with spring.

Step 1: Create Spring Boot Project

Create a Spring Boot project using Spring Initializr with the following dependencies:

  • Spring Web
  • Spring Security
  • Thymeleaf
  • Lombok
  • Spring DevTools
  • Spring Data JPA
  • MySQL Driver

After the project creation done, the folder structure will be like the below image:

👁 Project Structure


Step 2: Configure application.properties

Add database and user credentials configuration inside application.properties.

spring.application.name=spring-security-redirect-pages

# Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/example
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

# User Credentials
spring.security.user.name=user
spring.security.user.password=password
spring.security.user.roles=USER

# Admin Credentials

spring.security.admin.name=admin
spring.security.admin.password=password
spring.security.admin.roles=ADMIN

Step 3: Configure Spring Security

Create SecurityConfig.java inside the config package.

  • SecurityFilterChain secures application URLs.
  • /admin/** is accessible only to ADMIN users.

Step 4: Create Custom Authentication Success Handler

Create CustomAuthenticationSuccessHandler.java inside the config package.

  • Checks authenticated user roles.
  • Redirects ADMIN users to /admin/home.

Step 5: Create Admin Controller

Create AdminController.java.

  • Handles requests for the admin dashboard.
  • Returns adminHome.html page.

Step 6: Create User Controller

Create UserController.java.

  • Handles user dashboard requests.
  • Returns login page for authentication.

Step 7: Main Application Class

Open the main class and write the below code. (No change are required)

Step 8: Create Login Page

Create login.html inside templates.

Step 9: Create User Home Page

Displayed after successful USER login.

Step 10: Create Admin Home Page

Displayed after successful ADMIN login.

Step 12: Run the Application

Run the Spring Boot application. After completing the project, it will start at port 8080.

👁 Application Runs

Login page:

API:

http://localhost:8080/login

If we enter the user credential the it redirects to the user home page.

  • username: user
  • password: password

If we enter the admin credential the it redirects to the user home page.

  • username: admin
  • password: password

Output:

👁 Login page

AdminHome page:

👁 redirectadmin

UserHome page:

After hitting the URL, the below page will redirect.

👁 Use Home page
Comment
Article Tags:

Explore