Java 在SpringSecurity中AuthenticationProvider成功后执行某些操作

Java 在SpringSecurity中AuthenticationProvider成功后执行某些操作,java,spring-security,Java,Spring Security,我有多个身份验证提供程序(MyOwn、Kerberos、Local)。认证成功后,我想用密码保存一些信息。但是每个提供者后面的代码都是不同的。因此,我希望在authenticationProvider成功后立即运行此代码。我该怎么做 若我使用AuthenticationSuccessHandler,它将在提供者的任何成功后运行。如果我在CustomUserDetails服务中编写代码,我将无法访问其中的密码信息。您可以注册一个侦听器,用于AuthenticationSuccessEvent Pr

我有多个身份验证提供程序(MyOwn、Kerberos、Local)。认证成功后,我想用密码保存一些信息。但是每个提供者后面的代码都是不同的。因此,我希望在authenticationProvider成功后立即运行此代码。我该怎么做


若我使用AuthenticationSuccessHandler,它将在提供者的任何成功后运行。如果我在CustomUserDetails服务中编写代码,我将无法访问其中的密码信息。

您可以注册一个侦听器,用于
AuthenticationSuccessEvent

ProviderManager
将身份验证委托给其注册的每个
AuthenticationProvider
。其中一个
AuthenticationProvider
成功验证后,
ProviderManager
将通过其
AuthenticationEventPublisher
发布
AuthenticationSuccessEvent

如果您希望接收此事件并获得对
身份验证的访问权
,以下Java配置将在该事件的上下文中注册
ApplicationListener
bean:

@Bean
public ApplicationListener<AuthenticationSuccessEvent> authenticationSuccessEventListener() {
    return new ApplicationListener<AuthenticationSuccessEvent>() {

        @Override
        void onApplicationEvent(AuthenticationSuccessEvent event) {
                Authentication authentication = event.getAuthentication();
                // TODO
        }
    };
}
@Bean
公共应用程序侦听器: