2011-08-24 162 views
0

我正在使用的Struts2 + Spring + Hibernate的Web应用程序登录模块,并且我要强制用户登录,如果他们想浏览低谷的网站。Struts2的拦截器溢出异常

我有一个LoginInterceptor

public class LoginInterceptor implements Interceptor { 

public LoginInterceptor() { 
} 

public void destroy() { 
    System.out.println("LoginInterceptor destroy() is called..."); 
} 

public void init() { 
    System.out.println("LoginInterceptor init() is called..."); 
} 

public String intercept(ActionInvocation actionInvocation) throws Exception { 

    final ActionContext context = actionInvocation.getInvocationContext(); 
    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST); 
    HttpSession session = request.getSession(true); 

    //Code 

    try { 

     Users userObj = (Users) session.getAttribute("userObj"); 

     if (userObj == null) { 
      System.out.println("if from LoginInterceptor"); 
      return "noLogin"; 
     } 

    } catch (Exception e) { 
     Logger.getLogger(LoginInterceptor.class.getName()).log(Level.INFO, "message", e); 
    } 

    //Invoke action 
    String result = actionInvocation.invoke(); 

    return result; 
} 
} 

我在struts.xml

<interceptors> 

     <interceptor name="myLocale" class="com.deveto.struts.interceptors.LocaleInterceptor"/> 
     <interceptor name="login" class="com.deveto.struts.interceptors.LoginInterceptor"/> 
     <interceptor name="access" class="com.deveto.struts.interceptors.AccessInterceptor"/> 

     <interceptor-stack name="defaultStack"> 
      <interceptor-ref name="myLocale"/> 
      <interceptor-ref name="login"/> 
      <interceptor-ref name="exception"/> 
      <interceptor-ref name="alias"/> 
      <interceptor-ref name="prepare"/> 
      <interceptor-ref name="i18n"/> 
      <interceptor-ref name="chain"/> 
      <interceptor-ref name="debugging"/> 
      <interceptor-ref name="profiling"/> 
      <interceptor-ref name="fileUpload"/> 
      <interceptor-ref name="checkbox"/> 
      <interceptor-ref name="params"> 
       <param name="excludeParams">dojo\..*</param> 
      </interceptor-ref> 
      <interceptor-ref name="conversionError"/> 
      <interceptor-ref name="validation"> 
       <param name="excludeMethods">input,back,cancel,browse</param> 
      </interceptor-ref> 
      <interceptor-ref name="workflow"> 
       <param name="excludeMethods">input,back,cancel,browse</param> 
      </interceptor-ref> 
     </interceptor-stack> 
    </interceptors> 

    <default-interceptor-ref name="defaultStack"/> 

    <global-results> 
     <result name="noLogin" type="redirectAction">show-login</result> 
     <result name="noAccess" type="redirectAction">access-required</result> 
    </global-results> 

    <action 
     name="show-login" 
     class="com.deveto.struts.actions.UsersAction" > 
     <interceptor-ref name="defaultStack"/> 
     <result name="success" type="tiles">tiles.login</result> 
    </action> 

但是当我运行该项目,我有一个溢出异常,在堆栈跟踪我什么都没有,但Mozilla的告诉我即:

 The page isn't redirecting properly 

     Firefox has detected that the server is redirecting the request for this address in a way that will never complete. 

System.out.println("if from LoginInterceptor"); 

重复几次。我不明白为什么,但我有更多的拦截器,以及除此之外的其他作品。

+0

如何你'noLogin'结果定义? 我怀疑它重定向到再次得到由你'LoginInterceptor'然后重定向...你的想法截获一些登录页面。 –

+0

我在第二个框中提供了代码,请向下滚动第二个框,然后您会看到它。 – Denees

+0

对不起,我没有看到滚动条:/请参阅下面的答案。 –

回答

3

当你的LoginInterceptor返回其noLogin结果它重定向,这再次被LoginInterceptor拦截,这将其变成一个无尽的重定向循环。

所以,你必须从你的LoginInterceptor,例如被拦截排除您的show-login

定义两个拦截器堆栈,并与LoginInterceptor设置一个为默认:

<interceptor-stack name="defaultStack"> 
    ... same as in your question ... 
</interceptor-stack> 

<interceptor-stack name="noLoginStack"> 
    ... same as in your question but *without* the LoginInterceptor ... 
</interceptor-stack> 

<default-interceptor-ref name="defaultStack"/> 

再来说show-login动作而已,使用noLoginStack

<action name="show-login" 
    ... > 
    <interceptor-ref name="noLoginStack"/> 
    ... 
</action> 
+0

好吧,我做了这一点,但我也有同样的结果,即使我把行动没有任何拦截器栈,还是同样的错误 – Denees

+0

我只是你的确切LoginInterceptor,最小的struts.xml检查了本地(与拦截器堆栈我上面提出)和两个动作:“秘密”和“显示登录”(每个只是导致一个HTML简单的页面)---如果我尝试访问http:// myhost/myapp/secret我重定向一次到http://myhost/myapp/show-login.action然后停止。 你确定没有其他拦截器/代码正在做某件事吗? –

+0

下面是我用的struts.xml:http://pastebin.com/JKDDtZNV –