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