Jasypt(Java Simplified Encryption)是一个用于Java应用程序的简化加密库,可以用来对敏感信息进行加密和解密。下面是在Spring Boot中使用Jasypt库的基本步骤:
-
在你的Spring Boot项目中,添加Jasypt的依赖。在
pom.xml
文件中添加以下依赖配置:<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.4</version> </dependency>
-
在
application.properties
(或application.yml
)配置文件中,设置需要加密的属性值。使用ENC(encrypted_value)
的格式来表示加密的属性值。myapp.password=ENC(encrypted_password)
-
在Spring Boot的配置类中,使用
@EnableEncryptableProperties
注解来启用属性值的加密功能。import org.springframework.context.annotation.Configuration; import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; @Configuration @EnableEncryptableProperties public class AppConfig { // 配置类的其他代码... }
-
在需要使用加密属性值的地方,使用
@Value
注解来注入加密的属性值。Spring Boot会自动解密并注入对应的属性值。import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class MyComponent { @Value("${myapp.password}") private String password; // 组件的其他代码... }