2013-02-19 133 views
2

我在login.jsp頁面的隱藏字段中設置'cat = 1',並期待它在default-target-url上可用。進入春季-security.xml文件是,如何將請求參數傳遞給'default-target-url'

<form-login login-page="/login.html" default-target-url="/index.html" 
      authentication-failure-url="/loginfailed.html" /> 

,並在控制器,

@RequestMapping(value="/index", method = RequestMethod.GET) 
    public String index(HttpServletRequest request) { 
     String cat = request.getParameter("cat"); 
     if (cat != null && cat.equalsIgnoreCase("1")) { 
      return "add"; 
     } 
     return "redirect:/index.jsp"; 
    } 

但不能得到請求參數值(貓爲空),所以我認爲這是因爲「默認靶url'重定向請求(並且不轉發它?)。是這樣嗎?

如果是,那麼有什麼辦法可以將參數傳遞給'default-target-url'嗎?

回答

0

重定向由redirectStrategy屬性SimpleUrlAuthenticationSuccessHandler中定義的重定向策略控制。

redirectStrategy的默認值是DefaultRedirectStrategy的實例。

你需要做的是實現你自己的redirectStrategy(執行RedirectStrategy)。 ,對其進行配置:

... 
<bean id="usernamePasswordAuthenticationFilter"> 
    ... 
    <property name="authenticationSuccessHandler"> 
     <bean 
      class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"> 
      <property name="redirectStrategy"> 
       <bean class="yourRedirectStrategy"/> 
      <property> 
     </bean> 
    </property> 
</bean> 
0

重定向通過defult,但也有你可以用它來改變這種行爲,一對夫婦的配置選項。它們都在AbstractAuthenticationTargetUrlRequestHandler上定義,它是兩個現有認證成功處理程序實現的父類(缺省情況下SavedRequestAwareAuthenticationSuccessHandler由名稱空間配置使用)。

  1. 設置其targetUrlParameter屬性,所以它會檢查HTTP請求與該名稱的參數。如果是這樣,它將重定向到該請求參數中給出的URL。或者設置自定義redirectStrategy。默認實現調用response.sendRedirect(),但您可以在自定義實現中更改它。

您將有一定的難度,但因爲沒有這些配置點都是通過命名空間配置曝光,所以你需要去更深一層,手動讀寫bean定義。

2

我已經改變了實施方法。詳細信息在下面給出,

彈簧security.xml文件

<form-login login-page="/login.html" authentication-success-handler-ref="feedSuccessHandler" 
      authentication-failure-url="/loginfailed.html" /> 
     <logout logout-success-url="/loggedout.html"/> 

<beans:bean id="feedSuccessHandler" 
    class="main.java.com.sp.utilities.FeedSuccessHandler"> 
    </beans:bean> 

FeedSuccessHandler.java

public class FeedSuccessHandler implements AuthenticationSuccessHandler { 

    public void onAuthenticationSuccess(HttpServletRequest request, 
      HttpServletResponse response, Authentication authentication) 
      throws IOException, ServletException { 

     String cat = request.getParameter("cat"); 
     if (cat != null && cat.equalsIgnoreCase("1")) { 
      response.sendRedirect(request.getContextPath()+"/add.html"); 
     }else{ 
      SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response); 
      if(savedRequest != null) { 
       response.sendRedirect(savedRequest.getRedirectUrl()); 
      }else{ 
       response.sendRedirect(request.getContextPath()+"/"); 
      } 
     } 
    } 
} 

應用程序正在根據需要也將來如果我想根據重定向定製角色,我可以使用相同的類。