Spring-Security: Call method after authentication

后端 未结 7 1329
一整个雨季
一整个雨季 2021-02-01 04:52

I\'d like to track when users are logging in to my application. I have some code that I would like to execute right after the user is authenticated. The problem is, I can\'t fig

相关标签:
7条回答
  • 2021-02-01 05:44

    Best way is to create an application listener and register it with the spring security context.

    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");
    
        }
    }
    

    Make sure to add the above class the spring-security.xml as a bean. There are lots of other types of security events listeners you can listen to, check the type hierarchy for a list of all the types of security events you can listen to.

    0 讨论(0)
提交回复
热议问题