[英]Spring Boot JWT Authentication: trigger a method after login and logout
我正在使用Spring Boot实现REST API,并通过JWT和Oauth 2保护它。
身份验证和生成访问令牌没有问题。
现在,我想在用户获得身份验证之后执行一个操作,并在访问令牌到期和/或用户注销时执行另一个不同的操作。
在Spring Boot中登录和注销/访问令牌到期后,如何触发方法?
任何帮助,将不胜感激。
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
以在到期时触发您的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.