Java后端長(zhǎng)時(shí)間無(wú)操作自動(dòng)退出的實(shí)現(xiàn)方式
Java后端長(zhǎng)時(shí)間無(wú)操作自動(dòng)退出
使用session(最優(yōu))
設(shè)置session的過(guò)期時(shí)間,長(zhǎng)時(shí)間(例如30分鐘)無(wú)請(qǐng)求就會(huì)自動(dòng)清除,達(dá)到長(zhǎng)時(shí)間無(wú)操作自動(dòng)退出的目的
server: port: 9201 session: timeout: 1800
使用攔截器
實(shí)現(xiàn)思路:每次請(qǐng)求后臺(tái)時(shí),在攔截器中刷新session,設(shè)置session時(shí)間為30分鐘,這樣請(qǐng)求每次進(jìn)來(lái)都會(huì)重新設(shè)置session的過(guò)期時(shí)間
對(duì)于登陸長(zhǎng)時(shí)間未操作超時(shí)退出問(wèn)題
首先設(shè)置一個(gè)攔截器
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; ?? ??? ?// 如果不為'鑒定是否鎖定的請(qǐng)求',將用戶的請(qǐng)求時(shí)間放置session ?? ??? ?if (!httpRequest.getServletPath().equals( ?? ??? ??? ??? ?"/session/checkLastPostTime.action")) { ?? ??? ??? ?HttpSession session = httpRequest.getSession(true); ?? ??? ??? ?session.setAttribute("lastPostTime", System.currentTimeMillis()); ?? ??? ?} ?? ??? ?// 執(zhí)行之后的過(guò)濾 ?? ??? ?filterChain.doFilter(request, response);? ?? ?}? ?? ?/* ?? ? * (non-Javadoc) ?? ? *? ?? ? * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) ?? ? */ ?? ?@Override ?? ?public void init(FilterConfig arg0) throws ServletException {? ?? ?}? }
然后進(jìn)行配置文件
<?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; ? /** ?* 全局session處理action ?*? ?* @author shenyanwei ?* @date:2016年11月18日 下午1:59:56 ?* @version ?*/ @Controller @Scope("prototype") @SuppressWarnings("all") public class SessionAction extends ActionSupport { ?? ?/** ?? ? * 鑒權(quán)用戶停止操作的時(shí)間是否超過(guò)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; ?? ?} }
頁(yè)面以及調(diào)用
$(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"); ?? ??? ?} ?? ?}
頁(yè)面
<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 >賬 ? ?號(hào):</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>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
200行Java代碼編寫(xiě)一個(gè)計(jì)算器程序
本篇文章給大家分享的只用200行java代碼,實(shí)現(xiàn)一個(gè)計(jì)算器程序,不僅能夠計(jì)算加減乘除,還能夠匹配小括號(hào)。實(shí)現(xiàn)代碼超簡(jiǎn)單,需要的朋友參考下吧2017-12-12淺談Java消息隊(duì)列總結(jié)篇(ActiveMQ、RabbitMQ、ZeroMQ、Kafka)
這篇文章主要介紹了淺談Java消息隊(duì)列總結(jié)篇(ActiveMQ、RabbitMQ、ZeroMQ、Kafka),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05springmvc數(shù)據(jù)的封裝過(guò)程詳解
這篇文章主要介紹了springmvc數(shù)據(jù)的封裝過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Java 多線程并發(fā)編程_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java 多線程并發(fā)編程的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05