How to use SpringBoot—jasypt encryption and decryption library

introduce

In a production environment, if the database is configured in plain text, there will be security issues. jasypt is a general encryption and decryption library.

pom dependencies

<!--  数据库密码加密       -->
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.0.0</version>
</dependency>

Obtain ciphertext, encrypt and decrypt data

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

public class EncryptUtils {

    public static void main(String[] arg){

        StandardPBEStringEncryptor standardPBEStringEncryptor =new StandardPBEStringEncryptor();
        /*配置文件中配置如下的算法*/
        standardPBEStringEncryptor.setAlgorithm("PBEWithMD5AndDES");
        /*配置文件中配置的password*/
        standardPBEStringEncryptor.setPassword("SMRsmr2022202220222022");
        /*解密:加密的文本*/
        System.out.println( standardPBEStringEncryptor.decrypt("PBu1ro5ySdVbYivoer5uPw=="));

        // 加密账号
        String name = standardPBEStringEncryptor.encrypt("root");
        // 加密密码
        String password =standardPBEStringEncryptor.encrypt("root");

        /*将加密的文本写到配置文件中*/
        System.out.println("name="+name);
        System.out.println("password="+password);
    }

}

Configuration in the SpringBoot-yml configuration file

jasypt:
  encryptor:
    password: SMRsmr2022202220222022
    algorithm: PBEWithMD5AndDES

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/data?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    username: ENC(wFn1/z2FEjXVQfynQphO7A==)
    password: ENC(PBu1ro5ySdVbYivoer5uPw==)

Guess you like

Origin blog.csdn.net/Ls66666Ls/article/details/131412513