使用Hibernate OGM的MongoDb身份验证 [英] MongoDb authentication using Hibernate OGM

查看:580
本文介绍了使用Hibernate OGM的MongoDb身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #mongo -u user -p pwd  - -authenticationDatabase admin 
MongoDB shell版本v3.4.1
连接到:mongodb://127.0.0.1:27017
MongoDB服务器版本:3.4.1
>使用admin
切换到db admin
>显示用户
{
_id:admin.ladmin,
user:ladmin,
db:admin,
角色:[
{
角色:userAdminAnyDatabase,
db:admin
}
]
}
{
_id:admin.living,
user:user,
db:admin,
roles:[
{
role:readWrite,
db:lvdb
}
]
}

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

 列表与LT; ServerAddress> seeds = new ArrayList< ServerAddress>(); 
seeds.add(new ServerAddress(this.configurationResources.getMongodbServer(),this.configurationResources.getMongodbPort()));

列出< MongoCredential> credentials = new ArrayList< MongoCredential>();
credentials.add(
)MongoCredential.createScramSha1Credential(
this.configurationResources.getMongodbUsername(),
this.configurationResources.getMongodbAuthenticationDatabase(),
this.configurationResources.getMongodbPassword( ).toCharArray()

);

this.mongoClient =新的MongoClient(种子,凭证);

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

 <持久版本=2.1xmlns =http://xmlns.jcp.org/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation =http ://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd\"> 
< persistence-unit name =mongotransaction-type =JTA>
< provider> org.hibernate.ogm.jpa.HibernateOgmPersistence< / provider>

< class> com.living.persistence.entities.User< / class>

<属性>
< property name =hibernate.transaction.jta.platformvalue =org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform/>
< property name =hibernate.ogm.datastore.providervalue =org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider/>
< property name =hibernate.ogm.datastore.databasevalue =lvdb/>
< property name =hibernate.ogm.datastore.hostvalue =mongo/>
< property name =hibernate.ogm.datastore.portvalue =27017/>
< property name =hibernate.ogm.datastore.usernamevalue =user/>
< property name =hibernate.ogm.datastore.passwordvalue =pwd/>
< property name =hibernate.ogm.mongodb.authentication_mechanismvalue =SCRAM_SHA_1/>

< / properties>
< / persistence-unit>
< /余辉>

正如您所看到的,我正在使用 SCRAM-SHA1

然而,当我试图部署我的应用程序时,我收到了这条消息:


导致:org.hibernate.service.spi.ServiceException:OGM000071:无法启动数据源提供程序
原因:org.hibernate.HibernateException:OGM001214:无法连接到MongoDB实例:在等待与ReadPreferenceServerSelector {readPreference = primary}匹配的服务器的30000 ms后超时。客户端集群状态视图是{type = UNKNOWN,servers = [{address = mongo:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoSecurityException:异常验证MongoCredential {mechanism = SCRAM-SHA- username ='user',source ='lvdb',password =,mechanismProperties = {}}},由{com.mongodb.MongoCommandException:命令失败,错误18:'Authentication failed。'on server mongo:27017。完整响应为{\ok \:0.0,\errmsg \:\Authentication failed.\,\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:异常验证MongoCredential {mechanism = SCRAM-SHA- username ='user',source ='lvdb',password =,mechanismProperties = {}}},由{com.mongodb.MongoCommandException:命令失败,错误18:'Authentication failed。'on server mongo:27017。完整响应为{\ok \:0.0,\errmsg \:\Authentication failed.\,\code \:18,\codeName \ :\AuthenticationFailed \}}}]}}


解决方案

目前使用数据库名称作为身份验证数据库,这是一个错误,我现在正在处理它。



在你的例子中(所有的东西似乎都是正确的) ,你想连接到
lvdbdb,但是你在admin数据库中定义了用户。Hiebernate OGM实际上是在lvdb数据库中查找用户。



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


I'm able to authenticate on my mongodb using the shell command:

#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"
                }
        ]
}

I've also been able to authenticate on it using java driver:

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);

Currently, I'm engaged on a project I want to use Hibernate OGM. I've set persistence.xml file:

<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>

As you can see I'm using SCRAM-SHA1 as authentication mechanism.

Nevertheless, I'm getting this message when I'm trying to deploy my application:

Caused by: org.hibernate.service.spi.ServiceException: OGM000071: Unable to start datatore provider Caused by: org.hibernate.HibernateException: OGM001214: Unable to connect to MongoDB instance: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {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={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server mongo:27017. The full response is { \"ok\" : 0.0, \"errmsg\" : \"Authentication failed.\", \"code\" : 18, \"codeName\" : \"AuthenticationFailed\" }}}] Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {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={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server mongo:27017. The full response is { \"ok\" : 0.0, \"errmsg\" : \"Authentication failed.\", \"code\" : 18, \"codeName\" : \"AuthenticationFailed\" }}}]"}}

解决方案

Hibernate OGM is currently using the database name as authentication database. This is a bug, I'm working on it right now.

In your example (all seems correct by the way), you want to connect to the "lvdb" db but you defined the user in the "admin" database. Hiebernate OGM is actually looking for the user in the "lvdb" database.

UPDATE: This problem has now been fixed in the latest stable release (5.1.0.Final), you can use the property hibernate.ogm.mongodb.authentication_database to select the name of the authentication database (admin is the default name).

这篇关于使用Hibernate OGM的MongoDb身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆