使用Hibernate OGM进行MongoDb身份validation

我可以使用shell命令在我的mongodb上进行身份validation:

#mongo -u user -p pwd --authenticationDatabase admin MongoDB shell version v3.4.1 connecting to: mongodb://127.0.0.1:27017 MongoDB server version: 3.4.1 > use admin switched to db admin > show users { "_id" : "admin.ladmin", "user" : "ladmin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } { "_id" : "admin.living", "user" : "user", "db" : "admin", "roles" : [ { "role" : "readWrite", "db" : "lvdb" } ] } 

我也可以使用java驱动程序对它进行身份validation:

 List seeds = new ArrayList(); seeds.add(new ServerAddress(this.configurationResources.getMongodbServer(), this.configurationResources.getMongodbPort())); List credentials = new ArrayList(); credentials.add( MongoCredential.createScramSha1Credential( this.configurationResources.getMongodbUsername(), this.configurationResources.getMongodbAuthenticationDatabase(), this.configurationResources.getMongodbPassword().toCharArray() ) ); this.mongoClient = new MongoClient(seeds, credentials); 

目前,我正在参与一个我想使用Hibernate OGM的项目。 我设置了persistence.xml文件:

   org.hibernate.ogm.jpa.HibernateOgmPersistence com.living.persistence.entities.User                  

如您所见,我正在使用SCRAM-SHA1作为身份validation机制。

然而,当我尝试部署我的应用程序时,我收到此消息:

引起:org.hibernate.service.spi.ServiceException:OGM000071:无法启动数据提供程序引起:org.hibernate.HibernateException:OGM001214:无法连接到MongoDB实例:在等待匹配的服务器30000毫秒后超时ReadPreferenceServerSelector {readPreference =初级}。 集群状态的客户端视图是{type = UNKNOWN,servers = [{address = mongo:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSecurityException:Exception authenticating MongoCredential {mechanism = SCRAM-SHA-1, userName =’user’,source =’lvdb’,password =,mechanismProperties = {}}},由{com.mongodb.MongoCommandException引起:命令失败,错误18:’身份validation失败。’ 在服务器上mongo:27017。 完整响应为{\“ok \”:0.0,\“errmsg \”:\“身份validation失败。\”,\“code \”:18,\“codeName \”:\“AuthenticationFailed \”}}}]引起:com.mongodb.MongoTimeoutException:在等待与ReadPreferenceServerSelector {readPreference = primary}匹配的服务器后30000 ms后超时。 集群状态的客户端视图是{type = UNKNOWN,servers = [{address = mongo:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSecurityException:Exception authenticating MongoCredential {mechanism = SCRAM-SHA-1, userName =’user’,source =’lvdb’,password =,mechanismProperties = {}}},由{com.mongodb.MongoCommandException引起:命令失败,错误18:’身份validation失败。’ 在服务器上mongo:27017。 完整响应为{\“ok \”:0.0,\“errmsg \”:\“身份validation失败。\”,\“code \”:18,\“codeName \”:\“AuthenticationFailed \”}}}] “}}

Hibernate OGM目前使用数据库名称作为身份validation数据库。 这是一个错误,我现在正在努力。

在您的示例中(顺便说一下似乎都是正确的),您希望连接到“lvdb”数据库,但是您在“admin”数据库中定义了用户。 Hiebernate OGM实际上是在“lvdb”数据库中寻找用户。

更新 :此问题现已在最新的稳定版本(5.1.0.Final)中得到修复,您可以使用属性hibernate.ogm.mongodb.authentication_database来选择身份validation数据库的名称( admin是默认名称)。