java 在 Spring 会话到期之前执行自定义事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27753111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Perform custom event before Session Expiry in Spring
提问by Yasir Shabbir Choudhary
I am beginner in Spring framework.
我是 Spring 框架的初学者。
In my case Session can be expire by following way
--> Success Log-out (Explicit Log-out )
在我的情况下,会话可以通过以下方式过期
--> 成功注销(显式注销)
--> Session Timeout (Implicit Log-out )
--> 会话超时(隐式注销)
I have do DML(record insertion) in database whenever some user log in and I want to perform DML(record deletion) in database whenever user session timeout (Implicit Log-out).
每当某些用户登录时,我都会在数据库中执行 DML(记录插入),并且每当用户会话超时(隐式注销)时,我都想在数据库中执行 DML(记录删除)。
My Question is that is there any way in Spring that tell us before the expiry of session. So I can do perform my custom event before session expiry.
我的问题是 Spring 中有什么方法可以在会话到期之前告诉我们。所以我可以在会话到期之前执行我的自定义事件。
Thanks in advance
提前致谢
回答by Codo
Yes, you can do that with SessionDestroyedEvent.
是的,您可以使用SessionDestroyedEvent做到这一点。
@Component
public class SessionEndedListener implements ApplicationListener<SessionDestroyedEvent> {
@Override
public void onApplicationEvent(SessionDestroyedEvent event)
{
for (SecurityContext securityContext : event.getSecurityContexts())
{
Authentication authentication = securityContext.getAuthentication();
YourPrincipalClass user = (YourPrincipalClass) authentication.getPrincipal();
// do something
}
}
}
And in web.xml:
在 web.xml 中:
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
This event will be fired for both the regular logout as well as the session timeout.
常规注销和会话超时都会触发此事件。
回答by Yasir Shabbir Choudhary
I have solved my problem by following way similar @Codo answer
我已经通过类似@Codo 的回答方式解决了我的问题
@Component
public class SessionCreatedListenerService implements ApplicationListener<ApplicationEvent> {
private static final Logger logger = LoggerFactory
.getLogger(SessionCreatedListenerService.class);
@Autowired
HttpSession httpSession;
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
if(applicationEvent instanceof HttpSessionCreatedEvent){ //If event is a session created event
}else if(applicationEvent instanceof HttpSessionDestroyedEvent){ //If event is a session destroy event
// handler.expireCart();
logger.debug(""+(Long)httpSession.getAttribute("userId"));
logger.debug(" Session is destory :" ); //log data
}else if(applicationEvent instanceof AuthenticationSuccessEvent){ //If event is a session destroy event
logger.debug(" athentication is success :" ); //log data
}else{
/*logger.debug(" unknown event occur : " Source: " + ); //log data
}
}
}
回答by kundan kumar
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.LogoutSuccessEvent;
import org.springframework.stereotype.Component;
@Component
public class LogoutSuccessListener implements ApplicationListener<LogoutSuccessEvent>{
@Override
public void onApplicationEvent(LogoutSuccessEvent evt) {
String login = evt.getAuthentication().getName();
System.out.println(login + " has just logged out");
}
}