0
JavaでMongoDB認証を実行する方法を参照してください。使用されるMongDB Java Driverのバージョンは3.4.1です。 MongoDBのサーバーは= 3.4JavaでMongoDBの認証を行う方法
まずのmongod --authを使用して認証モードでアプリケーション・コンテキストのtest.xmlを使用して、私のユニットテストを開始serverAddressにインスタンス
<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" />
を作成することです。これで、データベースインスタンスを取得すると、そのインスタンスを操作できます。
それはスローされます。
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