Perform custom event before Session Expiry in Spring

后端 未结 3 1868
别跟我提以往
别跟我提以往 2020-12-10 03:25

I am beginner in Spring framework.

In my case Session can be expire by following way
--> Success Log-out (Explicit Log-out )

--> Session Timeout (Implici

相关标签:
3条回答
  • 2020-12-10 03:49

    Yes, you can do that with 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:

    <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.

    0 讨论(0)
  • 2020-12-10 04:00
    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");
        } 
    }
    
    0 讨论(0)
  • 2020-12-10 04:08

    I have solved my problem by following way similar @Codo answer

    @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
         }  
    }   
    }
    
    0 讨论(0)
提交回复
热议问题