我可以使用shell命令在我的mongodb上进行身份验证:
#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驱动程序对其进行身份验证:
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add(new ServerAddress(this.configurationResources.getMongodbServer(), this.configurationResources.getMongodbPort()));
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(
MongoCredential.createScramSha1Credential(
this.configurationResources.getMongodbUsername(),
this.configurationResources.getMongodbAuthenticationDatabase(),
this.configurationResources.getMongodbPassword().toCharArray()
)
);
this.mongoClient = new MongoClient(seeds, credentials);
目前,我正在参与一个我想使用Hibernate OGM的项目。我已设置persistence.xml
文件:
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="mongo" transaction-type="JTA">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>com.living.persistence.entities.User</class>
<properties>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
<property name="hibernate.ogm.datastore.provider" value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/>
<property name="hibernate.ogm.datastore.database" value="lvdb"/>
<property name="hibernate.ogm.datastore.host" value="mongo"/>
<property name="hibernate.ogm.datastore.port" value="27017"/>
<property name="hibernate.ogm.datastore.username" value="user"/>
<property name="hibernate.ogm.datastore.password" value="pwd"/>
<property name="hibernate.ogm.mongodb.authentication_mechanism" value="SCRAM_SHA_1"/>
<property name="hibernate.ogm.mongodb.connection_timeout" value="5000"></property>
<property name="hibernate.ogm.datastore.document.association_storage" value="IN_ENTITY"></property>
<property name="hibernate.ogm.mongodb.association_document_storage" value="GLOBAL_COLLECTION"></property>
<property name="hibernate.ogm.mongodb.write_concern" value="MAJORITY"></property>
<property name="hibernate.ogm.mongodb.read_preference" value="PRIMARY_PREFERRED"></property>
</properties>
</persistence-unit>
</persistence>
正如您所见,我使用SCRAM-SHA1
作为身份验证机制。
然而,当我尝试部署我的应用程序时,我收到此消息:
引起:org.hibernate.service.spi.ServiceException:OGM000071:无法启动数据提供程序 引起:org.hibernate.HibernateException:OGM001214:无法连接到MongoDB实例:在等待与ReadPreferenceServerSelector {readPreference = primary}匹配的服务器30000 ms后超时。簇状态的客户端视图是{式=未知,服务器= [{地址=蒙戈:27017,类型=未知,状态= CONNECTING,异常= {com.mongodb.MongoSecurityException:异常认证MongoCredential {机制= SCRAM-SHA-1, userName =&#39; user&#39;,source =&#39; lvdb&#39;,password =,mechanismProperties = {}}},由{com.mongodb.MongoCommandException引起:命令失败,错误18:&#39 ;身份验证失败。&#39;在服务器上mongo:27017。完整的回复是{\&#34; ok \&#34; :0.0,\&#34; errmsg \&#34; :\&#34;身份验证失败。\&#34;,\&#34;代码\&#34; :18,\&#34; codeName \&#34; :\&#34; AuthenticationFailed \&#34; }}}] 引发者:com.mongodb.MongoTimeoutException:在等待与ReadPreferenceServerSelector {readPreference = primary}匹配的服务器30000 ms后超时。簇状态的客户端视图是{式=未知,服务器= [{地址=蒙戈:27017,类型=未知,状态= CONNECTING,异常= {com.mongodb.MongoSecurityException:异常认证MongoCredential {机制= SCRAM-SHA-1, userName =&#39; user&#39;,source =&#39; lvdb&#39;,password =,mechanismProperties = {}}},由{com.mongodb.MongoCommandException引起:命令失败,错误18:&#39 ;身份验证失败。&#39;在服务器上mongo:27017。完整的回复是{\&#34; ok \&#34; :0.0,\&#34; errmsg \&#34; :\&#34;身份验证失败。\&#34;,\&#34;代码\&#34; :18,\&#34; codeName \&#34; :\&#34; AuthenticationFailed \&#34; }}}]&#34;}}
答案 0 :(得分:2)
Hibernate OGM目前正在使用数据库名称作为身份验证数据库。这是一个错误,我现在正在研究它。
在你的例子中(顺便说一句似乎都是正确的),你想要连接到 &#34; lvdb&#34; db但您在&#34; admin&#34;中定义了用户。数据库。 Hiebernate OGM实际上正在寻找&#34; lvdb&#34;数据库中。
UPDATE :此问题现已在最新的稳定版本(5.1.0.Final)中得到修复,您可以使用属性hibernate.ogm.mongodb.authentication_database
来选择身份验证数据库的名称( admin
是默认名称。)