Spring Security2.0 登录(登出)成功 后置处理

               由于目前项目中使用了Spring Security 2.0.1 权限管理框架,登入成功后可能就需要处理些登录日志等等后续操作,Security 3之后只需要在form-login中添加authentication-success-handler-ref即可,一般是authentication-success-handler实现AuthenticationSuccessHandler的onAuthenticationSuccess方法。那在2.0下应该怎么做呢?

              关键代码 ProviderManager 类中 在 doAuthentication 授权成功的时候,创建了授权成功事件。 触发登录成功后置业务 就是 监听该事件并做相关操作。

            if (result != null) {
                sessionController.registerSuccessfulAuthentication(result);
                publishEvent(new AuthenticationSuccessEvent(result));

                return result;
            }
public abstract class AbstractAuthenticationEvent extends ApplicationEvent {
    //~ Constructors ===================================================================================================

    public AbstractAuthenticationEvent(Authentication authentication) {
        super(authentication);
    }

    //~ Methods ========================================================================================================

    /**
     * Getters for the <code>Authentication</code> request that caused the event. Also available from
     * <code>super.getSource()</code>.
     *
     * @return the authentication request
     */
    public Authentication getAuthentication() {
        return (Authentication) super.getSource();
    }
}

              由上面源代码实现授权成功事件监听器 :

public class LoginSuccessListener implements ApplicationListener {  
  
    public void onApplicationEvent(ApplicationEvent event) {  
        if (event instanceof AuthenticationSuccessEvent) {  
            AuthenticationSuccessEvent authEvent = (AuthenticationSuccessEvent) event;  
            UserDetails user = (UserDetails) authEvent.getAuthentication().getPrincipal();  
            System.out.println("模拟输出用户登录日志:[" + java.util.Calendar.getInstance().getTime() + "] " + user.getUsername());  
        }  
    }  
}  

在Spring中加入 监听器:<bean class="LoginSuccessListener"></bean> 

登出就只需要配置下就可以

<logout logout-url="/j_logout" logout-success-url="/login/login.html"/>

主要类:org.springframework.security.ui.logout.LogoutFilter、org.springframework.security.config.LogoutBeanDefinitionParser

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值