我正在使用Spring Boot实现REST API,并通过JWT和Oauth 2保护它。
我在身份验证和生成访问令牌方面没有问题。
现在,我想在用户获得身份验证后执行一个操作,并在访问令牌到期和/或用户注销时执行另一个不同的操作。
在Spring Boot中登录和注销/访问令牌到期后,如何触发方法?
任何帮助将不胜感激。
答案 0 :(得分:0)
Spring Boot内置了对ApplicationEvent
和ApplicationListener
的支持。如果您需要它们异步,则可以使用ApplicationEventMulticaster
来“发出”事件,例如参见https://www.baeldung.com/spring-events。
例如,注销后,您可以拥有applicationEventMulticaster.multicastEvent(new OnLogoutCustomEvent(param1, param2))
(其中applicationEventMulticaster
是自动连线的ApplicationEventMulticaster
,而OnLogoutCustomEvent
是ApplicationEvent
的扩展名),然后有一个LogoutCustomListener (实现ApplicationListener<OnLogoutCustomEvent>
)来处理事件。
对于令牌超时,我认为您需要ScheduledExecutorService
之类的东西来在到期时触发您的逻辑。