Spring-Boot加密数据库密码

1.maven引入jar包

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

2.创建一个类用于生成加密后的密码

public static void main(String[] args) {
		BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt(),后面需要在文件中配置
        textEncryptor.setPassword("hykj");
        //要加密的数据(数据库的用户名或密码)
        String url = textEncryptor.encrypt("jdbc:mysql://127.0.0.1:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
        String username = textEncryptor.encrypt("root");
        String password = textEncryptor.encrypt("linchuang123");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        System.out.println("url:"+url);
	}

3.运行,得到密码

username:8pUGvMFU2qeey1D1F9uRjg==
password:VD9YUSb/jF/m9O5eHDZGrvsfSLlCJ0td
url:3+5OUDdIWuAGXmJJ5yB1rKryD6mDqP5uXerLTwHvIMCaw7f4mGL8mOQouNYEsHSIX9y/Ea5Ld/GcGbqPIyHgpeTiLpubHLMYV4LYjEEAD+Syr1kqVOd7zzvzYoZtTffrzBs6ZihGncm8Ly4o8a6YLEjloQaX6eP9bZ68U5+WvAV2gMi9x0BjurIO4R159LE2U9MSQ4qo=

4.在yml配置文件中配置salt(盐)

jasypt:
  encryptor:
    password: hykj

5.将数据库的明文改为密文

 #url: jdbc:mysql://127.0.0.1:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
 #username: root
 #password: 123
 url: ENC(1NxDSmkJ0w1XhWFkjOWPJM5wXqpt88PPKxA3/kz1oT691Kn6LqSZd02Rj+QJPQCFNT5tcRBiSNjajECczi3ja6CbeLWU1upf8ok/2obV+uNmVuMb9AeX0oa/If7nkD7oYjPZJ94XNeKXVS4hJ1fPx7XDWrFb4yf14dcaXC11/cXP5CSc5eomyXrWKJdWESaJ6Acoz/Uk=)
 username: ENC(QjN2ksQY7IDP/x79Jju1Wg==)
 password: ENC(X1TkHG54ltwdqfQjyCyFianP8jtQ0J4I

6.启动程序,能自动解密,正常启动

你可能感兴趣的:(jasypt加密)