java - struts2拦截器的问题

标签 java struts2 struts2-interceptors

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

这是我的 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>

最佳答案

我刚刚解决了这个问题!事实证明,我在操作代码中执行了 getHeyBean() 操作,如果以前不存在,那么将构造一个新的 HeyBean 实例。我的错:(。谢谢大家的合作。

关于java - struts2拦截器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27117549/

相关文章:

java - struts2使用什么算法来生成随机 token ?

java - 为什么我的学生字符串没有打印出来?

javascript - jQuery 用户界面 : How to pass values to dialog

java - Struts2无法判断拦截器是否工作

web-applications - 如何在 tomcat 中部署一个 struts2 web 应用程序?没有 eclipse ?我应该如何安排文件夹中的文件?

java - 如何在 Struts2 拦截器中配置 HTTP 响应 header ?

java - 使用 "allowedTypes"参数限制struts2中的文件类型

java - 使用正则表达式获取匹配字符串的索引

javax.el.E​​LException : java. java.util.AbstractList.add 处的 lang.UnsupportedOperationException

java - Android - 如何使 View 仅填充屏幕的其余部分