在做web上的b/s系统,用的是struts2+spring+hibernate。想要实现的是:
a)当用户登录系统中后一段时间无任何操作,比如15分钟,让其自动返回到登录页面。
b) session过期时,自动返回登录。
因为我在main.jsp 用的是iframe框架,系统登录进入后该页面将系统分成了左菜单栏,上栏和主工作区三个部分。这个main.jsp页面中没有body体,在页面中写 window.location.href='/login.jsp'貌似没起没用。所以无法让其自动刷新来判断session是否为空了。
(1)想法是加个过滤器来监测session是否为空,为空时让其跳转路径,但网上找了些代码,没有实现成功。
(2)至于在登录进主页面中时,也不知道如何判断用户无操作。有种是判断mousemove事件,但在这种框架中不起作用。 请教各位高手有什么好的想法,最好是有代码。网上找了好多,没有实现
a)当用户登录系统中后一段时间无任何操作,比如15分钟,让其自动返回到登录页面。
b) session过期时,自动返回登录。
因为我在main.jsp 用的是iframe框架,系统登录进入后该页面将系统分成了左菜单栏,上栏和主工作区三个部分。这个main.jsp页面中没有body体,在页面中写 window.location.href='/login.jsp'貌似没起没用。所以无法让其自动刷新来判断session是否为空了。
(1)想法是加个过滤器来监测session是否为空,为空时让其跳转路径,但网上找了些代码,没有实现成功。
(2)至于在登录进主页面中时,也不知道如何判断用户无操作。有种是判断mousemove事件,但在这种框架中不起作用。 请教各位高手有什么好的想法,最好是有代码。网上找了好多,没有实现
if (self != top) {
window.top.location.href = "login.jsp";
}
1、将session的失效时间设置为15分钟。2、js定时器定时判断session是否失效。3、如果失效,则返回登录。
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String url = ((HttpServletRequest) request).getRequestURL().toString();
//如果是非action操作或者登入登出操作直接返回,例如图片静态资源时不需要session信息
if (url.indexOf(".action")<1||(url.indexOf("login.action") > 0||url.indexOf("logout.action")>0)) {
chain.doFilter(request, response);
}
//如果seesion不存在返回登陆页面
else if (((HttpServletRequest) request).getSession().getAttribute(
"user") == null) {
request.getRequestDispatcher("/index.jsp").forward(request,
response);
}
}
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.anxin.bean.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
/** session过期、登录有效性及操作的权限验证拦截器 */
public class LoginedCheckInterceptor extends AbstractInterceptor {
/** 拦截请求并进行登录有效性验证 */
public String intercept(ActionInvocation ai) throws Exception {
//取得请求的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);
User user = null;
//对登录与注销请求直接放行,不予拦截
if (url.indexOf("user_login.action")!=-1 || url.indexOf("logout.action")!=-1){
return ai.invoke();
}
else{
//验证Session是否过期
if(!ServletActionContext.getRequest().isRequestedSessionIdValid()){
//session过期,转向session过期提示页,最终跳转至登录页面
return "tologin";
}
else{
user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
//验证是否已经登录
if (user==null){
//尚未登录,跳转至登录页面
return "tologin";
}else{
return ai.invoke();
}
}
}
}
}
然后在struts.xml中给每个action配置拦截器,并声明一个全局的result,当session失效的时候拦截器会转发到登陆页面<?xml version="1.0" encoding="gbk"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<package name="anxin" extends="struts-default">
<!-- 配置自定义拦截器LoginedCheckInterceptor -->
<interceptors>
<interceptor name="loginedCheck" class="com.anxin.struts.interceptor.LoginedCheckInterceptor"/>
<interceptor-stack name="mystack">
<interceptor-ref name="loginedCheck" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 定义全局result -->
<global-results>
<!-- 定义名为exception的全局result -->
<result name="exception">exception.jsp</result>
<result name="tologin">login.jsp</result>
</global-results>
<!-- 定义全局异常映射 -->
<global-exception-mappings>
<!-- 捕捉到Exception异常(所有异常)时跳转到exception所命名的视图上 -->
<exception-mapping exception="java.lang.Exception" result="exception"/>
</global-exception-mappings>
<action name="user_*" class="userAction" method="{1}">
<result name="input">login.jsp</result>
<result name="success" type="redirect">student_query.action</result>
<interceptor-ref name="mystack" />
</action>
<action name="student_*" class="studentAction" method="{1}">
<result name="add">jsp/studentAdd.jsp</result>
<result name="update">jsp/studentEdit.jsp</result>
<result name="query">jsp/studentList.jsp</result>
<result name="success" type="redirect">student_query.action</result>
<interceptor-ref name="mystack" />
</action>
</package>
</struts>