gpt4 book ai didi

mongodb - spring-data-mongodb 给出 mongodb 身份验证异常

转载 作者:可可西里 更新时间:2023-11-01 09:21:23 27 4
gpt4 key购买 nike

我正在使用

  • Mongodb 3.2.0
  • Mongo-Java-Driver 3.2.0
  • spring-data-mongodb 1.9.2

并引用此链接 accessing-mongodb-with-authentication ,并通过以管理员身份登录管理数据库在 myDb 中创建用户。

    db.createUser({user: "admin",pwd: "password",roles: [ { role: "readWrite", db: "myDb" } ]});
db.auth('admin','password');
db.grantRolesToUser("admin",[{ role: "dbAdmin", db: "myDb" }])

为了设置 mongodb 身份验证,链接示例有效。 Spring 数据配置是:

<bean class="org.springframework.data.mongodb.core.MongoTemplate"
id="mongoTemplate">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="myDb" />
<constructor-arg name="userCredentials" ref="mongoCredentials"/>
</bean>

<bean class="org.springframework.data.mongodb.core.MongoFactoryBean"
id="mongo">
<property name="host" value="localhost" />
<property name="port" value="27017" />
</bean>

<bean id="mongoCredentials" class="org.springframework.data.authentication.UserCredentials">
<constructor-arg name="username" value="admin" />
<constructor-arg name="password" value="password"/>

</bean>

在我的java代码中:

    public static void main(String[]s)
{
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");


dao.TemplateDao dao = (dao.TemplateDao)context.getBean("dao");
dao.testingSales();
}
public void testingSales() {
Date date = Calendar.getInstance().getTime();
Criteria c= null;
Aggregation agg = null;
Aggregation agg2 = null;
System.out.println(mongoTemplate.getCollectionNames());

}

它抛出这个异常:

         org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 13: 'not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }", "code" : 13 }; nested exception is com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on journaldev to execute command { listCollections: 1, cursor: { batchSize: 0 } }' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }", "code" : 13 }
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:107)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2114)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:448)
at org.springframework.data.mongodb.core.MongoTemplate.getCollectionNames(MongoTemplate.java:1654)
at dao.TemplateDaoImpl.testingSales(TemplateDaoImpl.java:4812)
at dao.impl.TemplateDaoImpl.main(TemplateDaoImpl.java:340)

Caused by: com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }' on server localhost:27017. The full response is { "ok" : 0.0, "errmsg" : "not authorized on myDb to execute command { listCollections: 1, cursor: { batchSize: 0 } }", "code" : 13 }
at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:86)
at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:119)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:173)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:215)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:206)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:112)
at com.mongodb.operation.ListCollectionsOperation$1.call(ListCollectionsOperation.java:177)
at com.mongodb.operation.ListCollectionsOperation$1.call(ListCollectionsOperation.java:172)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:239)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:212)
at com.mongodb.operation.ListCollectionsOperation.execute(ListCollectionsOperation.java:172)
at com.mongodb.operation.ListCollectionsOperation.execute(ListCollectionsOperation.java:80)
at com.mongodb.Mongo.execute(Mongo.java:773)
at com.mongodb.Mongo$2.execute(Mongo.java:760)
at com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
at com.mongodb.OperationIterable.forEach(OperationIterable.java:70)
at com.mongodb.MappingIterable.forEach(MappingIterable.java:50)
at com.mongodb.MappingIterable.into(MappingIterable.java:60)
at com.mongodb.DB.getCollectionNames(DB.java:253)
at org.springframework.data.mongodb.core.MongoTemplate$14.doInDB(MongoTemplate.java:1656)
at org.springframework.data.mongodb.core.MongoTemplate$14.doInDB(MongoTemplate.java:1654)
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:446)
... 3 more

问候

克里斯

最佳答案

I think maybe the mongodb's authorization has little hard to understand.
This works for me,

@Configuration
public class MongoConfig {

@Value("${mongo.host}")
private String host;

@Value("#{new Integer('${mongo.port}')}")
private Integer port;

@Value("${mongo.database}")
private String database;

@Value("${mongo.username}")
private String username;

@Value("${mongo.password}")
private String password;

public @Bean MongoClientFactoryBean mongoDbFactory() throws Exception {
MongoClientFactoryBean clientFactoryBean = new MongoClientFactoryBean();
clientFactoryBean.setHost(host);
clientFactoryBean.setPort(port);
MongoCredential credential = MongoCredential.createScramSha1Credential(username, database, password.toCharArray());
clientFactoryBean.setCredentials(new MongoCredential[]{credential});
return clientFactoryBean;
}

public @Bean MongoTemplate mongoTemplate(Mongo mongo) throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(mongo, database);
return mongoTemplate;

}
}

---------------------------------
mongo.host=127.0.0.1
mongo.port=27017
mongo.username=hisoka
mongo.password=welcome
mongo.database=test
----------------------------
Just in mongod cmd create user hisoka for db test,
db.createUser({"user":"hisoka","pwd":"welcome","roles":[{role:"readWrite",db:"test"}]});
------------------------------
Take look in mongoChef, the mongodb auth info:
{
"_id" : "test.hisoka",
"user" : "hisoka",
"db" : "test",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : NumberInt(10000),
"salt" : "tfXYRbCj6N433PFWJzwarA==",
"storedKey" : "pMnNVX7Co7AY7Q4b/dtq18IQfeE=",
"serverKey" : "x8vUt590SYVxqW2PW6DA849iTgE="
}
},
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}

关于mongodb - spring-data-mongodb 给出 mongodb 身份验证异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38605270/

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