简体   繁体   中英

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 :

<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. loginAction is used to validate the user.

You are missing a global result that will use a dynamic parameter for your target action's 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 . The target property should have a getter to be invoked by the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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