How to get session bean inside AuthenticationSuccessHandler?

I have a custom AuthenticationSuccessHandler.

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

To store session data, I want to use a bean for a session that works fine in any controller.

But if I try to get it in the onAuthenticationSuccess method, I get an exception:

Error creating bean named 'scopedTarget.sessionData': The scope of the "session" is inactive for the current thread;

My code is:

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

Any ideas?

+4
source share
1 answer

You can try to declare a listener that provides the state necessary to implement the session scope:

 <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> 

By default, this state is displayed by the DispatcherServlet , so it is not available until the request enters the DispatcherServlet (for example, Spring Security Filters).

+6
source

Source: https://habr.com/ru/post/1345090/


All Articles