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.
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.
}
}
}
精彩评论