mongo3.0.2 java程序,无法使用Java连接在MongoDB 3.0.2中对用户进行身份验证

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 - Connected to database from Java

List seeds = new ArrayList();

seeds.add( new ServerAddress( "remoteserver" ));

List credentials = new ArrayList();

credentials.add(

MongoCredential.createMongoCRCredential(

"tiger",

"admin",

"secret".toCharArray()

)

);

MongoClient mongoClient = new MongoClient( seeds); //, credentials );

MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, "zoo");

3 - Insert a row

mongoTemplate.insert(animal, "animal");

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)

解决方案

A little digging and I found that Mongo 3 isn't fond of challenge-response and prefers 'MongoCredential.createScramSha1Credential'

List seeds = new ArrayList();

seeds.add( new ServerAddress( "remoteServer" ));

List credentials = new ArrayList();

credentials.add(

MongoCredential.createScramSha1Credential(

"username",

"adminDatabase",

"password".toCharArray()

)

);

MongoClient mongoClient = new MongoClient( seeds, credentials );

MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, "applicationDatabase");

mongoTemplate = new MongoTemplate(mongoDbFactory);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值