最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java后端長(zhǎng)時(shí)間無操作自動(dòng)退出的實(shí)現(xiàn)方式

 更新時(shí)間:2022年01月07日 08:44:51   作者:shyの程序猿  
這篇文章主要介紹了Java后端長(zhǎng)時(shí)間無操作自動(dòng)退出的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java后端長(zhǎng)時(shí)間無操作自動(dòng)退出

使用session(最優(yōu))

設(shè)置session的過期時(shí)間,長(zhǎng)時(shí)間(例如30分鐘)無請(qǐng)求就會(huì)自動(dòng)清除,達(dá)到長(zhǎng)時(shí)間無操作自動(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)來都會(huì)重新設(shè)置session的過期時(shí)間

對(duì)于登陸長(zhǎng)時(shí)間未操作超時(shí)退出問題

首先設(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í)行之后的過濾
?? ??? ?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í)間是否超過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;
?? ?}
}

頁面以及調(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");
?? ??? ?}
?? ?}

頁面

<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)文章

  • JVM,JRE和JDK的區(qū)別小結(jié)

    JVM,JRE和JDK的區(qū)別小結(jié)

    在Java環(huán)境配置和項(xiàng)目啟動(dòng)中,JVM,JRE和JDK這三者的配置是項(xiàng)目啟動(dòng)的基礎(chǔ)保證,本文就詳細(xì)的介紹一下這三者的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • Java雪花算法生成分布式id詳解

    Java雪花算法生成分布式id詳解

    這篇文章主要介紹了Java雪花算法生成分布式id詳解,隨著業(yè)務(wù)的增長(zhǎng),有些表可能要占用很大的物理存儲(chǔ)空間,為了解決該問題,后期使用數(shù)據(jù)庫(kù)分片技術(shù),將一個(gè)數(shù)據(jù)庫(kù)進(jìn)行拆分,通過數(shù)據(jù)庫(kù)中間件連接,需要的朋友可以參考下
    2024-01-01
  • Java實(shí)現(xiàn)微信公眾號(hào)獲取臨時(shí)二維碼功能示例

    Java實(shí)現(xiàn)微信公眾號(hào)獲取臨時(shí)二維碼功能示例

    這篇文章主要介紹了Java實(shí)現(xiàn)微信公眾號(hào)獲取臨時(shí)二維碼功能,結(jié)合實(shí)例形式分析了java調(diào)用微信公眾號(hào)接口實(shí)現(xiàn)臨時(shí)二維碼生成功能相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10
  • Restful之通用返回格式類設(shè)計(jì)

    Restful之通用返回格式類設(shè)計(jì)

    這篇文章主要介紹了Restful之通用返回格式類設(shè)計(jì),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • 理解zookeeper選舉機(jī)制

    理解zookeeper選舉機(jī)制

    本文主要介紹了zookeeper選舉機(jī)制的相關(guān)知識(shí),具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • Java生成CSV文件實(shí)例詳解

    Java生成CSV文件實(shí)例詳解

    這篇文章主要介紹了Java生成CSV文件的方法,很實(shí)用的功能,需要的朋友可以參考下
    2014-07-07
  • SpringBoot 轉(zhuǎn)發(fā)請(qǐng)求至指定頁面的操作方法

    SpringBoot 轉(zhuǎn)發(fā)請(qǐng)求至指定頁面的操作方法

    這篇文章主要介紹了SpringBoot 轉(zhuǎn)發(fā)請(qǐng)求至指定頁面,需要使用@Controller,不可使用@RestController,@RestController相當(dāng)于@Controller和@ResponseBody合在一起的作用,本文通過實(shí)例代碼詳細(xì)講解,需要的朋友可以參考下
    2022-11-11
  • java?freemarker實(shí)現(xiàn)動(dòng)態(tài)生成excel文件

    java?freemarker實(shí)現(xiàn)動(dòng)態(tài)生成excel文件

    這篇文章主要為大家詳細(xì)介紹了java如何通過freemarker實(shí)現(xiàn)動(dòng)態(tài)生成excel文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12
  • JAVA--HashMap熱門面試題

    JAVA--HashMap熱門面試題

    這篇文章主要介紹了JAVA關(guān)于HashMap容易被提問的面試題,文中題目提問頻率高,相信對(duì)你的面試有一定幫助,想要入職JAVA的朋友可以了解下
    2020-06-06
  • Java中CountDownLatch進(jìn)行多線程同步詳解及實(shí)例代碼

    Java中CountDownLatch進(jìn)行多線程同步詳解及實(shí)例代碼

    這篇文章主要介紹了Java中CountDownLatch進(jìn)行多線程同步詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-03-03

最新評(píng)論

台东县| 玛沁县| 海晏县| 临猗县| 新巴尔虎左旗| 新乐市| 宜都市| 九江市| 大同县| 荃湾区| 兴文县| 柘城县| 铜鼓县| 新化县| 哈尔滨市| 元朗区| 龙游县| 卢氏县| 陈巴尔虎旗| 泸水县| 上杭县| 镇坪县| 贵溪市| 上林县| 平山县| 三河市| 阳信县| 久治县| 盐源县| 祥云县| 眉山市| 双牌县| 洛扎县| 布拖县| 旺苍县| 双牌县| 东台市| 乌海市| 呼和浩特市| 安福县| 冀州市|