配置依赖

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>1.16</version>
</dependency>
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot</artifactId>
    <version>2.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jasypt/jasypt -->
<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.3</version>
</dependency>

配置文件配置;配置加密所需的salt(盐),然后需要加密的配置用ENC(密文)的形式配置

#加密所需要的盐
jasypt.encryptor.password=your salt

# 解密得到原始密码
spring.datasource.password= ENC(密文)

得到密文

import org.jasypt.util.text.BasicTextEncryptor;

public static void main(String[] args) {
    BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    //加密所需的salt(盐)
    textEncryptor.setPassword("AFfYkitulv45IsskmXI50JMXoaxZTKJ6");
    //要加密的数据(数据库的用户名或密码)
    String username = textEncryptor.encrypt("root");
    String password = textEncryptor.encrypt("root123");

    //解密
    String user = textEncryptor.decrypt("Yf7YaiqdDaNppOwmKfi+qw==");
    System.out.println("username:"+username);
    System.out.println("password:"+password);
    System.out.println("user:"+user);
}

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐