簡體   English   中英

使用Hibernate OGM進行MongoDb身份驗證

[英]MongoDb authentication using 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" : "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實例:在等待匹配的服務器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:'身份驗證失敗。' 在服務器上mongo:27017。 完整響應為{\\“ok \\”:0.0,\\“errmsg \\”:\\“身份驗證失敗。\\”,\\“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:'身份驗證失敗。' 在服務器上mongo:27017。 完整響應為{\\“ok \\”:0.0,\\“errmsg \\”:\\“身份驗證失敗。\\”,\\“code \\”:18,\\“codeName \\”:\\“AuthenticationFailed \\”}}}] “}}

Hibernate OGM目前使用數據庫名稱作為身份驗證數據庫。 這是一個錯誤,我現在正在努力。

在您的示例中(順便說一下似乎都是正確的),您希望連接到“lvdb”數據庫,但是您在“admin”數據庫中定義了用戶。 Hiebernate OGM實際上是在“lvdb”數據庫中尋找用戶。

更新 :此問題現已在最新的穩定版本(5.1.0.Final)中得到修復,您可以使用屬性hibernate.ogm.mongodb.authentication_database選擇身份驗證數據庫的名稱( admin是默認名稱)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM