使用拦截器在struts 2中进行身份验证后的登录重定向 [英] Login redirect after authentication in struts 2 using interceptors

查看:100
本文介绍了使用拦截器在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 */
}

我的struts.xml

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可以请求登录. loginAction用于验证用户.

Pathfinderaction can request for login. loginAction is used to validate the user.

推荐答案

您丢失了将对目标操作的URL使用动态参数的全局结果.

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> 

您可以从拦截器或实现TargertAware的操作中返回此结果. target属性应该具有要由结果调用的getter.

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.

这篇关于使用拦截器在struts 2中进行身份验证后的登录重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆