无法使用Java连接在MongoDB 3.0.2中对用户进行身份验证 [英] Cannot authenticate user in MongoDB 3.0.2 using java connection
本文介绍了无法使用Java连接在MongoDB 3.0.2中对用户进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1-在mongo shell中创建的用户
1 - Created user in 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连接到数据库
2 - Connected to database from 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-收到此错误(当我不使用身份验证时此代码将起作用)
4 - Get this error (this code works when I don't use authentication)
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)
推荐答案
稍加挖掘,我发现Mongo 3不喜欢挑战响应,而是更喜欢使用 MongoCredential.createScramSha1Credential " >
A little digging and I found that Mongo 3 isn't fond of challenge-response and prefers 'MongoCredential.createScramSha1Credential'
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);
这篇关于无法使用Java连接在MongoDB 3.0.2中对用户进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文