From 45aae9c1a89ec7e1044934b086aa3cb299338d00 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Mon, 14 Oct 2024 20:43:18 +0800 Subject: [PATCH] fix: preventing access to login page after Halo setup (#6865) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug /area core /milestone 2.20.x #### What this PR does / why we need it: 修复初始化 Halo 之后无法进入登录页面的问题 此问题原因是更改了 AuthProvider 的逻辑,当系统启动之后缺少默认的登录方式导致登录页面无法正确渲染 此 PR 将确保默认的登录方式始终存在来解决此问题 how to test it? 重新初始化 Halo 之后能正确渲染登录页面并登录即为符合预期 #### Does this PR introduce a user-facing change? ```release-note 修复初始化 Halo 之后无法进入登录页面的问题 ``` --- .../app/security/AuthProviderServiceImpl.java | 16 ++++++++++++---- .../system-configurable-configmap.yaml | 8 ++++++++ .../security/AuthProviderServiceImplTest.java | 16 +++++++++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/run/halo/app/security/AuthProviderServiceImpl.java b/application/src/main/java/run/halo/app/security/AuthProviderServiceImpl.java index b971b7b255..8cdd9124d4 100644 --- a/application/src/main/java/run/halo/app/security/AuthProviderServiceImpl.java +++ b/application/src/main/java/run/halo/app/security/AuthProviderServiceImpl.java @@ -13,6 +13,7 @@ import lombok.experimental.Accessors; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.dao.OptimisticLockingFailureException; import org.springframework.lang.NonNull; import org.springframework.security.core.context.ReactiveSecurityContextHolder; @@ -29,6 +30,7 @@ import run.halo.app.extension.MetadataUtil; import run.halo.app.extension.ReactiveExtensionClient; import run.halo.app.extension.index.query.QueryFactory; +import run.halo.app.infra.SystemConfigurableEnvironmentFetcher; import run.halo.app.infra.SystemSetting; import run.halo.app.infra.utils.JsonUtils; @@ -42,6 +44,7 @@ @RequiredArgsConstructor public class AuthProviderServiceImpl implements AuthProviderService { private final ReactiveExtensionClient client; + private final ObjectProvider environmentFetcherProvider; @Override public Mono enable(String name) { @@ -146,7 +149,7 @@ public int compareTo(@NonNull AuthProviderWithPriority o) { } private Mono> fetchProviderStates() { - return fetchSystemConfigMap() + return getSystemConfigMap() .map(AuthProviderServiceImpl::getAuthProviderConfig) .map(SystemSetting.AuthProvider::getStates) .defaultIfEmpty(List.of()) @@ -218,7 +221,7 @@ private static SystemSetting.AuthProvider getAuthProviderConfig(ConfigMap config } private Mono updateAuthProviderEnabled(String name, boolean enabled) { - return Mono.defer(() -> fetchSystemConfigMap() + return Mono.defer(() -> getSystemConfigMap() .flatMap(configMap -> { var providerConfig = getAuthProviderConfig(configMap); var stateToFoundOpt = providerConfig.getStates() @@ -244,7 +247,12 @@ private Mono updateAuthProviderEnabled(String name, boolean enabled) .filter(OptimisticLockingFailureException.class::isInstance)); } - Mono fetchSystemConfigMap() { - return client.fetch(ConfigMap.class, SystemSetting.SYSTEM_CONFIG); + private Mono getSystemConfigMap() { + var systemFetcher = environmentFetcherProvider.getIfUnique(); + if (systemFetcher == null) { + return Mono.error( + new IllegalStateException("No SystemConfigurableEnvironmentFetcher found")); + } + return systemFetcher.getConfigMap(); } } diff --git a/application/src/main/resources/extensions/system-configurable-configmap.yaml b/application/src/main/resources/extensions/system-configurable-configmap.yaml index ca914d254d..64c98e0965 100644 --- a/application/src/main/resources/extensions/system-configurable-configmap.yaml +++ b/application/src/main/resources/extensions/system-configurable-configmap.yaml @@ -51,3 +51,11 @@ data: { "search-engine": ["search-engine-lucene"] } + authProvider: | + { + "states": [{ + "name": "local", + "enabled": true, + "priority": 0 + }] + } diff --git a/application/src/test/java/run/halo/app/security/AuthProviderServiceImplTest.java b/application/src/test/java/run/halo/app/security/AuthProviderServiceImplTest.java index 6583e13c23..6260cc6860 100644 --- a/application/src/test/java/run/halo/app/security/AuthProviderServiceImplTest.java +++ b/application/src/test/java/run/halo/app/security/AuthProviderServiceImplTest.java @@ -10,12 +10,14 @@ import java.util.HashMap; import org.json.JSONException; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.skyscreamer.jsonassert.JSONAssert; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.data.domain.Sort; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit.jupiter.SpringExtension; @@ -28,6 +30,7 @@ import run.halo.app.extension.ListOptions; import run.halo.app.extension.Metadata; import run.halo.app.extension.ReactiveExtensionClient; +import run.halo.app.infra.SystemConfigurableEnvironmentFetcher; import run.halo.app.infra.SystemSetting; import run.halo.app.infra.utils.JsonUtils; @@ -43,9 +46,20 @@ class AuthProviderServiceImplTest { @Mock ReactiveExtensionClient client; + @Mock + ObjectProvider systemFetchProvider; + + @Mock + SystemConfigurableEnvironmentFetcher systemConfigFetcher; + @InjectMocks AuthProviderServiceImpl authProviderService; + @BeforeEach + void setUp() { + when(systemFetchProvider.getIfUnique()).thenReturn(systemConfigFetcher); + } + @Test void testEnable() throws JSONException { // Create a test auth provider @@ -199,7 +213,7 @@ AuthProvider createAuthProvider(String name) { void pileSystemConfigMap() { ConfigMap configMap = new ConfigMap(); configMap.setData(new HashMap<>()); - when(client.fetch(eq(ConfigMap.class), eq(SystemSetting.SYSTEM_CONFIG))) + when(systemConfigFetcher.getConfigMap()) .thenReturn(Mono.just(configMap)); } } \ No newline at end of file