简体   繁体   中英

Spring cloud zookeeper Config - Set ACL for zookeeper

According to the documentation , I can add authentication information for Zookeeper ACLs by calling the addAuthInfo. But in the Curator Framework bean I don't find the method itself. It throws complilation issue !! .

My POM has

     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-config</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.8</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

How to add zookeeper auth info to Spring Cloud Zookeeper Config. Any working example would help me.

There is a github issue for this problem and it is still in the open status.

As long as this problem is solved by the spring cool team, you can create a custom curator config class and add authentication info to builder method of CuratorFrameworkFactory class:

    @BootstrapConfiguration
    @ConditionalOnZookeeperEnabled
    public class CustomCuratorFrameworkConfig {

        @Autowired(required = false)
        private EnsembleProvider ensembleProvider;

        @Bean
        @ConditionalOnMissingBean
        public ZookeeperProperties zookeeperProperties() {
            return new ZookeeperProperties();
        }

        @Bean
        @ConditionalOnMissingBean
        public CuratorFramework curatorFramework(RetryPolicy retryPolicy, ZookeeperProperties properties) throws Exception{
            // username and password of the ACL digest scheme
            String zkUsername = "user";
            String zkPassword = "password";

            CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
            if (this.ensembleProvider != null) {
                builder.ensembleProvider(this.ensembleProvider);
            } else {
                builder.connectString(properties.getConnectString());
            }

            builder.retryPolicy(retryPolicy);

                String authenticationString = zkUsername + ":" + zkPassword;
                builder.authorization("digest", authenticationString.getBytes())
                        .aclProvider(new ACLProvider() {
                            @Override
                            public List<ACL> getDefaultAcl() {
                                return ZooDefs.Ids.CREATOR_ALL_ACL;
                            }

                            @Override
                            public List<ACL> getAclForPath(String path) {
                                return ZooDefs.Ids.CREATOR_ALL_ACL;
                            }
                        });

            CuratorFramework curator =  builder.build();
            curator.start();
curator.blockUntilConnected(properties.getBlockUntilConnectedWait(), properties.getBlockUntilConnectedUnit());
            return curator;
        }

        @Bean
        @ConditionalOnMissingBean
        public RetryPolicy exponentialBackoffRetry(ZookeeperProperties properties) {
            return new ExponentialBackoffRetry(properties.getBaseSleepTimeMs(), properties.getMaxRetries(), properties.getMaxSleepMs());
        }

    }

And then continue like this spring document :

You can register configuration classes to run during this phase by annotating them with @BootstrapConfiguration and including them in a comma-separated list that you set as the value of the org.springframework.cloud.bootstrap.BootstrapConfiguration property in the resources/META-INF/spring.factories file

resources/META-INF/spring.factories

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
my.project.CustomCuratorFrameworkConfig

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM