在Spring Security中向logout添加flash属性

时间:2018-01-14 05:13:44

标签: spring spring-mvc spring-boot spring-security

我正在使用Spring Security,我想在自动重定向到登录页面时添加flash属性。但是,handle的{​​{1}}方法只有LogoutSuccessHandlerHttpServletRequestHttpServletResponse作为参数。如何在Authentication方法中检索RedirectAttributes以添加Flash属性?或者是否有另一种方法可以将flash属性添加到注销重定向?非常感谢你。

1 个答案:

答案 0 :(得分:0)

对我来说,以下代码有效:

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);
    }
}

灵感来自用户@vallismortis 的 this answer