In the real-life scenario, most software applications work with user-based authentication. When the user accesses the website url in the Spring boot application, the user is redirected to the authentication login page. After successful login, the user is redirected to different dashboard based on logged in user roles.

In this article, we’ll see how to create a login page to authenticate and link to different user role-based dashboard pages.

Step 1 – Configure Security dependency

Create a spring boot application and configure spring security dependency in pom.xml. Add web application dependency to run tomcat server. The security module will allow to configure user roles based redirection.

The pom.xml below contains all of the required dependency.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
		<relativePath /> <!-- lookup parent from repository -->
	<description>Spring Boot Project</description>







Step 2 – Add User and Admin Dashboard jsp files

The spring boot application runs on tomcat server. Add the jsp configurations in file.

Add login.jsp to login authentication page. Add two dashboard jsp files, say userDashboard.jsp and adminDashboard.jsp. Upon successful login, the security module will be redirected to any of the jsp dashboard pages.



<h1>Welcome to Spring Boot Security</h1>

<form method="POST" action="/login">
	User Name : <input type="text" name="username" value="user"/><br><br>
	Password  : <input type="password" name="password" value="password"/><br><br>
	<input type="submit" name="submit"/>


<h1>Welcome to Spring Boot User DashBoard</h1>

<h2>You are in Spring Boot User DashBoard Page</h2>
<br><a href="/logout">logout</a>


<h1>Welcome to Spring Boot Admin DashBoard</h1>

<h2>You are in Spring Boot Admin DashBoard Page</h2>
<br><a href="/logout">logout</a>

Step 3 – Configure Controller class for jsp files

Create a controller class that redirects your urls to the respective jsp files. Add 3 urls to the controller class. The “/login” url will render the.jsp login page. The “/userDashboard” url will display the “userdashboard.jsp” page. If the user role is “ROLE USER,” the login page will be redirected to the user dashboard page. “/adminDashboard” url will display the “admindashboard.jsp” page. If the logged in user role is “ROLE_ADMIN”, the user will be redirected to the admin dashboard page.

package com.yawintutor;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public class TestController {

	public ModelAndView login() {
		return new ModelAndView("login");

	public ModelAndView userDashboard() {
		return new ModelAndView("userdashboard");

	public ModelAndView adminDashboard() {
		return new ModelAndView("admindashboard");

Step 4 – Spring Boot Security configuration

The user-based redirect is configured using the customized Success Handler for spring boot security. Create a Custom Success Handler class with AuthenticationSuccessHandler. The AuthenticationSuccessHandler interface will be called for successful login authentication. Add the logic to redirect the user’s role to the respective dashboard pages.

The following code is a sample implementation of the AuthenticationSuccessHandler interface.

package com.yawintutor;

import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

public class CustomSuccessHandler implements AuthenticationSuccessHandler {

	public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
			Authentication authentication) throws IOException, ServletException {

		String redirectUrl = null;

		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
		for (GrantedAuthority grantedAuthority : authorities) {
			System.out.println("role " + grantedAuthority.getAuthority());
			if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
				redirectUrl = "/userDashboard";
			} else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
				redirectUrl = "/adminDashboard";
		System.out.println("redirectUrl " + redirectUrl);
		if (redirectUrl == null) {
			throw new IllegalStateException();
		new DefaultRedirectStrategy().sendRedirect(request, response, redirectUrl);

Create a Spring Boot Security Configuration class by extending the SpringBootSecurityConfiguration class. Create an auto-wired class object reference for the AuthenticationSuccessHandler interface. Use the AuthenticationSuccessHandler object to validate the security configuration.

package com.yawintutor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

public class SpringBootSecurityConfiguration extends WebSecurityConfigurerAdapter {
	AuthenticationSuccessHandler successHandler;
	public void configure(AuthenticationManagerBuilder auth) throws Exception {

	public void configure(HttpSecurity http) throws Exception {

Step 4 – How UI looks

Admin Login redirects to admin dash board.

Leave a Reply