gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:31:50 24 4
gpt4 key购买 nike

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)

最佳答案

稍加挖掘,我发现 Mongo 3 不喜欢挑战响应,更喜欢 '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 - 无法使用 java 连接在 MongoDB 3.0.2 中对用户进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30228672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com