MongoDB GridFS 身份验证不工作

MongoDB GridFS authentication not working

我正在尝试通过 Spring 启动应用程序将数据插入 MongoDB 3.4 GridFS 存储并启用身份验证,但我不断收到此错误:

Query failed with error code 13 and error message 'not authorized on user to execute command { find: \"fs.files\", filter: { metadata.fieldname: \"someContent\" } }' on server ip:27017"

我为此创建了一个拥有 "superuser" 权限的用户:

use admin
db.createUser(
{
   user: "admin", 
   pwd: "password", 
   roles:["root"]
})

我的 spring 开机 application.properties 是:

spring.data.mongodb.host=ip
spring.data.mongodb.port=27017
spring.data.mongodb.username=admin
spring.data.mongodb.password=password
spring.data.mongodb.database=user
spring.data.mongodb.authentication-database=admin

我已经尝试了 "old" MongoDB 个角色,但没有成功:

db.createUser(
{
   user: "admin",
   pwd: "password",
   roles: [
             { role: "userAdminAnyDatabase", db: "admin" },
             { role: "readWriteAnyDatabase", db: "admin" },
             { role: "dbAdminAnyDatabase", db: "admin" },
             { role: "clusterAdmin", db: "admin" }
          ]
})

读取和写入其他(非 gridFS)数据库完美无缺。 关于如何解决这个问题的任何想法?

解决方法: 我还有一个 AbstractMongoConfiguration class 躺在那里。

@Configuration
public class SpringMongoConfig extends AbstractMongoConfiguration {
  //some code...
}

删除它解决了它。 对于 MongoDB 配置,您必须使用 application.properties 或 java class。