Spring Security – Customize Login Handler

Spring Security provides lots of API for handling our program, the tutorial will guide you how to Customize Login Handler.
For How to start with Spring Security, you can follow the link: Spring Security – Config Security for Web MVC by Spring Boot

Related Articles: Spring Security Customize Logout Handler


I. AuthenticationSuccessHandler for Customize Login Handler tutorial

interface AuthenticationSuccessHandler is strategy used to handle a successful user authentication.

Implementations can do whatever they want but typical behaviour would be to control the navigation to the subsequent destination (using a redirect or a forward). For example, after a user has logged in by submitting a login form, the application needs to decide where they should be redirected to afterwards (see AbstractAuthenticationProcessingFilter and subclasses). Other logic may also be included if required.

II. Overview
1. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.2.RELEASE

2. Project structure

spring-security-customize-login-handler-project

3. Step to do

– Create a Spring Security step by step follow article: Spring Security – Config Security for Web MVC by Spring Boot

Main Part:
– Create a class that implements AuthenticationSuccessHandler
– Add successHandler config at: WebSecurityConfigurerAdapter

III. Practice
1. Create a Spring Security

Create a Spring Security step by step follow article: Spring Security – Config Security for Web MVC by Spring Boot

2. Create a class that implements AuthenticationSuccessHandler

Create a CustomAuthenticationSuccessHandler that implement interface AuthenticationSuccessHandler by override function: onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) for developing the customize logic of application after login successfully.

With above code, after login successfully, application will check role of login user, if user has Admin role, it will be redirected to /admin url, otherwise it will be redirected to /welcome url

3. Add successHandler config at: WebSecurityConfigurerAdapter

In config class SecurityConfig extends WebSecurityConfigurerAdapter, inject CustomAuthenticationSuccessHandler bean. Then config successhandler: formLogin().successHandler(customAuthenticationSuccessHandler)

Details:

IV. Sourcecode

SpringSecurityCustomizeLoginSucess


Related Posts



4 thoughts on “Spring Security – Customize Login Handler”

  1. What’s up, I check your blogs on a regular basis. Your story-telling style is awesome, keep it up!

  2. I am sure this post has touched all the internet users, its really really good paragraph on building up new webpage.

  3. Greetings from Idaho! I’m bored to death at work so I decided to check out your blog on my iphone during lunch break. I enjoy the info you provide here and can’t wait to take a look when I get home. I’m amazed at how fast your blog loaded on my phone .. I’m not even using WIFI, just 3G .. Anyhow, good site!

Got Something To Say:

Your email address will not be published. Required fields are marked *

*