Java 使用Hibernate OGM的MongoDb身份验证

Java 使用Hibernate OGM的MongoDb身份验证,java,mongodb,hibernate,hibernate-ogm,Java,Mongodb,Hibernate,Hibernate Ogm,我可以使用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

我可以使用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);
如您所见,我正在使用
SCRAM-SHA1
作为身份验证机制

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

原因:org.hibernate.service.spi.ServiceException:OGM000071:无法启动数据存储提供程序 原因:org.hibernate.hibernate异常:OGM001214:无法连接到MongoDB实例:等待与ReadPreferenceServerSelector{readPreference=primary}匹配的服务器时在30000毫秒后超时。群集状态的客户端视图为{type=UNKNOWN,servers=[{address=mongo:27017,type=UNKNOWN,state=CONNECTING,exception={com.mongodb.MongoSecurityException:exception authentication mongoredential{mechanism=SCRAM-SHA-1,userName='user',source='lvdb',password=,mechanismProperties={}},由{com.mongodb.MongoCommandException:命令失败,错误为18:“身份验证失败”。在服务器mongo:27017上。完整响应为{\'ok\':0.0,\'errmsg\':\'Authentication failed.\',\'code\':18,\'codeName\':\'AuthenticationFailed\'}] 原因:com.mongodb.MongoTimeoutException:等待与ReadPreferenceServerSelector{readPreference=primary}匹配的服务器时,30000毫秒后超时。群集状态的客户端视图为{type=UNKNOWN,servers=[{address=mongo:27017,type=UNKNOWN,state=CONNECTING,exception={com.mongodb.MongoSecurityException:MongoCredential{mechanism=SCRAM-SHA-1,userName='user',source='lvdb',password=,mechanismProperties={}}}的异常身份验证,由{com.mongodb.MongoCommandException:命令失败,错误18:'身份验证失败。{mongo:27017服务器上的完整响应为{\'ok\':0.0,\'errmsg\:\“身份验证失败。\”、\“代码\”:18、\“代码名\:\“身份验证失败\”}}]“}}


Hibernate OGM当前正在使用数据库名称作为身份验证数据库。这是一个bug,我正在处理它

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


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

此问题现在已在master上修复,下一版本应该可以正常工作
<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>