问题描述
我正在尝试在基于shiro
的应用程序中显示会话即将超时对话框,这是我尝试执行的代码,但尚无法正常工作...
I am trying to show session is about to timeout dialog in my shiro
based application here is my code what i tried but its not working yet...
<%
Subject user = SecurityUtils.getSubject();
Session usersession = user.getSession(true);
if(user.isAuthenticated()){
Date startTime = usersession.getStartTimestamp(); //login time
Date lastAccessTime = usersession.getLastAccessTime(); //last active time
Date currentTime = new Date(); // current time
long diffInSec = (currentTime.getTime()- lastAccessTime.getTime())/1000 ; // calculate idle time of user
long timeout = (long)(usersession.getTimeout()* 0.001); // get sessiontime out in seconds
long oneMinleft = timeout - 60 ; // get time to show timeout dialog before 1 min of session timeout
System.out.println("startTime :"+startTime);
System.out.println("lastAccessTime :"+lastAccessTime);
System.out.println("currentTime :"+currentTime);
System.out.println("diffInSec :"+diffInSec);
System.out.println("Timeout:"+timeout);
System.out.println("Seconds left to timeout:"+oneMinleft);
System.out.println();
if(diffInSec >= oneMinleft){ %>
<script type="text/javascript">
showTimeout(); // show dialog
</script>
<%
}
}
%>
感谢您的帮助
感谢&问候
推荐答案
您的问题是您正在JSP中执行此操作,因此当用户发出请求时它会被执行,因此它不会空闲,会话超时会重新开始.您可能会在javascript中执行类似的操作,因此当用户什么都不做时,时间就会流逝.
Your problem is that you are doing that in JSP, so it is executed when the user is making a request so it is not idle and the session-timeout restarts.You may do something similar in javascript so the time runs when the user is doing nothing.
您可以通过以下方式混合JSP和javascript:
You can mix JSP and javascript this way:
setTimeout(function(){showTimeout()},<%= session.getMaxInactiveInterval() - 60*1000 %>);
您不需要lastAccessTime,因为您知道出席请求时它始终为0.
You don't need lastAccessTime since you know it always be 0 when attending a request.
这篇关于如何在jsp中1分钟之前显示会话即将超时对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!