2017-01-27 2 views
0

Hier erfahren Sie, wie Sie die MongoDB-Authentifizierung in Java durchführen. Die verwendete MongDB Java Driver Version ist 3.4.1. MongoDB Server = 3,4So wird die Authentifizierung für MongoDB in Java ausgeführt

zuerst die Serveraddress Instanz

<context:annotation-config /> 

<mongo:mongo-client host="ndc-dv-mongo1.local" port="27017" id="mongo" credentials="UserTest:[email protected]_batch"> 
    <mongo:client-options connections-per-host="10" connect-timeout="1000" max-wait-time="1500" 
     socket-keep-alive="true" socket-timeout="1500" /> 
</mongo:mongo-client> 

<mongo:db-factory dbname="carelogic-batch" mongo-ref="mongo" id="mongoDbFactory" /> 

<mongo:mapping-converter id="mongoConverter" base-package="com.dmotta.mongotest.model"> 
    <mongo:custom-converters base-package="com.dmotta.mongotest.model" /> 
</mongo:mapping-converter> 

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
    <constructor-arg ref="mongoDbFactory" /> 
    <constructor-arg ref="mongoConverter" /> 
    <property name="writeConcern" value="SAFE" /> 
</bean> 

<mongo:repositories base-package="com.dmotta.mongotest.repository" /> 

<bean class="org.springframework.data.mongodb.core.mapping.event.LoggingEventListener" /> 
<bean class="org.springframework.data.mongodb.core.MongoExceptionTranslator" /> 

Start meines Unit Tests mit der anwendungskontext test.xml in Auth-Modus unter Verwendung von mongod --auth erstellen. Sobald Sie die Datenbankinstanz erhalten haben, können Sie Operationen für sie ausführen.

Es wird werfen:

2017-01-27 11:38:16.678 INFO ???? --- [cluster-ClusterId{value='588b777760866a17135c54b5', description='null'}-ndc-dv-mongo1.local:27017] o.m.d.cluster       : Exception 

in monitor thread while connecting to server ndc-dv-mongo1.local:27017 

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='UserTest', source='dbtest_batch', password=<hidden>, mechanismProperties={}} 
    at com.mongodb.connection.SaslAuthenticator.wrapInMongoSecurityException(SaslAuthenticator.java:157) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:37) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:66) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:44) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:162) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:44) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:109) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:46) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) [mongodb-driver-core-3.4.1.jar:?] 
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_101] 
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server ndc-dv-mongo1.local:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" } 
    at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:117) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:37) ~[mongodb-driver-core-3.4.1.jar:?] 
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:50) ~[mongodb-driver-core-3.4.1.jar:?] 
    ... 9 more 

Antwort

0

diesen Wurf zu beheben:

notwendig ist, erstellen Sie den Benutzer "Usertest: pass1 @ dbtest_batch" in die spezifische Datenbank "dbtest_batch":

db.createUser(
    { 
    user: "UserTest", 
    pwd: "pass1", 
    roles: [ 
     { 
      "role" : "executeFunctions", 
      "db" : "admin" 
     }, 
     { 
      "role" : "readWrite", 
      "db" : "dbtest_batch" 
     } 
     ] 

    } 
) 
Verwandte Themen