Spring Boot2 使用 Jasypt加密配置文件 idea

时间:2025-03-21 17:40:25
1 、首先在xml 配置 以下,
		<dependency>
			<groupId></groupId>
			<artifactId>jasypt-spring-boot-starter</artifactId>
			<version>1.8</version>
		</dependency>

		<dependency >
			<groupId>  </groupId >
			<artifactId > jasypt-spring-boot-starter </artifactId >
			<version > 2.0.0 </version >
		</dependency >

2、maven down 下以后,配置文件添加 ENC(加密后的数值)加密

=security
=PBEWithMD5AndDES
 = ENC(CI/3yFcn6kg/GDlp+miWOw==)

3、BootApp;ication 配置添加注解

@SpringBootApplication
@Configuration //这里是
@EnableEncryptableProperties //这里是

public class BootApplication {

	public static void main(String[] args) {
		(, args);
	}

}

4、加密解密方式

Maven依赖如下:

<dependency>
        <groupId></groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>2.0.0</version>
</dependency>
jasypt-spring-boot
如果项目里没有使用@SpringBootApplication或者@EnableAutoConfiguration,可以手动在Configuration类上添加注解@EnableEncryptableProperties,来在整个环境的属性启用属性加密。

Maven依赖如下:

<dependency>
        <groupId></groupId>
        <artifactId>jasypt-spring-boot</artifactId>
        <version>2.0.0</version>
</dependency>
使用:

@Configuration
@EnableEncryptableProperties
public class MyApplication {
    ...
}
指定加密属性文件
前面两种方法都会在整个环境中启动属性加密。如果想指定加密文件,可以使用@EncryptablePropertySource指定。

Maven依赖:

<dependency>
        <groupId></groupId>
        <artifactId>jasypt-spring-boot</artifactId>
        <version>2.0.0</version>
</dependency>
@EncryptablePropertySource指定文件:

@Configuration
@EncryptablePropertySource(name = "EncryptedProperties", value = "classpath:")
public class MyApplication {
	...
}
@EncryptablePropertySources指定多个文件:

@Configuration
@EncryptablePropertySources({@EncryptablePropertySource("classpath:"),
	                             @EncryptablePropertySource("classpath:")})
public class MyApplication {
	...
}
如果项目里使用@SpringBootApplication,可以使用@PropertySource指定加密的文件:

@SpringBootApplication
@EnableEncryptableProperties
@PropertySource(name="EncryptedProperties", value = "classpath:")
public class MyApplication {
        ...
}
使用
生成密文

启动时加载命令模式
 通过命令行运行 jasypt-1.9. 包命令来加密解密:




1. 在jar包所在目录打开命令行,运行如下加密命令:


[plain] view plain copy
java -cp jasypt-1.9.  input="root" 
password=security algorithm=PBEWithMD5AndDES  
运行结果如下:
[plain] view plain copy
----ENVIRONMENT-----------------  
  
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.91-b14   
  
----ARGUMENTS-------------------  
  
algorithm: PBEWithMD5AndDES  
input: root  
password: security  
  
----OUTPUT----------------------  
  
i00VogiiZ1FpZR9McY7XNw==  
 2. 使用刚才加密出来的结果进行解密,执行如下解密命令:


[plain] view plain copy
java -cp jasypt-1.9.  
input="i00VogiiZ1FpZR9McY7XNw==" password=security algorithm=PBEWithMD5AndD  
运行结果如下:


[plain] view plain copy
----ENVIRONMENT-----------------  
  
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.91-b14   
  
----ARGUMENTS-------------------  
  
algorithm: PBEWithMD5AndDES  
input: i00VogiiZ1FpZR9McY7XNw==  
password: security  
  
----OUTPUT----------------------  
  
root  
配置文件中使用密文

xxx=ENC(密文)
启动spring boot应用需要--配置前面用来加密明文的密码。

java -jar target/jasypt-spring-boot-demo-0.0. --=supersecretz