【问题标题】:Spring MongoDB authorization failedSpring MongoDB 授权失败
【发布时间】:2015-05-07 21:40:30
【问题描述】:

我在 Spring MongoDB 授权过程中遇到问题 - 尝试通过 REST API 检索数据导致响应:

"error": "Internal Server Error",
"exception": "org.springframework.data.mongodb.CannotGetMongoDbConnectionException",
"message": "Failed to authenticate to database [testdb], username = [test_user], password = [t**t]"

我已经安装并配置了MongoDB。这是我的MongoDB 配置文件:

systemLog:  
    destination: file  
    logAppend: true  
    path: C:\Program Files\MongoDB\data\log\mongod.log  
    timeStampFormat: iso8601-utc  

storage:
    dbPath: C:\Program Files\MongoDB\data\db  
    journal:  
        enabled: true  

processManagement:  
    # fork: true  
    pidFilePath: C:\Program Files\MongoDB\mongod.pid  

net:  
    port: 27017  
    bindIp: 127.0.0.1

security:
    authorization: enabled

下一步我为testdb数据库创建了管理员用户和用户:

> use admin
switched to db admin
> db.createUser({user: "admin", pwd: "qwerty", roles: ["root"]})
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
> use testdb
switched to db testdb
> db.createUser({user: "test_user", pwd: "test", roles: [{role: "readWrite", db: "testdb"}]})
Successfully added user: {
        "user" : "test_user",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "testdb"
                }
        ]
}

POM 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.rest</groupId>
    <artifactId>spring-rest-api</artifactId>
    <version>1.0</version>
    <packaging>war</packaging>

    <properties>
        <!-- Enable Java 8 -->
        <java.version>1.8</java.version>
        <guava.version>18.0</guava.version>
        <hamcrest.version>1.3</hamcrest.version>
        <mockito.version>1.9.5</mockito.version>
        <assertj.version>1.7.0</assertj.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- Configure the main class of our Spring Boot application -->
        <start-class>org.wixanz.App</start-class>
    </properties>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
    </parent>

    <dependencies>
        <!-- Get the dependencies of a web application -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Data MongoDB-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>${hamcrest.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>${assertj.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>${mockito.version}</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Util -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Spring Boot Maven Support -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Java MongoDB 配置文件:

@Configuration
@PropertySource("classpath:mongodb.properties")
public class MongoDBConfig {

    @Autowired
    Environment env;

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception {

        UserCredentials userCredentials = new UserCredentials(env.getProperty("mongodb.username"),  env.getProperty("mongodb.password"));
        MongoClient mongo = new MongoClient(env.getProperty("mongodb.host"),  Integer.parseInt(env.getProperty("mongodb.port")));
        return new SimpleMongoDbFactory(mongo, env.getProperty("mongodb.db"), userCredentials);
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }

}

MongoDB.properties 项目中的文件:

mongodb.host=localhost
mongodb.port=27017
mongodb.db=testdb
mongodb.username=test_user
mongodb.password=test

如果我尝试在没有实现UserCredential 的情况下连接到testdb 数据库,则连接建立成功并接收到数据。

我需要更正数据库授权连接成功通过吗?

【问题讨论】:

    标签: spring mongodb


    【解决方案1】:
    Connect to mongoDb
    

    mongo 127.0.0.1:27017

    创建用户 转到 mongoDB 控制台并删除您当前的用户并将 authSchema 版本设置为 3 而不是 5 , 在 mongo 控制台中执行这些命令 -

    mongo 使用管理员

    db.system.users.remove({})

    db.system.version.remove({})

    db.system.version.insert({ "_id" : "authSchema", "currentVersion" : 3 })

    创建用户:

    使用 test-dev-db

    db.createUser( { 用户:“根”, 密码:“bnuy93JoLJjiop”, 角色:[“readWrite”,“dbAdmin”] } )

    【讨论】:

      猜你喜欢
      • 2020-04-03
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多