Couchbase 5桶密码设置

时间:2017-10-28 22:10:36

标签: spring-boot spring-data couchbase spring-data-couchbase

我正在尝试编写样本以学习沙发基础。我试图将它与spring boot一起使用,它是crud存储库。

所以我已经下载了最新的docker镜像,但重点是:我找不到桶的密码。 couchbase控制台只允许用户创建,但在春天,没有像用户名/密码那样的用法。它只允许使用与couchbase 5不兼容的bucketName和密码。

我在这里遗漏了什么,或者春天与couchbase 5不兼容?如果spring不兼容,哪个版本的couchbase可以吗?

THX

3 个答案:

答案 0 :(得分:7)

Spring Data Couchbase与Couchbase Server 5.0兼容。您可以通过创建与存储桶同名的用户来实现与4.x相同的auth,然后只需使用Spring Data中的存储桶名称和密码(如果它在3.0 / Kay之前)。

文档应该涵盖这一点,如果有任何混淆,请点击"反馈"按钮并提供可以改进的内容!

https://developer.couchbase.com/documentation/server/5.0/security/security-authorization.html https://developer.couchbase.com/documentation/server/5.0/security/concepts-rba-for-apps.html https://developer.couchbase.com/documentation/server/5.0/security/security-resources-under-access-control.html

答案 1 :(得分:3)

我遇到了同样的问题。我开始通过进入AbstractCouchbaseConfiguration进行调试,然后找到了

public abstract class AbstractCouchbaseConfiguration
    extends AbstractCouchbaseDataConfiguration implements CouchbaseConfigurer {

  ....//some other configuration


@Override
@Bean(name = BeanNames.COUCHBASE_CLUSTER_INFO)
public ClusterInfo couchbaseClusterInfo() throws Exception {
  return couchbaseCluster().clusterManager(getBucketName(), getBucketPassword()).info();
}

我所做的是创建了一个与我的couchbase用户同名的存储桶。

couchbase用户名:userdetail

couchbase密码:******

存储桶名称:userdetail

答案 2 :(得分:2)

Couchbase驱动程序支持使用用户名/密码连接Couchbase 5存储桶。问题是spring-data-couchbase的开发速度不够快,无法涵盖Couchbase引入的所有新功能。因此,我们需要帮助Spring使用新的存储桶连接,通过覆盖spring-data-couchbase配置基类 - org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration的Couchbase集群实例化方法来实现。这是我们正在研究的方法:

@Override
@Bean(name = BeanNames.COUCHBASE_CLUSTER_INFO)
public ClusterInfo couchbaseClusterInfo() throws Exception {
return couchbaseCluster().clusterManager(getBucketName(), getBucketPassword()).info();
}

我们可以看到,它不是使用用户名,只是桶和密码,因此在我们的配置中,我们将覆盖它如下:

@Override
@Bean(name = BeanNames.COUCHBASE_CLUSTER_INFO)
public ClusterInfo couchbaseClusterInfo() throws Exception {
return couchbaseCluster().authenticate(couchbaseUsername, couchbasePassword).clusterManager().info();
}

就是这样。以下是我的spring-data-couchbase配置的完整代码:

import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.cluster.ClusterInfo;
import com.couchbase.client.java.env.CouchbaseEnvironment;
import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.config.BeanNames;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;

import javax.inject.Inject;
import java.security.KeyStore;
import java.util.List;

/**
 * @author by avoinovan
 */
@Configuration
@EnableCouchbaseRepositories
public class ModelConfig extends AbstractCouchbaseConfiguration {

    private final static int DEFAULT_HTTP_PORT = 8091;
    private final static int DEFAULT_HTTP_SSL_PORT = 18091;
    private final static int DEFAULT_CARRIER_PORT = 11210;
    private final static int DEFAULT_CARRIER_SSL_PORT = 11207;

    private final static long DEFAULT_KEEP_ALIVE_INTERVAL = 30000;
    private final static int DEFAULT_SOCKET_CONNECT_TIMEOUT_MS = 5000;
    private final static long DEFAULT_CONNECT_TIMEOUT_MS = 5000;
    private final static long DEFAULT_MANAGEMENT_TIMEOUT_MS = 75000;
    private final static long DEFAULT_DISCONNECT_TIMEOUT_MS = 25000;

    private final static String PROPERTY_KEEP_ALIVE_INTERVAL_MS = "couchbase.keep_alive_interval_ms";

    private final static String PROPERTY_SOCKET_CONNECT_TIMEOUT_MS = "couchbase.socket_connect_timeout_ms";
    private final static String PROPERTY_CONNECT_TIMEOUT_MS = "couchbase.connect_timeout_ms";
    private final static String PROPERTY_MANAGEMENT_TIMEOUT_MS = "couchbase.management_timeout_ms";
    private final static String PROPERTY_DISCONNECT_TIMEOUT_MS = "couchbase.disconnect_timeout_ms";

    private final static String PROPERTY_SSL_ENABLED = "couchbase.ssl.enabled";
    private final static String PROPERTY_SSL_KEYSTORE_FILE = "couchbase.ssl.keystore.file";
    private final static String PROPERTY_SSL_KEYSTORE_PASSWORD = "couchbase.ssl.keystore.password";
    private final static String PROPERTY_SSL_TRUSTSTORE_FILE = "couchbase.ssl.truststore.file";
    private final static String PROPERTY_SSL_TRUSTSTORE_PASSWORD = "couchbase.ssl.truststore.password";

    private final static String PROPERTY_BOOTSTRAP_HTTP_ENABLED = "couchbase.bootstrap.http.enabled";
    private final static String PROPERTY_BOOTSTRAP_HTTP_PORT = "couchbase.bootstrap.http.port";
    private final static String PROPERTY_BOOTSTRAP_HTTP_SSL_PORT = "couchbase.bootstrap.http.ssl.port";
    private final static String PROPERTY_BOOTSTRAP_CARRIER_ENABLED = "couchbase.bootstrap.carrier.enabled";
    private final static String PROPERTY_BOOTSTRAP_CARRIER_PORT = "couchbase.bootstrap.carrier.port";
    private final static String PROPERTY_BOOTSTRAP_CARRIER_SSL_PORT = "couchbase.bootstrap.carrier.ssl.port";

    @Value("#{'${spring.couchbase.bootstrap-hosts}'.split(',')}")
    private List<String> couchbaseBootstrapHosts;

    @Value("${spring.couchbase.bucket.name}")
    private String bucketName;

    @Value("${spring.couchbase.password}")
    private String couchbasePassword;

    @Value("${spring.couchbase.username}")
    private String couchbaseUsername;

    private final Environment environment;

    private final ResourceLoader resourceLoader;

    @Inject
    public ModelConfig(final Environment environment,
                       final ResourceLoader resourceLoader) {
        this.environment = environment;
        this.resourceLoader = resourceLoader;
    }

    protected List<String> getBootstrapHosts() {
        return couchbaseBootstrapHosts;
    }

    protected String getBucketName() {
        return bucketName;
    }

    protected String getBucketPassword() {
        return couchbasePassword;
    }

    protected CouchbaseEnvironment getEnvironment() {
        return DefaultCouchbaseEnvironment.builder()
                .keepAliveInterval(environment.getProperty(PROPERTY_KEEP_ALIVE_INTERVAL_MS,
                        Long.class,
                        DEFAULT_KEEP_ALIVE_INTERVAL))

                // timeout settings
                .socketConnectTimeout(environment.getProperty(PROPERTY_SOCKET_CONNECT_TIMEOUT_MS,
                        Integer.class,
                        DEFAULT_SOCKET_CONNECT_TIMEOUT_MS))
                .connectTimeout(environment.getProperty(PROPERTY_CONNECT_TIMEOUT_MS,
                        Long.class,
                        DEFAULT_CONNECT_TIMEOUT_MS))
                .managementTimeout(environment.getProperty(PROPERTY_MANAGEMENT_TIMEOUT_MS,
                        Long.class,
                        DEFAULT_MANAGEMENT_TIMEOUT_MS))
                .disconnectTimeout(environment.getProperty(PROPERTY_DISCONNECT_TIMEOUT_MS,
                        Long.class,
                        DEFAULT_DISCONNECT_TIMEOUT_MS))

                // port and ssl
                .sslEnabled(environment.getProperty(PROPERTY_SSL_ENABLED, Boolean.class, false))
                .bootstrapHttpEnabled(environment.getProperty(PROPERTY_BOOTSTRAP_HTTP_ENABLED,
                        Boolean.class,
                        Boolean.TRUE))
                .bootstrapHttpDirectPort(environment.getProperty(PROPERTY_BOOTSTRAP_HTTP_PORT,
                        Integer.class,
                        DEFAULT_HTTP_PORT))
                .bootstrapHttpSslPort(environment.getProperty(PROPERTY_BOOTSTRAP_HTTP_SSL_PORT,
                        Integer.class,
                        DEFAULT_HTTP_SSL_PORT))
                .bootstrapCarrierEnabled(environment.getProperty(PROPERTY_BOOTSTRAP_CARRIER_ENABLED,
                        Boolean.class,
                        Boolean.TRUE))
                .bootstrapCarrierDirectPort(environment.getProperty(PROPERTY_BOOTSTRAP_CARRIER_PORT,
                        Integer.class,
                        DEFAULT_CARRIER_PORT))
                .bootstrapCarrierSslPort(environment.getProperty(PROPERTY_BOOTSTRAP_CARRIER_SSL_PORT,
                        Integer.class,
                        DEFAULT_CARRIER_SSL_PORT))

                // keystore and trust store
                .sslKeystore(createKeyStore(environment, resourceLoader))
                .sslTruststore(createTrustStore(environment, resourceLoader))

                .build();
    }

    @Override
    @Bean(name = BeanNames.COUCHBASE_CLUSTER_INFO)
    public ClusterInfo couchbaseClusterInfo() throws Exception {
        return couchbaseCluster().authenticate(couchbaseUsername, couchbasePassword).clusterManager().info();
    }

    /**
     * Return the {@link Bucket} instance to connect to.
     *
     * @throws Exception on Bean construction failure.
     */
    @Override
    @Bean(destroyMethod = "close", name = BeanNames.COUCHBASE_BUCKET)
    public Bucket couchbaseClient() throws Exception {
        //@Bean method can use another @Bean method in the same @Configuration by directly invoking it
        return couchbaseCluster().openBucket(getBucketName());
    }

    private KeyStore createKeyStore(final Environment environment, final ResourceLoader resourceLoader) {
        return loadKeyStore(environment, resourceLoader, PROPERTY_SSL_KEYSTORE_FILE, PROPERTY_SSL_KEYSTORE_PASSWORD);
    }

    private KeyStore createTrustStore(final Environment environment, final ResourceLoader resourceLoader) {
        return loadKeyStore(environment, resourceLoader, PROPERTY_SSL_TRUSTSTORE_FILE, PROPERTY_SSL_TRUSTSTORE_PASSWORD);
    }

    private KeyStore loadKeyStore(final Environment environment,
                                  final ResourceLoader resourceLoader,
                                  final String fileProperty,
                                  final String passwordProperty) {
        String file = environment.getProperty(fileProperty);
        String password = environment.getProperty(passwordProperty);

        if (file != null) {
            Resource resource = resourceLoader.getResource(file);
            if (resource != null) {
                try {
                    KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
                    keyStore.load(resource.getInputStream(), password == null ? null : password.toCharArray());
                    return keyStore;
                } catch (final Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }
}