Spring Data MongoDB 手动配置

huangapple go评论82阅读模式
英文:

Spring data-MongoDb Manual configuration

问题

我最终想要实现的目标是从 Java 类连接到 MongoDB,这个配置应该适用于 MongoTemplate 和 MongoRepository。

Spring Boot 通过以下属性直接在 application.properties 文件中提供了 MongoDB 的配置:

spring.data.mongodb.database=delivery
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.password=delivery-user
spring.data.mongodb.username=delivery-user

不过我希望用另一种方式,在 Java 类中连接数据库,因为我需要在属性文件中对密码进行加密,然后在 Java 类中解密。根据我的理解,我尝试创建了以下类:

@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {

    @Override
    public MongoClient mongoClient() {
        String username = "delivery-user";
        String password = "delivery-user";
        String port = "27017";
        String host = "localhost";
        String db = "salesdata";
        // 也尝试过在 conString 中传递数据库名称
        String conString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=admin";
        MongoClient mClient = MongoClients.create(conString);
        return mClient;
    }

    @Override
    protected String getDatabaseName() {
        return "delivery";
    }

    @Bean
    @Override
    public MongoTemplate mongoTemplate() {
        MongoTemplate mt = new MongoTemplate(mongoClient(), getDatabaseName());
        return mt;
    }
}

当运行这个应用程序时,我遇到了以下错误:

Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='delivery-user', source='admin', password=<hidden>, mechanismProperties={}}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='delivery-user', source='admin', password=<hidden>, mechanismProperties={}}

用户名和密码是正确的,而且该用户可以访问 "delivery" 数据库。

英文:

What I finally want to achieve is, connect to mongo db from java class, this configuration should be applicable to both MongoTemplate and MongoRepository

spring boot provides configuration of mongodb directly via application.properties file by below properties

spring.data.mongodb.database=delivery
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.password=delivery-user
spring.data.mongodb.username=delivery-user

Instead of this way , I need to connect to db with java class itself,
This is because I need to make the password encrypted in properties file and then decrypt from java class
from my understanding I have tried creating a class below

@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {

    @Override
    public MongoClient mongoClient() {
        String username = &quot;delivery-user&quot;;
        String password = &quot;delivery-user&quot;;
        String port = &quot;27017&quot;;
        String host = &quot;localhost&quot;;
        String db = &quot;salesdata&quot;;
//have also tried by passing db name in conString
        String conString = &quot;mongodb://&quot; + username + &quot;:&quot; + password + &quot;@&quot; + host + &quot;:&quot; + port+&quot;/?authSource=admin&quot; ;
        MongoClient mClient = MongoClients.create(conString);
        return mClient;

    }

    @Override
    protected String getDatabaseName() {
        return &quot;delivery&quot;;
    }

    @Bean
    @Override
    public MongoTemplate mongoTemplate() {
        MongoTemplate mt = new MongoTemplate(mongoClient(), getDatabaseName());
        return mt;
    }
}

Im getting this error when running this application

 Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=&#39;delivery-user&#39;, source=&#39;admin&#39;, password=&lt;hidden&gt;, mechanismProperties={}}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=&#39;delivery-user&#39;, source=&#39;admin&#39;, password=&lt;hidden&gt;, mechanismProperties={}}

The username and password is correct, also user has access to "delivery" db

答案1

得分: 1

在字符串末尾指定数据库名称并移除"authSource"将解决这个问题。同时指定"authSource=targetDBname"似乎也是有效的。以下是可用的连接字符串示例:

String conString = &quot;mongodb://&quot; + username + &quot;:&quot; + password + &quot;@&quot; + host + &quot;:&quot; + port + &quot;/dbName&quot;;

请将dbName替换为用户具有访问权限的数据库名称。

英文:

Specifying db name in end of string and removing "authSource" will solved the problem.
Also specifying "authSource=targetDBname" seems to be working.
example for working connection string

String conString = &quot;mongodb://&quot; + username + &quot;:&quot; + password + &quot;@&quot; + host + &quot;:&quot; + port+&quot;/dbName&quot;;

Replace dbName with name of db in which the user has access to.

huangapple
  • 本文由 发表于 2020年9月29日 20:44:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64119915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定