2015-02-06 57 views
0

在簽名中沒有RedirectAttributes的方法中可以訪問RedirectAttributes嗎?例如,如下面的覆蓋方法:在簽名中沒有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。

+0

你打算如何處理屬性? – Ivan 2015-02-06 13:40:50

+0

@Ivan我需要他們補充說明客戶第一次登陸主頁。 – lolotron 2015-02-06 13:43:07

+0

對不起,顯然我不清楚這個問題:你打算使用什麼RedirectAttributes方法?添加/ getFlashAttributes?我問,因爲有直接訪問flash屬性的方法,其他參數可以作爲請求參數。 – Ivan 2015-02-06 13:45:50

回答

0

正如你可以在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"; 

春天有一個名爲RequestContextUtils提供,它擁有一些方法:

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

前兩種方法會給你分別訪問輸入和輸出閃存映射。 最後一個方法返回FlashMapManager,它有許多方便的方法來處理flash屬性。有關詳細信息,請參見此接口的實現,特別是AbstractFlashMapManager。

+0

我編輯了我的答案,使其更清晰 – Ivan 2015-02-06 14:01:53

+0

我有同樣的問題。我需要在我的自定義Spring AuthSuccessHandler和AuthFailureHandler中獲取FlashMapManager或outputFlashMap。手冊說FlashMapManager和FlashMap從不爲空(http://docs.spring.io/autorepo/docs/spring/3.2.3.RELEASE/javadoc-api/org/springframework/web/servlet/support/RequestContextUtils.html)但不幸的是。您可以請分享您訪問Flash地圖管理器或flashmap本身的經驗,因爲redirectAttributes無法訪問。謝謝。 – 2015-04-08 22:59:37

0

如果你的目標是「增加表明客戶已經登陸主頁上的第一次」,那麼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>; 
} 

這對我的作品。這背後的邏輯是登錄是在整個會話的上下文中,據推測,它包含多個請求。

相關問題