美文网首页
Springcloud+Nacos支持账号密码登录

Springcloud+Nacos支持账号密码登录

作者: 一曲畔上 | 来源:发表于2020-03-31 14:25 被阅读0次

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

                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.2.0.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
    

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

        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>2.2.0.RELEASE</version>
        <packaging>pom</packaging>
        <name>Spring Cloud Alibaba Dependencies</name>
        <description>Spring Cloud Alibaba Dependencies</description>
    
        <properties>
            <sentinel.version>1.7.1</sentinel.version>
            <oss.version>3.1.0</oss.version>
            <seata.version>1.0.0</seata.version>
            <nacos.client.version>1.1.4</nacos.client.version>
            <nacos.config.version>0.8.0</nacos.config.version>
            <acm.version>1.0.9</acm.version>
    

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

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

                <dependency>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                    <version>1.2.0</version>
                </dependency>
    
    • 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<String, Object> 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支持账号密码登录

          本文链接:https://www.haomeiwen.com/subject/cxuwuhtx.html