开发者

How to retrieve a session-scoped bean inside AuthenticationSuccessHandler?

开发者 https://www.devze.com 2023-02-19 17:26 出处:网络
I have a custom AuthenticationSuccessHandler. What I want to do is to set some session data within onAuthenticationSuccess method.

I have a custom AuthenticationSuccessHandler.

What I want to do is to set some session data within onAuthenticationSuccess method.

To store session data I want to use a session-scoped bean, which works fine within any controller.

But if I try to开发者_如何学运维 retrieve it within onAuthenticationSuccess method, I get an exception:

Error creating bean with name 'scopedTarget.sessionData': Scope 'session' is not active for the current thread;

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 (e.g. in Spring Security filters).

0

精彩评论

暂无评论...
验证码 换一张
取 消