Search Tutorials

Spring Boot Security - Redirect to different pages after login using AuthenticationSuccessHandler Example | JavaInUse

Spring Boot Security - Redirect to different pages after Login using AuthenticationSuccessHandler Example

In a previous post we had implemented Spring Boot Security - Database Authentication.
In some scenarios we might want to redirect different users to different pages depending on the roles assigned to the users.
For example we might want users with role USER to be redirected to the welcome page, while users with role ADMIN to be redirected to the add employee page.
We will be making use of the AuthenticationSuccessHandler.

Spring Boot Security - Table Of Contents

Spring Boot + Simple Security Configuration Spring Boot Form Security Login Hello World Example Spring Boot Security - Custom Login Page Example Spring Boot Security - JDBC Authentication Example Spring Boot Security - Creating Users Programmatically Using JdbcUserDetailsManager Spring Boot Security - Password Encoding Using Bcrypt Spring Boot Security - Enabling CSRF Protection Spring Boot Security - Authentication Handler Example

Lets Begin-

We will be modifying the code we developed in the previous Spring Boot Security - Creating a custom login page
Maven Project will be as follows-

Add a new custom AuthenticationSuccessHandler which will do the redirection based on the roles. So the user javainuse will be redirected to the add new employee page while the user employee will be redirected to the welcome page on login.
package com.javainuse.handler;

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 EmployeeAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

	private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

	public void onAuthenticationSuccess(HttpServletRequest arg0, HttpServletResponse arg1,
			Authentication authentication) throws IOException, ServletException {

		boolean hasUserRole = false;
		boolean hasAdminRole = false;
		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
		for (GrantedAuthority grantedAuthority : authorities) {
			if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
				hasUserRole = true;
			} else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
				hasAdminRole = true;

		if (hasUserRole) {
			redirectStrategy.sendRedirect(arg0, arg1, "/welcome");
		} else if (hasAdminRole) {
			redirectStrategy.sendRedirect(arg0, arg1, "/addNewEmployee");
		} else {
			throw new IllegalStateException();


Finally modify the Spring Security configuration to autowire and use the custom AuthenticationSuccessHandler.
package com.javainuse.config;

import javax.sql.DataSource;

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

import com.javainuse.handler.EmployeeAuthenticationSuccessHandler;

public class EmployeeSecurityConfiguration extends WebSecurityConfigurerAdapter {

	DataSource dataSource;

	private EmployeeAuthenticationSuccessHandler successHandler;

	// Enable jdbc authentication
	public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {

	public void configure(WebSecurity web) throws Exception {

	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/welcome").hasAnyRole("USER", "ADMIN")
				.antMatchers("/getEmployees").hasAnyRole("USER", "ADMIN").antMatchers("/addNewEmployee")


	// @Autowired
	// public void configureGlobal(AuthenticationManagerBuilder authenticationMgr)
	// throws Exception {
	// authenticationMgr.inMemoryAuthentication().withUser("admin").password("admin").authorities("ROLE_USER").and()
	// .withUser("javainuse").password("javainuse").authorities("ROLE_USER",
	// "ROLE_ADMIN");
	// }


These are the only changes required. Go to localhost:8080/welcome, we will be redirected to the custom login page.
Enter the user javainuse and password javainuse, user is redirected to the add employee page.
Enter the user employee and password employee, user is redirected to the welcome page.

Download Source Code

Download it -
Spring Boot Security - Redirect using Authentication Handler