gpt4 book ai didi

java - Spring-Security:认证后调用方法

转载 作者:IT老高 更新时间:2023-10-28 20:43:51 25 4
gpt4 key购买 nike

我想跟踪用户何时登录我的应用程序。我有一些代码想在用户通过身份验证后立即执行。问题是,我不知道应该在哪里调用它。 spring-security 有办法在认证后调用方法吗?

最佳答案

可能对某人有用...在 Spring 3 的情况下,配置安全性:

<security:http use-expressions="true" auto-config="true">
<security:intercept-url pattern="..."/>
<security:form-login
authentication-failure-handler-ref="authFailureHandler"
authentication-success-handler-ref="authSuccessHandler"/>
<security:logout success-handler-ref="logoutSuccessHandler"
invalidate-session="true"/>
<security:session-management session-fixation-protection="newSession"/>
</security:http>

<bean id="authFailureHandler" class="mine.AuthenticationFailureHandlerImpl"/>
<bean id="authSuccessHandler" class="mine.AuthenticationSuccessHandlerImpl"/>
<bean id="logoutSuccessHandler" class="mine.LogoutSuccessHandlerImpl"/>

并实现一个适当的类:

public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//do what you want with
response.getOutputStream().write("success".getBytes());
}
}

您可以通过该 xml 配置链接资源。

关于java - Spring-Security:认证后调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2579431/

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