gpt4 book ai didi

java - Spring MVC 的 C# Session_OnStart 模拟

转载 作者:行者123 更新时间:2023-12-01 13:27:44 25 4
gpt4 key购买 nike

在我的Java应用程序(Spring MVC + Spring Security)中,我需要在授权后将每个用户的一些信息放入 session 中。在 C# 中,我使用 Session_OnStart事件Global.asax 。 Spring 中有一些类似的东西吗?

我尝试使用ApplicationListener<AuthenticationSuccessEvent> ,但出现错误:

Scope 'session' is not active for the current thread

最佳答案

您可以尝试在 session 范围 bean 中定义一个方法并将其标记为 @PostConstruct。当 bean 准备就绪时,将调用该方法。

更新或使用

public class MyLoginSuccessfullHandler extends SimpleUrlAuthenticationSuccessHandler {

@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
//your code
}

并定义

<beans:bean id="usernamePasswordAuthenticationFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">



<beans:property name="authenticationSuccessHandler" ref="yourSuccessHandlerBean"/>
</beans:bean>

关于java - Spring MVC 的 C# Session_OnStart 模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21722365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com