Spring Boot JWT身份验证:登录和注销后触发方法

时间:2019-02-25 15:30:23

标签: java spring spring-boot spring-security jwt

我正在使用Spring Boot实现REST API,并通过JWT和Oauth 2保护它。

我在身份验证和生成访问令牌方面没有问题。

现在,我想在用户获得身份验证后执行一个操作,并在访问令牌到期和/或用户注销时执行另一个不同的操作。

在Spring Boot中登录和注销/访问令牌到期后,如何触发方法?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

Spring Boot内置了对ApplicationEventApplicationListener的支持。如果您需要它们异步,则可以使用ApplicationEventMulticaster来“发出”事件,例如参见https://www.baeldung.com/spring-events

例如,注销后,您可以拥有applicationEventMulticaster.multicastEvent(new OnLogoutCustomEvent(param1, param2))(其中applicationEventMulticaster是自动连线的ApplicationEventMulticaster,而OnLogoutCustomEventApplicationEvent的扩展名),然后有一个LogoutCustomListener (实现ApplicationListener<OnLogoutCustomEvent>)来处理事件。

对于令牌超时,我认为您需要ScheduledExecutorService之类的东西来在到期时触发您的逻辑。