简体   繁体   中英

Spring Boot JWT Authentication: trigger a method after login and logout

I am implementing a REST API with Spring Boot and I am securing it with JWT and Oauth 2.

I have no problems with authentication and producing an access token.

Now, I want to perform an action after the user gets authenticated and another different action when the access token is expired and/or user logs out.

How can I trigger a method after login and logout/access token expiry in Spring Boot?

Any help would be appreciated.

Spring boot has built-in support for ApplicationEvent s and ApplicationListener s. If you need them to be async you can use ApplicationEventMulticaster to "emit" the event, eg see https://www.baeldung.com/spring-events .

For example, upon logout, you can have applicationEventMulticaster.multicastEvent(new OnLogoutCustomEvent(param1, param2)) (where applicationEventMulticaster is an autowired ApplicationEventMulticaster and OnLogoutCustomEvent extends ApplicationEvent ) and then have a LogoutCustomListener (implements ApplicationListener<OnLogoutCustomEvent> ) to handle the event.

For the token timeout I think you need something like ScheduledExecutorService to trigger your logic at the time of expiry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM