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

主宰稳场 提交于 2019-12-12 09:27:27

问题


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.


回答1:


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.
        }
    }
}


来源:https://stackoverflow.com/questions/5668634/spring-security-at-which-point-do-i-get-to-know-that-a-user-logged-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!