簡體   English   中英

使用 Struts 2 中的攔截器進行身份驗證后登錄重定向

[英]Login redirect after authentication using interceptors in Struts 2

我有一個登錄頁面。 登錄請求可以來自多個操作類。 驗證用戶后,我必須將其重定向到前一個操作類(登錄請求來自該類)。 我正在使用攔截器來做到這一點。 但我錯過了一些東西,它無法正確重定向。

這是我的代碼:

public class SetTargetInterceptor extends MethodFilterInterceptor implements
    Interceptor {
private static final long serialVersionUID = 1L;

public String doIntercept(ActionInvocation invocation) throws Exception {
    Object action = invocation.getAction();
    HttpServletRequest request = (HttpServletRequest) invocation
            .getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
    if (action instanceof TargetAware) {
        TargetAware targetAwareAction = (TargetAware) action;
        if (targetAwareAction.getTarget() == null)
            targetAwareAction.setTarget(getCurrentUri(request));
    }
    return invocation.invoke();
}

private static String getCurrentUri(HttpServletRequest request) {
    String uri = request.getRequestURI();
    String[] arr = org.apache.commons.lang3.StringUtils.split(uri, "/");
    for (int i = 0; i < arr.length; i++) {
        System.out.println(arr[i]);
    }
    if (arr != null && arr.length > 0) {
        int len = arr.length;
        uri = arr[len - 1];
    }

    String queryString = request.getQueryString();

    if (queryString != null && !queryString.equals(""))
        uri += "?" + queryString;
    return uri;

}

public void init() { /* do nothing */
}

public void destroy() { /* do nothing */
}

我的struts.xml

<interceptors>  
<interceptor name="setTargetInterceptor" 
class="com.markEffy.aggregator.interceptors.SetTargetInterceptor"></interceptor>

     <interceptor-stack name="newStack">
        <interceptor-ref name="setTargetInterceptor"/>
    <interceptor-ref name="defaultStack" />
      </interceptor-stack>

</interceptors>
 <action name="login" 
        class="com.markEffy.aggregator.web.LoginAction" 
        method="login">
  <result name="success"  type="redirect">
  <param name="location">${target}</param>
  <param name="parse">true</param>
   </result>
 <action name="reload" 
        class="com.markEffy.aggregator.web.PathFinderAction" 
        method="execute">
         <interceptor-ref name="newStack"/>
        <result name="success">/results.jsp</result>
  </action>

PathFinderAction可以請求登錄。 loginAction用於驗證用戶。

您缺少一個全局結果,該結果將對目標操作的 URL 使用動態參數。

<global-results>
    <result name="return" type="redirect">${target}</result>
</global-results> 

您可以從攔截器或實現TargertAware的操作中返回此結果。 target屬性應該有一個由結果調用的 getter。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM