简体   繁体   English

使用 Struts 2 中的拦截器进行身份验证后登录重定向

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

I have a login page.我有一个登录页面。 Requests for login can come from multiple action classes.登录请求可以来自多个操作类。 Once the user is validated I have to redirect it to the previous action class (from which the request to login has come).验证用户后,我必须将其重定向到前一个操作类(登录请求来自该类)。 I am using interceptors for doing this.我正在使用拦截器来做到这一点。 But I have missed something and it is not able to redirect properly.但我错过了一些东西,它无法正确重定向。

Here is my code:这是我的代码:

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 */
}

My struts.xml :我的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 can request for login. PathFinderAction可以请求登录。 loginAction is used to validate the user. loginAction用于验证用户。

You are missing a global result that will use a dynamic parameter for your target action's URL.您缺少一个全局结果,该结果将对目标操作的 URL 使用动态参数。

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

You can return this result from the interceptor or from the action that implements TargertAware .您可以从拦截器或实现TargertAware的操作中返回此结果。 The target property should have a getter to be invoked by the result. target属性应该有一个由结果调用的 getter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM