Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Struts2登录侦听器不工作_Java_Jsp_Authentication_Struts2_Interceptor - Fatal编程技术网

Java Struts2登录侦听器不工作

Java Struts2登录侦听器不工作,java,jsp,authentication,struts2,interceptor,Java,Jsp,Authentication,Struts2,Interceptor,当用户不在会话中时,我无法阻止他访问页面(比如welcome.jsp),请帮助我实现登录拦截器。这是我的密码。我想做的就是当用户使用他的用户id登录时,检查他是否在会话中,如果他在会话中,让他访问任何其他资源,将用户重定向到“somePage”。短暂性脑缺血发作 登录jsp <%@ page language="java" contentType="text/html; charset=US-ASCII" pageEncoding="US-ASCII"%> <!

当用户不在会话中时,我无法阻止他访问页面(比如welcome.jsp),请帮助我实现登录拦截器。这是我的密码。我想做的就是当用户使用他的用户id登录时,检查他是否在会话中,如果他在会话中,让他访问任何其他资源,将用户重定向到“somePage”。短暂性脑缺血发作

登录jsp

    <%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%-- Using Struts2 Tags in JSP --%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Login Page</title>
</head>
<body>
<h3>Welcome User, please login below</h3>
<s:form action="login">
    <s:textfield name="userId" label="userId"></s:textfield>
    <s:submit value="Login"></s:submit>
</s:form>
</body>
</html>

登录页面
欢迎用户,请在下面登录

您没有使用在操作中定义的authStack:

 <action name="welcome" class="com.mycompany.abc.webapp.action.WelcomeAction">
<interceptor-ref name="authStack"></interceptor-ref>
    <result name="success">/jsp/welcome.jsp</result>
</action>
最后(也是最重要的),你的拦截器是错误的。下一行返回ActionContext,而不是会话:

ActionContext sessionAttributes = actionInvocation.getInvocationContext();
如果要返回会话,请尝试:

Map<String, Object> session = ActionContext.getContext().getSession();
Map session=ActionContext.getContext().getSession();

你的拦截器代码完全是胡说八道

public String intercept(ActionInvocation actionInvocation) throws Exception {
    ActionContext sessionAttributes = actionInvocation.getInvocationContext();

    Object sess = sessionAttributes.get("userid");
    if (sess == null) {
        if (sessionAttributes.get("userId") != null) {
            result = actionInvocation.invoke();
        }
        return result;
    }

    return actionInvocation.invoke();
}
  • 获取“用户ID”
  • 如果它是空的
  • …检查它是否为空,然后
  • …如果不是,则调用并返回操作的结果
  • 如果不是。。。 1…调用并返回操作的结果
  • 这假设您实际上正在查看会话

    粗略地说,根据记忆,你打算做什么:

    public String intercept(ActionInvocation actionInvocation) throws Exception {
        Map<String, Object> session = actionInvocation.getInvocationContext().getSession();
        return session.containsKey(SESSION_USER_KEY) ? actionInvocation.invoke() 
                                                     : GLOBAL_RESULT_LOGIN;
    }
    
    publicstringintercept(ActionInvocation-ActionInvocation)引发异常{
    映射会话=actionInvocation.getInvocationContext().getSession();
    返回session.containsKey(session\u USER\u KEY)?actionInvocation.invoke()
    :全局\u结果\u登录;
    }
    
    如果将
    ActionContext
    命名为
    sessionAttributes
    is,则不会将其类型更改为session。
    HttpSession session = ServletActionContext.getRequest().getSession();
    
    ActionContext sessionAttributes = actionInvocation.getInvocationContext();
    
    Map<String, Object> session = ActionContext.getContext().getSession();
    
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        ActionContext sessionAttributes = actionInvocation.getInvocationContext();
    
        Object sess = sessionAttributes.get("userid");
        if (sess == null) {
            if (sessionAttributes.get("userId") != null) {
                result = actionInvocation.invoke();
            }
            return result;
        }
    
        return actionInvocation.invoke();
    }
    
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        Map<String, Object> session = actionInvocation.getInvocationContext().getSession();
        return session.containsKey(SESSION_USER_KEY) ? actionInvocation.invoke() 
                                                     : GLOBAL_RESULT_LOGIN;
    }