From ecd8fa9e7d62286efe1f4fc82cac46296c2743b1 Mon Sep 17 00:00:00 2001 From: onewe Date: Thu, 21 Apr 2022 10:15:46 +0800 Subject: [PATCH] [ISSUE #8214] Resolve CI error --- .../plugin/auth/impl/LdapAuthConfig.java | 1 - .../plugin/auth/impl/NacosAuthConfig.java | 34 ++++++++++++------- .../plugin/auth/impl/JwtTokenManagerTest.java | 10 ++++-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/LdapAuthConfig.java b/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/LdapAuthConfig.java index f284e673dc0..f5bb21f02c2 100644 --- a/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/LdapAuthConfig.java +++ b/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/LdapAuthConfig.java @@ -37,7 +37,6 @@ * @author onewe */ @Configuration -@Conditional(ConditionOnLdapAuth.class) @EnableAutoConfiguration(exclude = LdapAutoConfiguration.class) public class LdapAuthConfig { diff --git a/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/NacosAuthConfig.java b/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/NacosAuthConfig.java index 57d0b4a58cc..d314e7a943a 100644 --- a/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/NacosAuthConfig.java +++ b/plugin-default-impl/src/main/java/com/alibaba/nacos/plugin/auth/impl/NacosAuthConfig.java @@ -25,7 +25,7 @@ import com.alibaba.nacos.plugin.auth.impl.users.NacosUserDetailsServiceImpl; import io.jsonwebtoken.io.Decoders; import io.jsonwebtoken.io.DecodingException; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.context.annotation.Bean; import org.springframework.core.env.Environment; import org.springframework.security.authentication.AuthenticationManager; @@ -63,23 +63,17 @@ public class NacosAuthConfig extends WebSecurityConfigurerAdapter { private static final String PROPERTY_IGNORE_URLS = "nacos.security.ignore.urls"; - @Autowired - private Environment env; + private final Environment env; - @Autowired - private JwtTokenManager tokenProvider; + private final JwtTokenManager tokenProvider; - @Autowired - private AuthConfigs authConfigs; + private final AuthConfigs authConfigs; - @Autowired - private NacosUserDetailsServiceImpl userDetailsService; + private final NacosUserDetailsServiceImpl userDetailsService; - @Autowired - private LdapAuthenticationProvider ldapAuthenticationProvider; + private final LdapAuthenticationProvider ldapAuthenticationProvider; - @Autowired - private ControllerMethodsCache methodsCache; + private final ControllerMethodsCache methodsCache; /** * secret key. @@ -96,6 +90,20 @@ public class NacosAuthConfig extends WebSecurityConfigurerAdapter { */ private long tokenValidityInSeconds; + public NacosAuthConfig(Environment env, JwtTokenManager tokenProvider, AuthConfigs authConfigs, + NacosUserDetailsServiceImpl userDetailsService, + ObjectProvider ldapAuthenticationProvider, + ControllerMethodsCache methodsCache) { + + this.env = env; + this.tokenProvider = tokenProvider; + this.authConfigs = authConfigs; + this.userDetailsService = userDetailsService; + this.ldapAuthenticationProvider = ldapAuthenticationProvider.getIfAvailable(); + this.methodsCache = methodsCache; + + } + /** * Init. */ diff --git a/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/JwtTokenManagerTest.java b/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/JwtTokenManagerTest.java index 437d8f4980e..872e81d1a7d 100644 --- a/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/JwtTokenManagerTest.java +++ b/plugin-default-impl/src/test/java/com/alibaba/nacos/plugin/auth/impl/JwtTokenManagerTest.java @@ -24,6 +24,7 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.beans.factory.ObjectProvider; import java.lang.reflect.Field; import java.util.Properties; @@ -39,6 +40,9 @@ public class JwtTokenManagerTest { @Mock private ControllerMethodsCache methodsCache; + @Mock + private ObjectProvider ldapAuthenticationProvider; + private NacosAuthConfig nacosAuthConfig; @Test @@ -57,9 +61,9 @@ private void createToken(String secretKey) throws NoSuchFieldException, IllegalA properties.setProperty(AuthConstants.TOKEN_SECRET_KEY, secretKey); properties.setProperty(AuthConstants.TOKEN_EXPIRE_SECONDS, "300"); when(authConfigs.getAuthPluginProperties(AuthConstants.AUTH_PLUGIN_TYPE)).thenReturn(properties); - nacosAuthConfig = new NacosAuthConfig(); - injectProperty(nacosAuthConfig, "methodsCache", methodsCache); - injectProperty(nacosAuthConfig, "authConfigs", authConfigs); + + nacosAuthConfig = new NacosAuthConfig(null, null, authConfigs, null, + ldapAuthenticationProvider, methodsCache); nacosAuthConfig.init(); JwtTokenManager jwtTokenManager = new JwtTokenManager(); injectProperty(jwtTokenManager, "nacosAuthConfig", nacosAuthConfig);