spring - Spring Boot session 超时不起作用?

标签 spring spring-boot spring-security session-timeout

我设置了以下属性

server.servlet.session.timeout=30s

在我的应用程序属性中,但未触发 session 超时。 但设置后

server.servlet.session.cookie.max-age=30s

session 超时已触发,但以下用于更新注销时间的代码未触发。

 @Component
    public class LogoutListener implements ApplicationListener<SessionDestroyedEvent> {
     
   @Override
        public void onApplicationEvent(SessionDestroyedEvent event)
        {
            List<SecurityContext> lstSecurityContext = event.getSecurityContexts();
            UserDetails ud;
            for (SecurityContext securityContext : lstSecurityContext)
            {
                ud = (UserDetails) securityContext.getAuthentication().getPrincipal();
        
                us.findAllUsersByEmail(ud.getUsername()).get(0).setLastLogout(LocalDateTime.now());
                System.out.println("lastloginspec : " + ud.getUsername() + " : 00 : " + LocalDateTime.now());
            }
        }
        
        }
    
    
    @Bean
        public ServletListenerRegistrationBean<HttpSessionEventPublisher> httpSessionEventPublisher() {
            return new ServletListenerRegistrationBean<HttpSessionEventPublisher>(new HttpSessionEventPublisher());
    }

谁能帮帮我吗?

最佳答案

我通过以下方式实现了 session 监听器。

  1. 创建自定义 http session 监听器。

    @Component
    public class CustomHttpSessionListener implements HttpSessionListener{
    
    private static final Logger LOG= LoggerFactory.getLogger(Test.class);
    
     @Override
     public void sessionCreated(HttpSessionEvent se) {
         LOG.info("New session is created.");
         UserPrincipal principal = (UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
     }
    
     @Override
     public void sessionDestroyed(HttpSessionEvent se) {
         LOG.info("Session destroyed.");
         UserPrincipal principal = (UserPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
    
     }}
    
  2. 调用新的ServletListenerRegistrationBean并向其中添加CustomHttpListener并将其注释为@Bean。

    @Autowired private CustomHttpSessionListener customHttpSessionListener;
    
    @Bean 
    public ServletListenerRegistrationBean<CustomSessionListner>sessionListenerWithMetrics() {  ServletListenerRegistrationBean<CustomSessionListner>
         listenerRegBean = new ServletListenerRegistrationBean<>();
         listenerRegBean.setListener(customHttpSessionListener);
         return listenerRegBean;
    }
    
  3. 向 application.properties 添加属性

    server.servlet.session.timeout = 15m

关于spring - Spring Boot session 超时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63079913/

相关文章:

Redis 上的 Spring HttpSession 更改键名

java - Spring找不到我的html页面

spring-boot - 用于构建 SpringBoot 项目的原型(prototype)是什么?

java - 从 Grails Controller /服务获取 OpenID URL

spring - Spring Security可以解析 header 并验证身份验证信息吗?

java - Spring Data JPA 重复条目

java - 获取应用程序端口而不访问请求对象

java - 在 Spring Boot 中将属性放在 application.yml 或 bootstrap.yml 上有什么区别?

java - JSP 和 Spring Boot

java - 在 spring security saml 中,无法 GET/saml/login