[英]How to retrieve a session-scoped bean inside AuthenticationSuccessHandler?
I have a custom AuthenticationSuccessHandler. 我有一个自定义AuthenticationSuccessHandler。
What I want to do is to set some session data within onAuthenticationSuccess method. 我想要做的是在onAuthenticationSuccess方法中设置一些会话数据。
To store session data I want to use a session-scoped bean, which works fine within any controller. 要存储会话数据,我想使用会话范围的bean,它可以在任何控制器中正常工作。
But if I try to retrieve it within onAuthenticationSuccess method, I get an exception: 但是如果我尝试在onAuthenticationSuccess方法中检索它,我会得到一个异常:
Error creating bean with name 'scopedTarget.sessionData': Scope 'session' is not active for the current thread;
创建名为'scopedTarget.sessionData'的bean时出错:范围'session'对当前线程无效;
My code is: 我的代码是:
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
SessionData sessionData = context.getBean(SessionData.class);
Any ideas? 有任何想法吗?
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>
By default that state is exposed by DispatcherServlet
, so it's not available before request enters DispatcherServlet
(eg in Spring Security filters). 默认情况下,
DispatcherServlet
公开该状态,因此在请求进入DispatcherServlet
之前它不可用(例如,在Spring Security过滤器中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.