Java Struts2登录侦听器不工作
当用户不在会话中时,我无法阻止他访问页面(比如welcome.jsp),请帮助我实现登录拦截器。这是我的密码。我想做的就是当用户使用他的用户id登录时,检查他是否在会话中,如果他在会话中,让他访问任何其他资源,将用户重定向到“somePage”。短暂性脑缺血发作 登录jspJava 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"%> <!
<%@ 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();
}
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;
}