首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring-Security:认证后调用方法

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

Stack Overflow用户
提问于 2010-04-06 00:06:21
回答 6查看 31.3K关注 0票数 26

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

EN

回答 6

Stack Overflow用户

发布于 2012-08-31 20:10:51

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

代码语言:javascript
复制
<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"/>

并实现一个适当的类:

代码语言:javascript
复制
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配置链接资源。

票数 33
EN

Stack Overflow用户

发布于 2012-05-25 10:47:18

最好的方法是创建一个应用程序侦听器,并将其注册到spring安全上下文。

代码语言:javascript
复制
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent;

public class AuthenticationSuccessListener implements ApplicationListener<InteractiveAuthenticationSuccessEvent> {

    @Override
    public void onApplicationEvent(InteractiveAuthenticationSuccessEvent event) {
        System.out.println("User Logged In");

    }
}

确保将上述类spring-security.xml作为bean添加。还有许多其他类型的安全事件侦听器可以侦听,请检查类型层次结构,以获得您可以侦听的所有类型的安全事件的列表。

票数 14
EN

Stack Overflow用户

发布于 2013-08-01 14:58:48

如果您想继续使用默认行为,但只是在其间执行您自己的业务逻辑,那么您可以在返回之前使用extend SimpleUrlAuthenticationSuccessHandler并调用super.onAuthenticationSuccess(request, response, authentication);。更多详细信息请参阅https://stackoverflow.com/a/6770785/418439

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2579431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档