<导航

使用jasypt加密Spring Boot应用中的敏感配置

jasypt 简介

详细信息直接看github文档即可,这里仅简单罗列一丢丢信息。

Jasypt是java中流行的开源加解密工具包。Jasypt为Spring Boot应用提供property sources的加密支持,可以加密的数据有:

  • system property

  • environment property

  • command line argument

  • application.properties

  • yaml properties

  • other custom property sources

哪些是敏感信息

由于很多应用使用 配置文件 (eg:properties、yml) 来存储配置信息,配置中经常会涉及到许多敏感信息。

举几个小例子:

普通应用密码信息,如:DB、Rabbit、Redis等
特殊密码信息,如:Spring Cloud Config需要配置Git等VCS密码信息
第三方通讯凭证信息,如:调用第三方接口发送短信的通讯凭证信息
由于各业务场景不同,因此敏感信息的定义也不同。

如何使用

1、springboot项目中,引入依赖

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

2、在application.yml中配置jasypt.encryptor.password=yoursalt ,例如

jasypt:
  encryptor:
    #加解密的根密码
    password: turing 

默认使用 PBEWithMD5AndDES 加密算法,只有 jasypt.encryptor.password 是必要配置。

如果不想将jasypt的加密盐值直接配置在配置文件中,则也可改为在启动jar命令行中指定程序参数或jvm参数
#启动springboot的jar包,指定程序参数
java -jar xxx.jar --jasypt.encryptor.password=turing

#启动springboot的jar包,指定JVM参数
java  -Djasypt.encryptor.password=turing -jar xxx.jar 

3、命令行加解密

#直接使用命令对明文进行加密
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=turing algorithm=PBEWithMD5AndDES
#直接使用命令对密文进行解密
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="KCQCruXa2BN+StcKVPlkAg==" password=turing algorithm=PBEWithMD5AndDES

4、 在项目业务代码中使用jasypt

@autowired 注入StringEncryptor  bean

package com.tingcream.springmybatis2;
 
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class JasyptTest {
 
@Autowired
private StringEncryptor stringEncryptor;
 
 
@Test
public void encrypt() {
String text ="123456";
String s1= stringEncryptor.encrypt(text);
System.out.println("加密后得到密文为:"+s1);
String s2= stringEncryptor.decrypt(s1);
System.out.println("解密后得到原文为:"+s2);
 
/*
 * 注意: 每次使用stringEncryptor加密同样的明文,所得到的密码都可能不一样
 * 加密后得到密文为:LTsP+Ixe26vAZYnVd28Lag==
解密后得到原文为:123456
加密后得到密文为:EeTv7ggGS3SVEICVd1TVdA==
解密后得到原文为:123456
加密后得到密文为:L0gcXucNVewXZSsFNrmVhw==
解密后得到原文为:123456
 * 
 * */
 
}  
}

这个是依赖spring容器来进行加密。
不依赖spring容器直接使用JAVA方法:

@Test
public void testEnvironmentProperties() {
  //对应配置文件中对应的根密码
  System.setProperty("jasypt.encryptor.password", "jiaxing");
  StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment();
  //加密方法
  System.out.println(stringEncryptor.encrypt("jiaxing"));
  //解密方法    
  System.out.println(stringEncryptor.decryptstringEncryptor.encrypt("jiaxing"))
}

5、application.yml中配置加密后的属性

将加密后的属性值配置在配置文件中即可,ENC 是约定的关键字,在启动时会解析所有 PropertySource 中的加密属性。

spring: 
  datasource: 
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: root
      password: ENC(KCQCruXa2BN+StcKVPlkAg==)  #注意jasypt的密文需要使用ENC括起来

附上工具类

package com.example.code.bot_monomer.utils;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

public class JasyptUtils {


    public static void main(String[] args) {
        String pw = JasyptUtils.encryptPwd("123abc","aaaaafddddddddddfdfdfdfdfdfddfdfd");
        System.out.println(pw);
        String mw = JasyptUtils.decyptPwd("123abc","XtCQ+/Totbxh66iktPkWg/O1Orj7fYKZBq1yx4mh0C4rDESUcwaC7moofKX9cify");
        System.out.println(mw);
    }

    /**
     * Jasypt生成加密结果
     *
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value    待加密值
     * @return
     */
    public static String encryptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(cryptOr(password));
        String result = encryptOr.encrypt(value);
        return result;
    }

    /**
     * 解密
     *
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value    待解密密文
     * @return
     */
    public static String decyptPwd(String password, String value) {
        PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor();
        encryptOr.setConfig(cryptOr(password));
        String result = encryptOr.decrypt(value);
        return result;
    }

    /**
     * @param password salt
     * @return
     */
    public static SimpleStringPBEConfig cryptOr(String password) {
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations(1000);
        config.setPoolSize(1);
        // according Jasypt documentation, if no provider name is explicitly set, the default JVM provider will be used.
        config.setProviderName(null);
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        return config;
    }
}
View Code

 

 

 

参考文章:

https://blog.csdn.net/jasnet_u/article/details/101485385

https://www.jianshu.com/p/5a120bd360bc

posted @ 2019-10-25 18:46  字节悦动  阅读(2788)  评论(0编辑  收藏  举报