From a36822c861413e3fbe163a7376c2b957f5c75d9f Mon Sep 17 00:00:00 2001 From: John Niang Date: Thu, 12 Sep 2024 10:34:20 +0800 Subject: [PATCH] Expose CryptoService and RateLimiterRegistry to plugins (#6638) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind feature /area core /area plugin /milestone 2.20.x #### What this PR does / why we need it: Currently, we are refactoring login and logout pages to make them extensible. If plugins want to realize a new authentication method, the CryptoService and RateLimiterRegistry may be used to authenticate. So this PR exposes the two beans to plugins. No side effect will be introduced. #### Does this PR introduce a user-facing change? ```release-note 【开发相关】允许在插件使用 CryptoService 和 RateLimiterRegistry ``` --- .../app/security/authentication/CryptoService.java | 0 .../app/plugin/SharedApplicationContextFactory.java | 10 ++++++++++ 2 files changed, 10 insertions(+) rename {application => api}/src/main/java/run/halo/app/security/authentication/CryptoService.java (100%) diff --git a/application/src/main/java/run/halo/app/security/authentication/CryptoService.java b/api/src/main/java/run/halo/app/security/authentication/CryptoService.java similarity index 100% rename from application/src/main/java/run/halo/app/security/authentication/CryptoService.java rename to api/src/main/java/run/halo/app/security/authentication/CryptoService.java diff --git a/application/src/main/java/run/halo/app/plugin/SharedApplicationContextFactory.java b/application/src/main/java/run/halo/app/plugin/SharedApplicationContextFactory.java index 90b146114..c0236917b 100644 --- a/application/src/main/java/run/halo/app/plugin/SharedApplicationContextFactory.java +++ b/application/src/main/java/run/halo/app/plugin/SharedApplicationContextFactory.java @@ -1,5 +1,6 @@ package run.halo.app.plugin; +import io.github.resilience4j.ratelimiter.RateLimiterRegistry; import org.springframework.cache.CacheManager; import org.springframework.context.ApplicationContext; import org.springframework.context.support.GenericApplicationContext; @@ -16,6 +17,7 @@ import run.halo.app.notification.NotificationCenter; import run.halo.app.notification.NotificationReasonEmitter; import run.halo.app.plugin.extensionpoint.ExtensionGetter; import run.halo.app.security.LoginHandlerEnhancer; +import run.halo.app.security.authentication.CryptoService; /** * Utility for creating shared application context. @@ -69,6 +71,14 @@ public enum SharedApplicationContextFactory { ); beanFactory.registerSingleton("extensionGetter", rootContext.getBean(ExtensionGetter.class)); + rootContext.getBeanProvider(CryptoService.class) + .ifUnique( + cryptoService -> beanFactory.registerSingleton("cryptoService", cryptoService) + ); + rootContext.getBeanProvider(RateLimiterRegistry.class) + .ifUnique(rateLimiterRegistry -> + beanFactory.registerSingleton("rateLimiterRegistry", rateLimiterRegistry) + ); // TODO add more shared instance here sharedContext.refresh();