问题描述
我想在用户身份验证成功后将对象添加到HttpSession
.请不要使用SavedRequestAwareAuthenticationSuccessHandler
建议解决方案,因为在此应用中,由于某种原因,应用会忽略原始请求.
I want to add object to HttpSession
after successful user authentication. Please don't suggest solution with SavedRequestAwareAuthenticationSuccessHandler
because in this app for some reason application are ingnoring original request.
public class AuthenticationSuccessListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
@Override
public void onApplicationEvent(InteractiveAuthenticationSuccessEvent e) {
//adding object to HttpSession
}
}
推荐答案
据我所知,ApplicationListener
实例只是ApplicationContext
中的bean.因此,您应该能够向其中注入其他bean或资源.
As far as I am aware, ApplicationListener
instances are just beans within your ApplicationContext
. Therefore you should be able to inject other beans or resources into them.
因此要获得对当前HttpSession
实例的引用:
So to get a reference to the current HttpSession
instance:
public class AuthenticationSuccessListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {
@Autowired
private HttpSession httpSession;
@Override
public void onApplicationEvent(InteractiveAuthenticationSuccessEvent e) {
//adding object to HttpSession
}
}
Spring将使用其HttpSession -other-injection"rel =" noreferrer>作用域代理机制,确保您获得与当前执行线程相关的HTTPSession
.
Spring will inject the HttpSession
using its scoped proxy mechanism ensuring that you get the HTTPSession
relevant to the current thread of execution.
您还需要确保在web.xml中注册一个RequestContextListener
,以便Spring可以注入当前的HTTPSession
.
You'll also need to ensure that you register a RequestContextListener
in your web.xml so that Spring can inject the current HTTPSession
.
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
这篇关于如何从ApplicationListener方法获取会话对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!