如何使用spring-data couchbase设置存储桶密码

时间:2015-06-09 16:23:01

标签: couchbase spring-data-couchbase

我已经遵循了spring-data couchbase的教程,并且有一个成功的示例项目,其中包含单元测试,用于持久化一系列自定义实体,并实现了一系列查看实体的视图。

当使用“默认”存储桶名称且没有密码作为身份验证时,这在本地开发环境和ci环境中都能正常工作。

超越示例,我想使用不同的存储桶并最终使用密码。

当我创建一个新的存储桶(名为“test_bucket”),并更新注入CouchbaseConfig(扩展AbstractCouchbaseConfiguration)的属性以使用这个新存储桶取代“默认”时,运行单元测试时会出现以下异常。

我还尝试在创建脚本中添加密码,并在CouchbaseConfig中使用的属性中添加相同的密码(两种情况下都为“psswd”字符串),但获得的信息如下所示。

因此可以使用另一个桶而不是“默认”(并且需要其无需授权),如何配置密码以便在此存储桶上使用?

我已验证已从管理GUI中在couchbase中正确创建了存储桶和预期视图。

2015-06-09 16:41:40 INFO  ClasspathLoggingApplicationListener:55 - Application failed to start with classpath: [file:/C:/tools/cmd/cygwin64/home/akirby/workspaces/repos/blackjack/persistence/target/surefire/surefirebooter7615727324811258159.jar]
2015-06-09 16:41:40 INFO  AutoConfigurationReportLoggingInitializer:107 -

Error starting ApplicationContext. To display the auto-configuration report enabled debug logging (start with --debug)

2015-06-09 16:41:40 ERROR SpringApplication:338 - Application startup failed
java.lang.NoSuchMethodError:     org.apache.commons.codec.binary.Base64.encodeBase64String([B)Ljava/lang/String;
    at com.couchbase.client.http.HttpUtil.buildAuthHeader(HttpUtil.java:55)
    at com.couchbase.client.ViewConnection.addOp(ViewConnection.java:205)
    at com.couchbase.client.CouchbaseClient.addOp(CouchbaseClient.java:803)
    at com.couchbase.client.CouchbaseClient.asyncGetView(CouchbaseClient.java:342)
    at com.couchbase.client.CouchbaseClient.getView(CouchbaseClient.java:430)
    at org.springframework.data.couchbase.core.CouchbaseTemplate$2.doInBucket(CouchbaseTemplate.java:223)
    at org.springframework.data.couchbase.core.CouchbaseTemplate$2.doInBucket(CouchbaseTemplate.java:220)
    at org.springframework.data.couchbase.core.CouchbaseTemplate.execute(CouchbaseTemplate.java:244)
    at org.springframework.data.couchbase.core.CouchbaseTemplate.queryView(CouchbaseTemplate.java:220)
    at org.springframework.data.couchbase.repository.support.SimpleCouchbaseRepository.deleteAll(SimpleCouchbaseRepository.java:168)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:416)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:401)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:373)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$DefaultMethodInvokingMethodInterceptor.invoke(RepositoryFactorySupport.java:486)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.couchbase.repository.support.ViewPostProcessor$ViewInterceptor.invoke(ViewPostProcessor.java:87)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy50.deleteAll(Unknown Source)
    at com.pubtech.cms.persistence.RepositoryService.doWork(RepositoryService.java:47)
    at com.pubtech.cms.persistence.ApplicationRepository.lambda$commandLineRunner$0(ApplicationRepository.java:83)
    at com.pubtech.cms.persistence.ApplicationRepository$$Lambda$9/594916129.run(Unknown Source)
    at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:672)
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:101)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:68)
    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86)
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:170)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:110)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
2015-06-09 16:41:40 INFO  GenericWebApplicationContext:862 - Closing org.springframework.web.context.support.GenericWebApplicationContext@6302bbb1: startup date [Tue Jun 09 16:41:33 BST 2015]; root of context hierarchy
2015-06-09 16:41:40 INFO  CouchbaseConnection:87 - Shut down Couchbase client
2015-06-09 16:41:40 INFO  ViewConnection:87 - I/O reactor terminated

当使用需要密码的存储桶名称(存储桶“t1”,密码“pswd”)时,我在日志中看到此验证错误,是否有某种格式。除了纯文本,密码应该用?编码?

2015-06-10 10:55:58 INFO  DefaultListableBeanFactory:822 - Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-06-10 10:55:59 INFO  Version:27 - HV000001: Hibernate Validator 5.1.3.Final
2015-06-10 10:56:00 ERROR SASLStepOperationImpl:93 - Error:  Auth failure
2015-06-10 10:56:00 WARN  BinaryMemcachedNodeImpl:90 - Discarding partially completed op: SASL steps operation
2015-06-10 10:56:00 WARN  AuthThread:90 - Authentication failed to localhost/127.0.0.1:11210, Status: {OperationStatus success=false:  cancelled}
2015-06-10 10:56:02 WARN  AuthThread:90 - Authentication failed to localhost/127.0.0.1:11210, Status: {OperationStatus success=false:  Invalid arguments}
2015-06-10 10:56:02 WARN  AuthThread:90 - Authentication failed to localhost/127.0.0.1:11210, Status: {OperationStatus success=false:  Invalid arguments}

我使用couchbase-cli从脚本创建存储桶,使用相同的脚本创建工作“默认”而不工作“test_bucket”,(使用mvn过滤器正确注入属性):

# Create Bucket
couchbase-cli bucket-create -c $COUCHBASE_HOST:$COUCHBASE_PORT -u $CB_REST_USERNAME -p $CB_REST_PASSWORD \
   --bucket=$BUCKET_NAME \
   --bucket-type=couchbase \
   --bucket-ramsize=200 \
   --bucket-replica=1 \
   --wait

CouchbaseConfig类:

..
@Configuration
@EnableCouchbaseRepositories(basePackages = {"com.persistence.db"})
@EnableAutoConfiguration
public class CouchbaseConfig extends AbstractCouchbaseConfiguration {

@Value("${couchbase.bucket:boris}")
private String bucketName;

@Value("${couchbase.bucket.password:nopwd}")
private String password;

@Value("${couchbase.host:127.0.0.1}")
private String ip;

..

1 个答案:

答案 0 :(得分:0)

我认为你遇到的问题与我遇到的问题类似,我在@Configuration类中使用@Value的问题有一些特殊要求。如果重要的话,我正在使用YAML作为我的属性文件。

将此添加到您的班级(也必须是静态的)

/**
 * this is required for some reason: https://jira.spring.io/browse/SPR-11773
 * 
 * @return
 */
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();