How I can get session object from ApplicationListener method

前端 未结 1 1535
清歌不尽
清歌不尽 2020-12-18 09:50

I want to add object to HttpSession after successful user authentication. Please don\'t suggest solution with SavedRequestAwareAuthentica

相关标签:
1条回答
  • 2020-12-18 10:24

    As far as I am aware, ApplicationListener instances are just beans within your ApplicationContext. Therefore you should be able to inject other beans or resources into them.

    So to get a reference to the current HttpSession instance:

    public class AuthenticationSuccessListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
    
    @Autowired
    private HttpSession httpSession;
    
            @Override
            public void onApplicationEvent(InteractiveAuthenticationSuccessEvent e) {
                   //adding object to HttpSession
            }
    }
    

    Spring will inject the HttpSession using its scoped proxy mechanism ensuring that you get the HTTPSession relevant to the current thread of execution.

    You'll also need to ensure that you register a RequestContextListener in your web.xml so that Spring can inject the current HTTPSession.

    <listener>  
       <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>  
    </listener>
    
    0 讨论(0)
提交回复
热议问题