前端js部分:
思路:設定一個初始時間,監聽滑鼠事件,如果滑鼠有事件,初始值不變,否則間隔1s減少初始值,當初始值小于0的時候,調用後端登出即可
//監聽操作
var sessiontime;//這個值是自定的時間,可存資料庫,也可自定義
var maxTime; // seconds
if (sessiontime == null) {
maxTime = 30 * 60;
}else{
maxTime = sessiontime * 60;
}
var time = maxTime;
$('body').on('keydown mousemove keyCode', function (e) {
time = maxTime; // reset
});
var intervalId = setInterval(function () {
time--;
if (time <= 0) {
test();
clearInterval(intervalId);
}
}, 1000)
function test() {
var out = 1;
$.ajax({
url: ctx + "cmd/cmdbatomlineuser",
type: 'post',
data: {out: out},
success: function (data) {
if (data == "0") {
// layer.open({
// content: '登入逾時 (30 秒未活動),請重新登入。',
// btn: ['确認'],
// yes: function(index, layero) {
// window.location.href="/login" target="_blank" rel="external nofollow" ;
// },
// });
window.location.href = ctx + "login";
}
},
error: function () {
}
})
}
簡簡單的登出controller:
@RequestMapping("cmdbatomlineuser")
@ResponseBody
public String cmdbatomlineuser(String out) {
{
System.out.println("前端監聽操作事件");
if (out.equals("1")) {
return "0";
}else{
return "";
}
}
}
大佬勿噴,歡迎提意見建議評論!!!!