How to retrieve a session-scoped bean inside AuthenticationSuccessHandler?

泪湿孤枕 提交于 2019-12-10 07:41:46

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/5420161/how-to-retrieve-a-session-scoped-bean-inside-authenticationsuccesshandler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!