gpt4 book ai didi

java - 未调用 ApplicationListener

转载 作者:行者123 更新时间:2023-12-02 10:42:28 26 4
gpt4 key购买 nike

我需要审核超时 logOut 事件,我搜索了一下并找到了解决方案。但这不起作用。当用户注销或超时时,根本不会调用事件方法。

这是我的代码:ObjectLock.java:

@Component
public class ObjectLock implements ApplicationListener<SessionDestroyedEvent> {

@Override
public void onApplicationEvent(SessionDestroyedEvent event)
{
List<SecurityContext> lstSecurityContext = event.getSecurityContexts();
String userName;
for (SecurityContext securityContext : lstSecurityContext)
{
userName = (String)securityContext.getAuthentication().getPrincipal();
System.out.println("Log Out " + userName);
// ...
}
}
}

应用程序.java:

 public class Application {
public static void main(String[] args) throws Exception {
TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
SpringApplication application = new SpringApplication( Application.class );
application.addListeners(new ObjectLock());
ConfigurableApplicationContext context = application.run(args);
// ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
}
}

谁能告诉我这是怎么回事?谢谢。

最佳答案

添加以下代码/类,然后监听器将被调用:

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.web.session.HttpSessionEventPublisher;

@Configuration
public class ApplicationConfig {
@Bean
public ServletListenerRegistrationBean<HttpSessionEventPublisher> httpSessionEventPublisher() {
return new ServletListenerRegistrationBean<HttpSessionEventPublisher>(new HttpSessionEventPublisher());
}
}

关于java - 未调用 ApplicationListener<SessionDestroyedEvent>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52838124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com