我有一个spring应用程序,我使用Spring安全性进行了用户身份验证。
一切都很好。登录和注销工作完美!
现在,我想实现以便自动注销。例如,如果用户打开了大约30分钟的窗口而什么也不做(例如会话过期了),系统应该自动注销。我如何实现这一点?
它可能由客户端实现(我每1分钟发送一次请求,并检查会话是否结束)。但我不能从春天就自动这么做吗?
我有一个配置:
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/admin**" />
<access-denied-handler error-page="/403" />
<form-login login-page="/login"
default-target-url="/admin"
authentication-failure-url="/login?error"
username-parameter="NAME"
password-parameter="PASSWORD" />
<logout invalidate-session="true"
logout-success-url="/login?logout"/>
</http>在web.xml中
<session-config>
<session-timeout>1</session-timeout>
</session-config>1分钟后,我看到那次会议被毁了。1分钟后结束治疗。但是页面没有重定向到/login?logout
发布于 2015-01-05 08:31:26
如何使用安全配置。?我希望下面的配置:将工作。
applicationContext.xml
--namespace-> xmlns:security="http://www.springframework.org/schema/security"
<security:logout invalidate-session="true"
success-handler-ref="Logout"
logout-url="/logout.html" />
</security:http>web.xml
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>而且,您需要编写自己的程序,因为成功处理程序-ref=“logout”是用于注销的自定义处理程序:
注销@组件
public class Logout extends SimpleUrlLogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
if (authentication != null) {
// do something
}
setDefaultTargetUrl("/login");
super.onLogoutSuccess(request, response, authentication);
}
}发布于 2019-08-21 19:42:43
无论是spring安全性、spring还是servlet,如果没有完善的客户端逻辑,自动注销是不可能的。
考虑申请将有两种类型的请求
自动注销需要非常计算的逻辑。以下列方式显示我的自动退出功能实现
优势。
用法
1.在所需的JSP页面中包括自动注销脚本,如下所示
....
</body>
<jsp:include page="../template/autologout-script.jsp"></jsp:include>
</html>2.创建一个JSP页面,autoologout-script.jsp并添加下面的代码。注意:不需要编辑/配置
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script>
$(document).ready(function()
{
var timeOutTimeInSeconds = ${ timeOutTimeInSeconds };
var showTimerTimeInSeconds= ${ showTimerTimeInSeconds };
var sessionCheckIntervalId = setInterval(redirectToLoginPage, timeOutTimeInSeconds * 1000);
var timerDisplayIntervalId = setInterval(showTimer, (timeOutTimeInSeconds - showTimerTimeInSeconds) * 1000);
var badgeTimerId;
window.localStorage.setItem("AjaxRequestFired", new Date());
function redirectToLoginPage(){
//location.href = '<c:url value="/" />'+'${loginPageUrl}';
window.location.reload();
}
$(document).ajaxComplete(function () {
resetTimer();
});
$(window).bind('storage', function (e) {
if(e.originalEvent.key == "AjaxRequestFired"){
console.log("Request sent from another tab, hence resetting timer")
resetTimer();
}
});
function resetTimer()
{
showTimerTimeInSeconds= ${ showTimerTimeInSeconds };
console.log("timeOutTimeInSeconds : "+timeOutTimeInSeconds)
window.localStorage.setItem("AjaxRequestFired", new Date());
window.clearInterval(sessionCheckIntervalId);
sessionCheckIntervalId = setInterval(redirectToLoginPage, timeOutTimeInSeconds * 1000);
window.clearInterval(timerDisplayIntervalId);
timerDisplayIntervalId = setInterval(showTimer, (timeOutTimeInSeconds - showTimerTimeInSeconds) * 1000);
hideTimer();
}
function showTimer()
{
$('#sessionTimeRemaining').show();
$('#sessionTimeRemainingBadge').html(showTimerTimeInSeconds--);
window.clearInterval(timerDisplayIntervalId);
badgeTimerId = setInterval(function(){
$('#sessionTimeRemainingBadge').html(showTimerTimeInSeconds--);
}, 1000);
}
function hideTimer()
{
window.clearInterval(badgeTimerId);
$('#sessionTimeRemaining').hide();
}
});
</script>3.将会话属性配置为配置超时设置备注:在创建会话后配置此属性。您可以实现HttpSessionListener sessionCreated方法,并根据需要设置以下配置。
session.setMaxInactiveInterval(300);
session.setAttribute("timeOutTimeInSeconds", 300);
session.setAttribute("showTimerTimeInSeconds", 30);4.在html下面添加以显示计时器.
注意:如果你擅长CSS,它可以被移动到自动脚本模板页面。因此,您可以避免在每一页中添加此内容。
包括引导或添加您的自定义css。
<span class="badge badge-primary" title="click to keep session alive" id="sessionTimeRemaining"
onclick="ajaxSessionRefresh()" style="display:none;">
<i class="badge badge-danger" id="sessionTimeRemainingBadge" style="float:left">30</i>
<small>Refresh</small>
<i class="glyphicon glyphicon-refresh"></i>
</span>

这都是关于一个简单的自动注销实现。您可以从我的github存储库下载工作示例。
逻辑解释
案例1:页面加载
这里的逻辑很简单,页面加载设置定时器的间隔为maxInactiveInterval。超时后重定向到登录页面。
案例2:跟踪AJAX调用
现在,考虑到AJAX请求,您可以使用jquery的.ajaxStart()或.ajaxComplete()回调,以便如果触发任何ajax请求,就可以重置间隔。
案例3:跟踪多选项卡/窗口活动
选项卡间通信用于同步每个选项卡的状态。在更改事件上使用localStorage。
所需的限制/改进
要求
比较目前实施的备选办法
response.setHeader("Refresh", "60; URL=login.jsp");<meta http-equiv="refresh" content="60; url=login.jsp">
发布于 2015-01-05 08:22:10
您可以在web.xml中使用全局超时值:
<session-config>
<session-timeout>30</session-timeout>
</session-config>https://stackoverflow.com/questions/27775651
复制相似问题