如何在AuthenticationSuccessHandler中检索会话范围的bean? [英] How to retrieve a session-scoped bean inside AuthenticationSuccessHandler?
问题描述
我有一个自定义AuthenticationSuccessHandler。
I have a custom AuthenticationSuccessHandler.
我想要做的是在onAuthenticationSuccess方法中设置一些会话数据。
What I want to do is to set some session data within onAuthenticationSuccess method.
要存储会话数据我想使用会话范围的bean,它可以在任何控制器中正常工作。
To store session data I want to use a session-scoped bean, which works fine within any controller.
但是如果我尝试在onAuthenticationSuccess方法中检索它,我得到一个例外:
But if I try to retrieve it within onAuthenticationSuccess method, I get an exception:
创建名称为
'scopedTarget.sessionData'的bean时出错:范围
'session '对于
当前线程无效;
Error creating bean with name 'scopedTarget.sessionData': Scope 'session' is not active for the current thread;
我的代码是:
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
SessionData sessionData = context.getBean(SessionData.class);
任何想法?
推荐答案
您可以尝试声明一个暴露实现会话范围所需的状态的侦听器:
You can try to declare a listener that exposes state necessary to implement session scope:
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
默认情况下,该状态由 DispatcherServlet
公开,所以在请求进入 DispatcherServlet
之前它不可用(例如在Spring Security过滤器中)。
By default that state is exposed by DispatcherServlet
, so it's not available before request enters DispatcherServlet
(e.g. in Spring Security filters).
这篇关于如何在AuthenticationSuccessHandler中检索会话范围的bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!