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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With