1
votes

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.

1
Maybe you can use a library like Guice to do the job. Emit an event the login/logout and handle it where you need.Oreste Viron

1 Answers

0
votes

Spring boot has built-in support for ApplicationEvents and ApplicationListeners. If you need them to be async you can use ApplicationEventMulticaster to "emit" the event, e.g. 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.