struts2拦截器的问题

时间:2014-11-25 01:57:19

标签: java struts2 struts2-interceptors

我遇到麻烦,通过验证布尔变量是真还是假,拦截器阻止用户访问任何jsp;该变量位于bean(heyBean)中,通常在会话中使用操作方法设置(该操作实现会话感知)。如果为true,则用户可以继续执行操作;如果没有,则将用户重定向到登录页面。显然,登录页面不得受此拦截器的保护。问题是,在登录之前调用受保护的操作时,不会调用拦截器。

这是我的heyBean:

package hey.model;

import java.util.ArrayList;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import rmiserver.RMIServerInterface;

public class HeyBean {
    private RMIServerInterface server;
    private String username; // username and password supplied by the user
    private String password;
    private boolean isAuthenticated;

    public HeyBean() {
        try {
            server = (RMIServerInterface) Naming.lookup("server");
        } catch(NotBoundException|MalformedURLException|RemoteException e) {
            e.printStackTrace(); // what happens *after* we reach this line?
        }
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isIsAuthenticated() {
            return isAuthenticated;
    }

    public void setIsAuthenticated(boolean isAuthenticated) {
            this.isAuthenticated = isAuthenticated;
    }

    public boolean getUserMatchesPassword() throws RemoteException {
        return server.userMatchesPassword(this.username, this.password);
    }

    public ArrayList<String> getAllUsers() throws RemoteException {
        return server.getAllUsers(); // are you going to throw all exceptions?
    }

    public void sayHey(String whoSaidHey, String toWhoSaidHey) throws RemoteException {
        server.markAsHeyed(whoSaidHey, toWhoSaidHey);
    }

    public ArrayList<String> getAllWhoSaidHey() throws RemoteException {
        return server.getAllWhoSaidHey(); // are you going to throw all exceptions?
    }
}

这是我的拦截器:

package hey.interceptor;

import java.util.Map;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import hey.model.HeyBean;

public class LoginInterceptor implements Interceptor {
    private static final long serialVersionUID = 189237412378L;

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        Map<String, Object> session = invocation.getInvocationContext().getSession();

        // this method intercepts the execution of the action and we get access
        // to the session, to the action, and to the context of this invocation
        HeyBean hB = (HeyBean) session.get("heyBean");
        if(hB != null && hB.isIsAuthenticated()) {
            System.out.println("PASSOU!");
            return invocation.invoke();
        }
        else {
            System.out.println("NAO PASSOU!");
            return Action.LOGIN; 
        }
    }

    @Override
    public void init() { }

    @Override
    public void destroy() { }
}

这是我的struts.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!-- The core configuration file for the framework is the default (struts.xml) file
and should reside on the classpath of the webapp (generally /WEB-INF/classes). -->

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <!-- devMode equals debug information and reload everything for every request -->
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />

    <package name="hey" extends="struts-default">


        <!-- interceptor -->
        <interceptors>
            <interceptor name="loginInterceptor" class="hey.interceptor.LoginInterceptor" />
            <interceptor-stack name="loginStack">
                <interceptor-ref name="loginInterceptor" />
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>
        <default-interceptor-ref name="loginStack" />

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/error.jsp</result>
            <result name="login">/index.jsp</result>
        </global-results>

        <!-- all exceptions not caught by the application will lead to error.jsp -->
        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error" />
        </global-exception-mappings>

        <!-- 'index' action leads to the view provided by index.jsp -->
        <action name="index">
            <result>/index.jsp</result>
        </action>

        <!-- 'login' action calls 'execute' or 'logout' in 'LoginAction' -->
        <action name="login" class="hey.action.LoginAction" method="execute">
            <interceptor-ref name="defaultStack" />
            <result name="success">/hey.jsp</result>
            <result name="input">/index.jsp</result>
        </action>

        <action name="logout" class="hey.action.LogoutAction" method="execute">
            <result name="success">/index.jsp</result>
        </action>

        <action name="sayHey" class="hey.action.SayHeyAction" method="execute">
            <result name="success">/hey.jsp</result>
        </action>

    </package>

</struts>

1 个答案:

答案 0 :(得分:0)

我刚解决了!事实证明我在动作代码中执行了一个getHeyBean(),如果它之前不存在,它将构建一个新的HeyBean实例。我的不好:(。谢谢大家的合作。