欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot使用jasypt對配置文件加密加密數(shù)據(jù)庫連接的操作代碼

 更新時間:2024年01月25日 16:01:53   作者:一名技術(shù)極客  
這篇文章主要介紹了springboot使用jasypt對配置文件加密加密數(shù)據(jù)庫連接的操作代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

springboot使用jasypt對配置文件加密 

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

springboot配置

jasypt:
  encryptor:
    password: saltValue    #salt值,密文加鹽
spring:
  datasource: # 數(shù)據(jù)庫鏈接
    db1:
      jdbc-url: jdbc:mysql://x.x.x.x:3306/db_test?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
      username: root      #也可以加密用戶名,依然是ENC()格式,這里沒有進行加密
      password: ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+)  #加密了密碼,ENC()括號內(nèi)為密文
      driver-class-name: com.mysql.cj.jdbc.Driver
      mapper-locations: classpath*:mapper/otcmapper/*.xml

啟動類添加注解:@EnableEncryptableProperties

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEncryptableProperties
@EnableScheduling
//@EnableAsync
public class SpBatchApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpBatchApplication.class, args);
    }
}

通過明文獲取加密的值 cmd在自己的maven倉庫目錄下執(zhí)行命令,(要保證依賴下載下來了)
解釋:
input:文字的明文
password:加密的鹽值(可隨意,必須=jasypt:encryptor:password: saltValue)
algorithm:PBEWithMD5AndDES(默認算法)

java -cp org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="密碼明文" password=saltValue algorithm=PBEWithMD5AndDES

執(zhí)行后輸出結(jié)果:OUTPUT就是密文了,把密文替換yml的屬性值就行
ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+)

----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: 密碼明文
password: saltValue
----OUTPUT----------------------
OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+

啟動springboot就會自動解密了

通過密文和鹽值解密得到明文

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="加密后的密文" password=saltValue algorithm=PBEWithMD5AndDES

代碼封裝工具類

public class JasyptUtil {
	private static final String PBEWITHMD5ANDDES = "PBEWithMD5AndDES";
	private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";
	/**
	 * 
	 * @param text  待加密原文
	 * @param crack 鹽值(密鑰)
	 * @return 加密后的字符串
	 * @Description: Jasypt加密(PBEWithMD5AndDES)
	 */
	public static String encryptWithMD5(String text, String crack) {
//1.創(chuàng)建加解密工具實例
		StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
//2.加解密配置
		EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
		config.setAlgorithm(PBEWITHMD5ANDDES);
		config.setPassword(crack);
		encryptor.setConfig(config);
//3.加密
		return encryptor.encrypt(text);
	}
	/**
	 * 
	 * @param text  待解密原文
	 * @param crack 鹽值(密鑰)
	 * @return 解密后的字符串
	 * @Description: Jasypt解密(PBEWithMD5AndDES)
	 */
	public static String decryptWithMD5(String text, String crack) {
//1.創(chuàng)建加解密工具實例
		StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
//2.加解密配置
		EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
		config.setAlgorithm(PBEWITHMD5ANDDES);
		config.setPassword(crack);
		encryptor.setConfig(config);
//解密
		return encryptor.decrypt(text);
	}
	/**
	 * 
	 * @param text  待加密的原文
	 * @param crack 鹽值(密鑰)
	 * @return 加密后的字符串
	 * @Description: jasypt 加密(PBEWITHHMACSHA512ANDAES_256)
	 */
	public static String encryptWithSHA512(String text, String crack) {
//1.創(chuàng)建加解密工具實例
		PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
//2.加解密配置
		SimpleStringPBEConfig config = new SimpleStringPBEConfig();
		config.setPassword(crack);
		config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
// 為減少配置文件的書寫,以下都是 Jasypt 3.x 版本,配置文件默認配置
		config.setKeyObtentionIterations("1000");
		config.setPoolSize("1");
		config.setProviderName("SunJCE");
		config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
		config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
		config.setStringOutputType("base64");
		encryptor.setConfig(config);
//3.加密
		return encryptor.encrypt(text);
	}
	/**
	 * 
	 * @param text  待解密原文
	 * @param crack 鹽值(密鑰)
	 * @return 解密后的字符串
	 * @Description: jasypt 解密(PBEWITHHMACSHA512ANDAES_256)
	 */
	public static String decryptWithSHA512(String text, String crack) {
//1.創(chuàng)建加解密工具實例
		PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
//2.加解密配置
		SimpleStringPBEConfig config = new SimpleStringPBEConfig();
		config.setPassword(crack);
		config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
// 為減少配置文件的書寫,以下都是 Jasypt 3.x 版本,配置文件默認配置
		config.setKeyObtentionIterations("1000");
		config.setPoolSize("1");
		config.setProviderName("SunJCE");
		config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
		config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
		config.setStringOutputType("base64");
		encryptor.setConfig(config);
//3.解密
		return encryptor.decrypt(text);
	}
}

到此這篇關(guān)于springboot使用jasypt對配置文件加密,加密數(shù)據(jù)庫連接的文章就介紹到這了,更多相關(guān)springboot使用jasypt內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論