当前位置:   article > 正文

springBoot配置文件设置mongodb连接密码加密_springboot 集成mongodb replica方式密码加密

springboot 集成mongodb replica方式密码加密

方案

1、对明文密码加密
2、自定义MongoDB配置文件,在获取密码的时候解密。

实践

pom文件引入依赖


<dependency>
  <groupId>cn.hutool</groupId>
  <artifactId>hutool-all</artifactId>
  <version>5.6.6</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

application.yml文件中定义加密的秘钥

# --spring.profiles.active=prod
spring:
  profiles:
    active: dev

encryption:
  key: fjk.fdhtpwDF.GHF

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

定义main函数测试加解密

    public static void main(String[] args) {
        byte[] bytes = "fjk.fdhtpwDF.GHF".getBytes(StandardCharsets.UTF_8);
        SymmetricCrypto aes=new SymmetricCrypto(SymmetricAlgorithm.DES,bytes);
        String testStr = aes.encryptBase64("admin");
        System.out.println("testStr:"+testStr);
        String s = aes.decryptStr(testStr);
        System.out.println("s:"+s);
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

把加密后的密码放置到MongoDB的properties配置文件中

mongodb.uri=192.xxx.xx.xx:27017
mongodb.username=mcsas
#加密后的密码
mongodb.password=rd4ytDko8pE=
mongodb.schema=mcsas

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

新增MongoDB配置文件

package com.xxx.framework.galaxy.apower.config;

import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.mongodb.MongoClientURI;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

@Configuration
@PropertySource(value = "classpath:database.properties",ignoreResourceNotFound = true)
public class MongoDBConfig {

    @Value("${mongodb.schema}")
    private String databaseName;

    @Value("${mongodb.uri}")
    private String uri;

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

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

    @Value("${encryption.key}")
    private String key;

    @Bean
    public MongoDbFactory mongoDbFactory() throws UnknownHostException {
        //解密password
        byte[] bytes = key.getBytes(StandardCharsets.UTF_8);
        SymmetricCrypto aes=new SymmetricCrypto(SymmetricAlgorithm.DES,bytes);
        String pwd = aes.decryptStr(password);
        String uriStr="mongodb://"+userName+":"+pwd+"@"+uri+"/"+databaseName;
        System.out.println("MongonDB Connction Info >>>>>>>>>>>>>>>>\t"+uriStr);
        MongoClientURI mongoClientURI=new MongoClientURI(uriStr);
        MongoDbFactory mongoDbFactory=new SimpleMongoDbFactory(mongoClientURI);
        return mongoDbFactory;
    }

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54

springBoot启动类上把自动加载的MongoDB配置类排除

package com.xxx.framework.galaxy.xx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;


@EnableAsync
@EnableScheduling
@SpringBootApplication(exclude = MongoDataAutoConfiguration.class)
public class APowerApplication {
    public static void main(String[] args) {
        SpringApplication.run(APowerApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

启动测试

连接为解密密码后的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/701589
推荐阅读
相关标签
  

闽ICP备14008679号