[英]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.