Struts2拦截器溢出异常

发布于 2024-11-30 22:54:17 字数 3713 浏览 0 评论 0原文

我正在使用 Struts2 + Spring + Hibernate 为 Web 应用程序开发登录模块,如果用户想要浏览网站,我想强制用户登录。

我有一个 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");

重复了几次。我不明白为什么,但我有更多的拦截器,除此之外其他工作也很好。

I am working on a Login module for a Web Application using Struts2 + Spring + Hibernate, and I want to force the users to Login if they want to navigate trough the site.

I have an 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;
}
}

My 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>

But when I run the project, I have an Overflow exception, in Stack trace I have nothing, but Mozilla tells me that:

      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.

and

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

is repeated several times. I don't understand why, but I have more Interceptors, and other works good besides of this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往事随风而去 2024-12-07 22:54:17

当您的 LoginInterceptor 返回其 noLogin 结果时,它会进行重定向,并再次被 LoginInterceptor 拦截,从而将其变成无限的重定向循环。

因此,您必须排除您的 show-login 被您的 LoginInterceptor 拦截,例如

定义两个拦截器堆栈,并将其中一个 with 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>

When your LoginInterceptor returns its noLogin result it redirects and this gets again intercepted by LoginInterceptor which turns it into an endless redirect loop.

So you have to exclude your show-login from being intercepted by your LoginInterceptor, e.g.

Define two interceptor stacks, and set the one with LoginInterceptor as default:

<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"/>

And then for the show-login action only, use the noLoginStack:

<action name="show-login"
    ... >
    <interceptor-ref name="noLoginStack"/>
    ...
</action>
时光无声 2024-12-07 22:54:17

为什么不使用 Spring Security 来做到这一点呢?
它配置非常简单,并且与 Struts2 完美配合。
从那里开始: http://static.springsource.org/spring-security/ site/tutorial.html 或那里:http://www.mularien.com/blog/2008/07/07/5-min-guide-to-spring-security/

Why don't you just use Spring Security to do that ?
It's very simple to configure and it works perfectly with Struts2.
Just start from there : http://static.springsource.org/spring-security/site/tutorial.html or there : http://www.mularien.com/blog/2008/07/07/5-minute-guide-to-spring-security/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文