springboot中数据库配置加密

在springboot中,配置数据库等信息时,用户名和密码明文显示会大大下降安全性,在此介绍一种加密方式,简单易用。java

添加依赖:mysql

<dependency>
   <groupId>com.github.ulisesbocchio</groupId>
   <artifactId>jasypt-spring-boot-starter</artifactId>
   <version>1.8</version>
</dependency>

在yml文件或properties文件中配置加密参数:git

jasypt:
  encryptor:
    password: 123

获得加密后的密码:github

@Autowired
    StringEncryptor stringEncryptor;    @Test
    public void encryptPwd() {
        String result = stringEncryptor.encrypt("yourpassword");
        System.out.println(result); 
    }

将加密后的密码配置在yml或properties文件中便可:spring

datasource:
    url: jdbc:mysql://网段/数据库名
    username: 用户名
    password: ENC(Ipjb1cUctOHmbt6a1qIUjw==) #Ipjb1cUctOHmbt6a1qIUjw==  就是加密后的密码
    driverClassName: com.mysql.jdbc.Driver
相关文章
相关标签/搜索