Springcloud+Nacos支持账号密码登录

在Springcloud官方,spring-cloud-alibaba目前的最新版本是2.2.0.RELEASE,如下

            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.2.0.RELEASE
                pom
                import
            

该版本支持的nacos-client版本是1.1.4,如下:

    com.alibaba.cloud
    spring-cloud-alibaba-dependencies
    2.2.0.RELEASE
    pom
    Spring Cloud Alibaba Dependencies
    Spring Cloud Alibaba Dependencies

    
        1.7.1
        3.1.0
        1.0.0
        1.1.4
        0.8.0
        1.0.9

但是,该版本的nacos-client不支持账号密码登录到nacos服务端。
那我们想要支持client端账号密码登录该如何做呢?

  • 1,先升级nacos-client版本到最新:

            
                com.alibaba.nacos
                nacos-client
                1.2.0
            
  • 2,扩展NacosConfigProperties支持账号密码

public class MyNacosConfigProperties extends NacosConfigProperties {
    private static final Pattern PATTERN = Pattern.compile("-(\\w)");
    
    private String username;
    private String password;
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    @Override
    public Properties assembleConfigServiceProperties() {
        Properties properties = super.assembleConfigServiceProperties();
        
        // 把账号添加到properties环境中
        properties.put(USERNAME, Objects.toString(this.username, ""));
        properties.put(PASSWORD, Objects.toString(this.password, ""));

        enrichNacosConfigProperties(properties);
        
        return properties;
    }
    
    private void enrichNacosConfigProperties(Properties nacosConfigProperties) {
        Map properties = PropertySourcesUtils
                .getSubProperties((ConfigurableEnvironment) getEnvironment(), PREFIX);
        properties.forEach((k, v) -> nacosConfigProperties.putIfAbsent(resolveKey(k),
                String.valueOf(v)));
    }
    
    private String resolveKey(String key) {
        Matcher matcher = PATTERN.matcher(key);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}

注意:这里重点是enrichNacosConfigProperties()方法;并且注意与NacosConfigProperties 的实例化bean的顺序。

  • 3,注册一个MyNacosConfigProperties类型的bean

@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
@AutoConfigureAfter(NacosConfigProperties.class)
public class NacosConfigConfig {
    @Primary
    @Bean
         @ConfigurationProperties(NacosConfigProperties.PREFIX)
    public MyNacosConfigProperties myNacosConfigProperties(ApplicationContext context) {
        if (context.getParent() != null
                && BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
                        context.getParent(), MyNacosConfigProperties.class).length > 0) {
            return BeanFactoryUtils.beanOfTypeIncludingAncestors(context.getParent(),
                    MyNacosConfigProperties.class);
        }
        return new MyNacosConfigProperties();
    }
}
  • 4,修改配置文件,增加username和password配置

spring: 
  cloud: 
    nacos: 
      config: 
        enabled: true
        server-addr: 192.168.1.10:8848
        file-extension: yaml
        namespace: tenantA
        group: APPA_GROUP
        data-id: appA_A
        refresh.enabled: true
        prefix: appA_A
        encode: UTF-8
        timeout: 10000
        context-path: /nacos
        username: nacos
        password: nacos

现在client端可以支持username和password登录到nacos服务端了。

你可能感兴趣的:(Springcloud+Nacos支持账号密码登录)