【问题标题】:Add flash attribute to logout in Spring Security在 Spring Security 中添加 flash 属性以注销
【发布时间】:2018-06-23 03:11:05
【问题描述】:

我正在使用 Spring Security,我想为登录页面的自动重定向添加一个 flash 属性。但是LogoutSuccessHandlerhandle方法只有HttpServletRequestHttpServletResponseAuthentication作为参数。如何在handle 方法中检索RedirectAttributes 以添加闪存属性?还是有其他方法可以将 Flash 属性添加到注销重定向?

【问题讨论】:

  • RedirectAttributes 是添加 flash 属性的默认/推荐方式(参见文档:docs.spring.io/spring/docs/current/spring-framework-reference/…)。
  • @dur 非常感谢,我忽略了这一点。我试图从 LogoutSuccessHandler 中检索 outputFlashMap 和 flashMapManager,但 RequestContextUtils.getFlashMapManagerRequestContextUtils.getOutputFlashMap 都返回 null。
  • @dur 我用 LogoutHandler 进行了尝试,但结果相同(即两种方法都返回 null)。也许是不可能的。
  • Rolch2015 你找到答案了吗?我也有同样的闪信问题?
  • @carousel 不幸的是,在写这个问题之前我已经尝试了很长一段时间来寻找解决方案,但从那以后就放弃了,因为有更高优先级的事情。也许我会在稍后的某个时间查看它,如果我找到解决方案,我会在这里发布解决方案。但是,如果您找到解决方案,我也很想知道它。谢谢!

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


【解决方案1】:

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

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

灵感来自this answer 用户@vallismortis。

【讨论】:

    猜你喜欢
    • 2014-08-11
    • 1970-01-01
    • 2020-04-02
    • 2017-04-22
    • 2012-10-09
    • 2018-09-02
    • 2011-06-28
    • 2016-07-12
    • 2011-07-19
    相关资源
    最近更新 更多