Spring Security Customize Logout Handler

Spring Security provides lots of function for handling our program, the tutorial will guide you how to Customize Logout 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:
How to use Spring Security JDBC Authentication with PostgreSQL & Spring Boot
Spring Security – Customize Authentication Provider
Spring Security – Config Security for Web MVC by Spring Boot
Spring Security – Customize Login Handler

I. LogoutSuccessHandler for Customize Logout Handler tutorial

LogoutSuccessHandler is Spring Security strategy that is called after a successful logout, to handle redirection or forwarding to the appropriate destination.

Note that the interface is almost the same as LogoutHandler but may raise an exception. LogoutHandler implementations expect to be invoked to perform necessary cleanup, so should not throw exceptions.

II. Overview
1. Technologies

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

2. Overview project

customize-logout-handler-structure-of-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 LogoutSuccessHandler
– Add logoutSuccessHandler config at: WebSecurityConfigurerAdapter

III. Practices
1. Create Spring Security Project

For creating a Spring Security Project step by step follow article: Spring Security – Config Security for Web MVC by Spring Boot

2. Create a class that implements LogoutSuccessHandler

Create a class CustomLogoutSuccessHandler implements LogoutSuccessHandler, then override function: onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) for what program expected.

With above code: after logout successfully, program makes a log with info is username of User, then redirect to login Page.

3. Add logoutSuccessHandler config at: WebSecurityConfigurerAdapter

– In the class SecurityConfig extends WebSecurityConfigurerAdapter, inject logout customize bean:

Then handle logout config: logout().logoutSuccessHandler(customLogoutSuccessHandler)

Details:

IV. Sourcecode

SpringSecurityCustomizeLogoutSucessful

By JavaSampleApproach | November 15, 2016.

Last updated on June 4, 2017.


Related Posts


1 thought on “Spring Security Customize Logout Handler”

Got Something To Say:

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

*