对于登陆长时间未操作超时退出问题

首先设置一个拦截器:

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 全局session拦截器
 * 
 * @author shenyanwei
 * @date:2016年11月18日 下午1:33:40
 * @version
 */
public class SessionFilter implements Filter {

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		// 如果不为'鉴定是否锁定的请求',将用户的请求时间放置session
		if (!httpRequest.getServletPath().equals(
				"/session/checkLastPostTime.action")) {
			HttpSession session = httpRequest.getSession(true);
			session.setAttribute("lastPostTime", System.currentTimeMillis());
		}
		// 执行之后的过滤
		filterChain.doFilter(request, response);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}
然后进行配置文件:




	
	
		
			
				text/html
				false
			
		
	
Action:

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 全局session处理action
 * 
 * @author shenyanwei
 * @date:2016年11月18日 下午1:59:56
 * @version
 */
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class SessionAction extends ActionSupport {
	/**
	 * 鉴权用户停止操作的时间是否超过20分钟
	 * 
	 * @return
	 */
	public String checkLastPostTime() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object attribute = session.getAttribute("lastPostTime");
		ActionContext.getContext().getValueStack().push(false);
		if (null != attribute) {
			Long lastPostTime = (Long) attribute;
			long currentTimeMillis = System.currentTimeMillis();
			if (1200000 <= (currentTimeMillis - lastPostTime)) {
				ActionContext.getContext().getValueStack().push(true);
			}
		}
		return ReturnType.JSON;
	}
}
页面以及调用:


$(function() {
      
        checkLastPostTime();
    });
var checkLastPostTimeInterval; 
	function checkLastPostTime(){
		checkLastPostTimeInterval = window.setInterval(function() {
			$.post("${pageContext.request.contextPath}/session/checkLastPostTime.action",{},
				function(result){
					if (result) {
						window.clearInterval(checkLastPostTimeInterval);
						$("#checkLastPostTimeDialogError").html(" ");
						$("#checkLastPostTimeDialog").dialog('open');
					}
				}
			,"json");
		}, 300000);
	}
	
	function checkLastPostTimeDialogFormSumbit(){
		if($('#checkLastPostTimeDialogForm').form('validate')){
			$.post("${pageContext.request.contextPath}/jsonLogin.action",{
					username:$("#checkLastPostTimeDialogFormUserName").val(),
					password:$("#checkLastPostTimeDialogFormPassword").textbox('getValue')
				},
				function(result){
					if (result.errorMsg) {
						$("#checkLastPostTimeDialogError").html(result.errorMsg);
					} else {
						$("#checkLastPostTimeDialog").dialog('close');
						checkLastPostTime();
					}
				}
			,"json");
		}
	}

页面:

账    号:
密    码:


你可能感兴趣的:(技术)