在 Spring Security 中为注销添加 flash 属性

Rol*_*015 5 java spring spring-mvc spring-security spring-boot

我正在使用 Spring Security,我想为自动重定向到登录页面添加一个 flash 属性。但是,唯一的handle方法LogoutSuccessHandlerHttpServletRequest, HttpServletResponse,Authentication作为参数。如何RedirectAttributeshandle方法中检索以添加 flash 属性?或者有没有其他方法可以将 flash 属性添加到注销重定向?

Glo*_*del 3

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

灵感来自于用户@vallismortis 的这个答案