如何在AuthenticationSuccessHandler中检索会话范围的bean?

我有一个自定义AuthenticationSuccessHandler。

我想要做的是在onAuthenticationSuccess方法中设置一些会话数据。

要存储会话数据,我想使用会话范围的bean,它可以在任何控制器中正常工作。

但是如果我尝试在onAuthenticationSuccess方法中检索它,我会得到一个例外:

创建名为’scopedTarget.sessionData’的bean时出错:当前线程的作用域’session’无效;

我的代码是:

WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()); SessionData sessionData = context.getBean(SessionData.class); 

有任何想法吗?

您可以尝试声明一个暴露实现会话范围所必需的状态的侦听器:

   org.springframework.web.context.request.RequestContextListener   

默认情况下, DispatcherServlet公开该状态,因此在请求进入DispatcherServlet之前它不可用(例如,在Spring Securityfilter中)。