Struts2拦截器控制session失效

8 篇文章 0 订阅

com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.huangt.bean.SystemUser;
/* session过期、登录有效性及操作的权限验证拦截器 */
@SuppressWarnings("serial")
public class LoginedCheckInterceptor extends AbstractInterceptor {
/** 拦截请求并进行登录有效性验证 */
public String intercept(ActionInvocation ai) throws Exception {
HttpServletRequest request = (HttpServletRequest)ai.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
//取得请求的URL
String url = ServletActionContext.getRequest().getRequestURL().toString();
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires",0);
//对登录与注销请求直接放行,不予拦截
if (url.indexOf("login.action")!=-1 || url.indexOf("logout.action")!=-1){
return ai.invoke();
}
else{
//验证Session是否过期
if(!ServletActionContext.getRequest().isRequestedSessionIdValid()){
//session过期,转向session过期提示页,最终跳转至登录页面
return "tologin";
}
else{
HttpSession session = request.getSession();
String userId = (String)session.getAttribute(SessionConstant.SESSION_USER)
//验证是否已经登录
if (userId == null){
//尚未登录,跳转至登录页面
return "tologin";
}else{
return ai.invoke();

}
}
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值