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");

数回繰り返されます。理由はわかりませんが、インターセプターが多く、これ以外にもうまく機能します。

4

2 に答える 2

3

LoginInterceptor結果が返されると、noLoginリダイレクトされ、これが再び傍受さLoginInterceptorれ、無限のリダイレクト ループになります。

したがって、たとえば、show-loginによって傍受されないように除外する必要があります。LoginInterceptor

2 つのインターセプター スタックを定義し、1 つを 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>
于 2011-08-24T13:57:17.670 に答える
1

そのためにSpring Securityを使用してみませんか?構成は非常に簡単で、Struts2 と完全に連携します。そこから始めてください: http://static.springsource.org/spring-security/site/tutorial.htmlまたはそこから: http://www.mularien.com/blog/2008/07/07/5-minute-guide -to-spring-security/

于 2011-08-25T20:20:37.030 に答える