使用jasypt加密配置文件

添加依赖:

 

  
            com.github.ulisesbocchio
            jasypt-spring-boot-starter
            2.1.0
        

        
        
            mysql
            mysql-connector-java
            
            8.0.11
        


        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.1.2
        

配置jasypt的秘钥,可以随便填写,

#jasypt加密的密匙
jasypt:
  encryptor:
    password: banjuanliunian

获取到加密后的密文:

使用jasypt加密配置文件_第1张图片

如下:

 @Test
    void getPw(){
        String password = encryptor.encrypt("密码");
        String username = encryptor.encrypt("账户");
        String url = encryptor.encrypt("jdbc:mysql://ip地址:3306/数据库?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT%2b8");

        System.out.println("url:" +url);
        System.out.println("password:" +password);
        System.out.println("username:" +username);

    }

 

加密后的密文如下:

使用jasypt加密配置文件_第2张图片

配置yml中:

spring:
  datasource:
    username: ENC(D6iIROr2zwifm5R0PZUN9Q==)
    password: ENC(UI/n8+smZpukoASwk6GgYw==)
    url: ENC(iR+kA8TtHnlwhGAmzHeTJUUPFvvkme9FS8azvbSifq7byce6vyWGH3f0v799b92kZ0SLXTwtJZaNVrP6WgeOLKxYFG/tuGCKWTVxXqciuK+Yl97yzyNQWzgI/KP3gelP+qXYjMEy9GmnAIYExpFDJWJQeZfpKzDcbA5KaJVamzR6w7pnBvUvBnN+yle/NsGD1Z3i2wUJMgjnKHwmq1VfhPkwiWBnoV5+ar+GUGuECJyxwAZVyy774p1k3/uUPLCU)
server:
  port: 9091

#jasypt加密的密匙
jasypt:
  encryptor:
    password: banjuanliunian
mybatis-plus:
  mapper-locations: classpath*:/mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

注意上面的 ENC()为固定写法,()里面是加密后的信息。

测试查询:

  @Test
    void testDataBase(){
        TAdmin admin = tAdminService.getById(1L);
        System.out.println(admin);
    }

数据如下:

 

 

可以看到数据成功被查询出来了

你可能感兴趣的:(Java)