Java后端长时间无操作自动退出的实现方式
短信预约 -IT技能 免费直播动态提醒
Java后端长时间无操作自动退出
使用session(最优)
设置session的过期时间,长时间(例如30分钟)无请求就会自动清除,达到长时间无操作自动退出的目的
server:
port: 9201
session:
timeout: 1800
使用拦截器
实现思路:每次请求后台时,在拦截器中刷新session,设置session时间为30分钟,这样请求每次进来都会重新设置session的过期时间
对于登陆长时间未操作超时退出问题
首先设置一个拦截器
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;
public class SessionFilter implements Filter {
@Override
public void destroy() {
}
@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);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
然后进行配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 全局session处理action -->
<package name="session" namespace="/session" extends="json-default">
<action name="*" class="sessionAction" method="{1}">
<result name="json" type="json">
<param name="contentType">text/html</param>
<param name="ignoreHierarchy">false</param>
</result>
</action>
</package>
</struts>
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;
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class SessionAction extends ActionSupport {
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");
}
}
页面
<div id="checkLastPostTimeDialog" class="easyui-dialog" style="width:400px;height:200px;padding:20px;" data-options="title:'已锁定',border:false,closable:false,draggable:false,resizable:false,closed:true,modal:true,tools:'#checkLastPostTimeDialogTool'">
<form id="checkLastPostTimeDialogForm" method="post">
<div id="checkLastPostTimeDialogError" style="color:red;position:absolute;right:20px;" align="right" ></div>
<table width="100%" >
<tr>
<td >账 号:</td>
<td height="40px" align="left"><shiro:principal /><input id="checkLastPostTimeDialogFormUserName" type="hidden" style="width:220px;height:30px;line-height:30px;border-color:#5b97db;border-width: 1px;border-style: solid;" name="username" value="<shiro:principal />" /></td>
</tr>
<tr>
<td >密 码:</td>
<td height="40px" align="left"><input id="checkLastPostTimeDialogFormPassword" required="true" class="easyui-textbox" type="password" style="width:220px;height:30px;line-height:30px;border-color:#5b97db;border-width: 1px;border-style: solid;" name="password" value="" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input class="login" type="button" οnclick="checkLastPostTimeDialogFormSumbit();" style="width:110px;height:33px;border:0" value="" />
</td>
</tr>
</table>
</form>
</div>
<div id="checkLastPostTimeDialogTool">
<a href="#" style="width:30px;text-decoration:underline;line-height:16px;font-size:12px;" οnclick="logout();" >注销</a>
</div>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341