Erik
Erik

Reputation: 12140

Spring Security: At which point do I get to know that a user logged in?

I am using spring security with URL based interceptors to secure my application. In which classes/at which points can I do some custom processing after a user logged in?

I specifically want to save the date the user logged in last, but I cannot figure out how to achieve this.

Thanks a lot for your help.

Upvotes: 0

Views: 317

Answers (1)

hooknc
hooknc

Reputation: 5001

You could consider implementing the org.springframework.context.ApplicationListener interface.

You would then listen specifically for the org.springframework.security.authentication.event.AuthenticationSuccessEvent.

You could then persist your user's login.

Possible example code:

public void onApplicationEvent(ApplicationEvent event) {

    if (event instanceof AuthenticationSuccessEvent) {

        try {

            AuthenticationSuccessEvent authenticationSuccessEvent = (AuthenticationSuccessEvent) event;

            Authentication authentication = authenticationSuccessEvent.getAuthentication();

            //Persist your user's login here.

        } catch (Exception e) {

            // Handle exception as needed.
        }
    }
}

Upvotes: 3

Related Questions