【问题标题】:Accessing RedirectAttributes in method without RedirectAttributes in signature在签名中没有 RedirectAttributes 的方法中访问 RedirectAttributes
【发布时间】:2015-02-06 13:24:59
【问题描述】:

是否可以在没有 RedirectAttributes 签名的方法中访问 RedirectAttributes?例如,当覆盖如下方法时:

@Override
public void onAuthenticationSuccess(final HttpServletRequest req, final HttpServletResponse res,
        final Authentication auth) throws IOException, ServletException {

    // add something to RedirectAttributes here
    // redirectAttributes.addFlashAttribute("attr", "value");

    super.onAuthenticationSuccess(req, res, auth);
}

我正在使用 spring 3.2.2.RELEASE。

【问题讨论】:

  • 你打算用属性做什么?
  • @Ivan 我需要他们添加客户首次登陆主页的指示。
  • 对不起,显然我不清楚这个问题:你打算使用什么 RedirectAttributes 方法?添加/获取FlashAttributes?我在问,因为有一种直接访问flash属性的方法,其他参数可以作为请求参数。
  • @Ivan 是的,我需要 redirectAttributes.addFlashAttribute

标签: java spring spring-mvc servlets


【解决方案1】:

在 DispatcherServlet 类实现中可以看到,有常量:

public static final String FLASH_MAP_MANAGER_BEAN_NAME = "flashMapManager";
public static final String OUTPUT_FLASH_MAP_ATTRIBUTE = DispatcherServlet.class.getName() + ".OUTPUT_FLASH_MAP";
public static final String FLASH_MAP_MANAGER_ATTRIBUTE = DispatcherServlet.class.getName() + ".FLASH_MAP_MANAGER";

Spring 有一个名为 RequestContextUtils 的类,它有方法:

public static Map<String, ?> getInputFlashMap(HttpServletRequest request)
public static FlashMap getOutputFlashMap(HttpServletRequest request)    
public static FlashMapManager getFlashMapManager(HttpServletRequest request)

前两种方法将分别让您访问输入和输出闪存映射。 最后一个方法返回 FlashMapManager,它有许多方便的方法来处理 flash 属性。有关详细信息,请参阅此接口的实现,特别是 AbstractFlashMapManager。

【讨论】:

  • 我也有同样的问题。我需要在我的自定义 Spring AuthSuccessHandler 和 AuthFailureHandler 中获取 FlashMapManager 或 outputFlashMap。 Manual 说 FlashMapManager 和 FlashMap 永远不会为空(docs.spring.io/autorepo/docs/spring/3.2.3.RELEASE/javadoc-api/…),但不幸的是它是。您能否分享您从无法访问重定向属性的地方访问闪存地图管理器或闪存地图本身的经验。谢谢。
【解决方案2】:

如果您的目标是“添加客户首次登陆主页的指示”,那么 HttpSession 可能会成功:

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
      throws IOException
{
    ...
    boolean firstLogin = <determine whether this is the first login>;
    <reset condition that caused firstLogin to be true>;
    ...
    HttpSession session = request.getSession(false);
    if (session != null) {
        session.setAttribute("firstLogin", firstLogin);
    }
    else {
        log.debug("No session");
    }
}

// Controller for some page
@RequestMapping(value = <your page>, method = RequestMethod.GET)
public String showPage(<some other args>, HttpSession session)
{
    ...
    Object firstLogin = session.getAttribute("firstLogin");
    Assert.isInstanceOf(Boolean.class, firstLogin, "firstLogin");
    if ((Boolean)firstLogin) {
        <handle first login>;
    }
    ...
    return <page>;
}

这对我有用。这背后的逻辑是登录是在整个会话的上下文中进行的,该会话可能包含多个请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多