无法使用java连接在MongoDB 3.0.2中对用户进行身份验证

时间:2015-05-14 03:20:12

标签: java mongodb authentication

1 - 在mongo shell中创建用户

> use admin
switched to db admin
> db.createUser( { user:"tiger", pwd:"secret", roles: [ { role:"readWrite", db:"zoo" } ] } )
Successfully added user: {
    "user" : "tiger",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "zoo"
        }
    ]
}

2 - 从Java连接到数据库

List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add( new ServerAddress( "remoteserver" ));
List<MongoCredential> credentials = new ArrayList<MongoCredential>();  
credentials.add(
    MongoCredential.createMongoCRCredential(
        "tiger",
        "admin",
        "secret".toCharArray()
    )
);
MongoClient mongoClient = new MongoClient( seeds);  //, credentials );
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, "zoo");

3 - 插入一行

mongoTemplate.insert(animal, "animal");

4 - 获取此错误(此代码在我不使用身份验证时有效)

2015-05-13 23:11:36.166 ERROR 67846 --- [nio-8443-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed" : "remoteserver:27017" , "ok" : 0.0 , "errmsg" : "auth failed" , "code" : 18}; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "remoteserver:27017" , "ok" : 0.0 , "errmsg" : "auth failed" , "code" : 18}] with root cause

com.mongodb.CommandFailureException: { "serverUsed" : "remoteserver:27017" , "ok" : 0.0 , "errmsg" : "auth failed" , "code" : 18}
    at com.mongodb.CommandResult.getException(CommandResult.java:76)
    at com.mongodb.CommandResult.throwOnError(CommandResult.java:131)
    at com.mongodb.DBPort$NativeAuthenticator.authenticate(DBPort.java:652)
    at com.mongodb.DBPort.authenticate(DBPort.java:364)
    at com.mongodb.DBPort.checkAuth(DBPort.java:375)
    at com.mongodb.DBTCPConnector.doOperation(DBTCPConnector.java:206)
    at com.mongodb.DBCollectionImpl.writeWithCommandProtocol(DBCollectionImpl.java:424)
    at com.mongodb.DBCollectionImpl.insertWithCommandProtocol(DBCollectionImpl.java:389)
    at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:188)
    at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:167)
    at com.mongodb.DBCollection.insert(DBCollection.java:93)
    at com.mongodb.DBCollection.insert(DBCollection.java:78)
    at com.mongodb.DBCollection.insert(DBCollection.java:120)
    at org.springframework.data.mongodb.core.MongoTemplate$8.doInCollection(MongoTemplate.java:904)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:410)
    at org.springframework.data.mongodb.core.MongoTemplate.insertDBObject(MongoTemplate.java:899)
    at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:721)
    at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:676)

2 个答案:

答案 0 :(得分:15)

稍微挖掘一下,我发现Mongo 3不喜欢挑战 - 反应并且更喜欢 MongoCredential.createScramSha1Credential &#39;

List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add( new ServerAddress( "remoteServer" ));
List<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(
    MongoCredential.createScramSha1Credential(
        "username",
        "adminDatabase",
        "password".toCharArray()
    )
);
MongoClient mongoClient = new MongoClient( seeds, credentials );
MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, "applicationDatabase");
mongoTemplate = new MongoTemplate(mongoDbFactory);

答案 1 :(得分:0)

当使用artefactId&#34; spring-boot-starter-data-mongodb&#34; MongoCredential.createScramSha1Credential 不可用。

所以这就是我必须要做的事情:

A)在 pom.xml 中包含对mongodb的maven依赖:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.4.0</version>
</dependency>

B)使用user4898172的解决方案( MongoCredential.createScramSha1Credential

public Mongo mongo() throws Exception {
    List<MongoCredential> credentials = new ArrayList<>();
    credentials.add(MongoCredential.createScramSha1Credential(username, database, password.toCharArray()));

    return new MongoClient(new ServerAddress(host, port), credentials);
}

这两个变化帮助我解决了Exeption:

  

com.mongodb.CommandFailureException:{&#34; serverUsed&#34; :   &#34; REMOTESERVER:27017&#34; ,&#34; ok&#34; :0.0,&#34; errmsg&#34; :&#34; auth失败&#34; ,&#34;代码&#34;   :18}

(我使用的是MongoDB服务器版本:3.4.0)

mongodb github site也是实施细节的一个很好的参考。