diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java index 2cad70668e..28f14f89dc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseConfiguration.java @@ -57,7 +57,13 @@ public class CouchbaseConfiguration { @Bean @Primary public Cluster couchbaseCluster() { - return CouchbaseCluster.create(couchbaseEnvironment(), determineBootstrapHosts()); + CouchbaseCluster couchbaseCluster = CouchbaseCluster + .create(couchbaseEnvironment(), determineBootstrapHosts()); + if (isRoleBasedAccessControlEnabled()) { + return couchbaseCluster.authenticate(this.properties.getUsername(), + this.properties.getPassword()); + } + return couchbaseCluster; } /** @@ -79,10 +85,18 @@ public class CouchbaseConfiguration { @Bean @Primary public Bucket couchbaseClient() { + if (isRoleBasedAccessControlEnabled()) { + return couchbaseCluster().openBucket(this.properties.getBucket().getName()); + } return couchbaseCluster().openBucket(this.properties.getBucket().getName(), this.properties.getBucket().getPassword()); } + private boolean isRoleBasedAccessControlEnabled() { + return this.properties.getUsername() != null + && this.properties.getPassword() != null; + } + /** * Initialize an environment builder based on the specified settings. * @param properties the couchbase properties to use diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java index 2fbfa8e8c2..e5aa85ee29 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/couchbase/CouchbaseProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,6 +38,16 @@ public class CouchbaseProperties { */ private List bootstrapHosts; + /** + * Cluster username when using role based access. + */ + private String username; + + /** + * Cluster password when using role based access. + */ + private String password; + private final Bucket bucket = new Bucket(); private final Env env = new Env(); @@ -50,6 +60,22 @@ public class CouchbaseProperties { this.bootstrapHosts = bootstrapHosts; } + public String getUsername() { + return this.username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + public Bucket getBucket() { return this.bucket; }