Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add flash attribute to logout in Spring Security

I am using Spring Security, and I would like to add a flash attribute to the automatic redirect to the login page. However, the handle method of the LogoutSuccessHandler only has HttpServletRequest, HttpServletResponse, Authentication as parameters. How can I retrieve the RedirectAttributes within the handle method in order to add flash attributes? Or is there an other way to add to flash attributes to the logout redirect?

like image 772
Rolch2015 Avatar asked Nov 18 '22 23:11

Rolch2015


1 Answers

For me, the following code worked:

public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {
        final FlashMap flashMap = new FlashMap();
        flashMap.put("information", "You've been logged out.");
        final FlashMapManager flashMapManager = new SessionFlashMapManager();
        flashMapManager.saveOutputFlashMap(flashMap, request, response);
        response.sendRedirect("/"); // or any other location you want
        super.handle(request, response, authentication);
    }
}

Inspiration was taken from this answer by user @vallismortis.

like image 104
Glorfindel Avatar answered Jan 05 '23 09:01

Glorfindel