struts2拦截器问题

Jomicobasi

我在通过验证布尔变量为true或false来使拦截器阻止用户访问任何jsp时遇到麻烦;该变量位于bean(heyBean)中,该bean通常是在会话中使用操作方法设置的(该操作实现了会话识别)。如果为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>
Jomicobasi

我已经解决了!事实证明,我正在操作代码中执行getHeyBean(),如果以前不存在,它将构造一个新的HeyBean实例。我的坏人:(。谢谢大家的合作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Struts2准备拦截器?

来自分类Dev

Fileuploader拦截器Struts2

来自分类Dev

Fileuploader拦截器Struts2

来自分类Dev

Ajax jquery和struts2拦截器的参数

来自分类Dev

Struts2令牌拦截器:CSRF保护

来自分类Dev

使用struts2注释定义拦截器

来自分类Dev

通过Struts2 + Spring项目中的StrutsSpringTestCase调用拦截器

来自分类Dev

i18n在Struts2中使用拦截器

来自分类Dev

如何处理Struts2中默认拦截器引发的异常?

来自分类Dev

无法在struts2的modelDreiven拦截器中将字符串设置为Int

来自分类Dev

使用消息存储拦截器进行struts2验证

来自分类Dev

如何像在struts2中一样在烧瓶上添加拦截器

来自分类Dev

如何在Struts2中配置范围模型驱动的拦截器

来自分类Dev

页面未正确重定向拦截器struts2

来自分类Dev

如何在Struts2拦截器中配置HTTP响应标头?

来自分类Dev

Struts2如何为操作覆盖全局拦截器堆栈

来自分类Dev

Struts2从登录拦截器重定向

来自分类Dev

Struts2会话中使用了拦截器,为什么它永远不会为空?

来自分类Dev

如何在struts2中为自定义拦截器添加excludeMethods参数列表

来自分类Dev

在Struts 2拦截器中获取HttpServletRequest

来自分类Dev

无法使用ModelDriven拦截器执行Struts 2程序

来自分类Dev

未设置 Struts 2 拦截器响应标头

来自分类Dev

AngularJS:$ resource中的拦截器问题

来自分类Dev

LoopBack 版本 4 拦截器问题

来自分类Dev

无法通过超链接实现Struts 2令牌拦截器

来自分类Dev

Struts 2仍在拦截器中时重置操作实例

来自分类Dev

Struts 2中具有bean值的自定义日志记录拦截器

来自分类Dev

如何从Struts 2的默认拦截器中排除一项操作

来自分类Dev

无法通过超链接实现Struts 2令牌拦截器

Related 相关文章

热门标签

归档