SpringBoot—jasypt加解密库的使用方法

Jasypt(Java Simplified Encryption)是一个用于Java应用程序的简化加密库,可以用来对敏感信息进行加密和解密。下面是在Spring Boot中使用Jasypt库的基本步骤:

  1. 在你的Spring Boot项目中,添加Jasypt的依赖。在 pom.xml文件中添加以下依赖配置:

    <dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>3.0.4</version>
    </dependency>
     
     
  2. 在 application.properties(或 application.yml)配置文件中,设置需要加密的属性值。使用 ENC(encrypted_value)的格式来表示加密的属性值。

    myapp.password=ENC(encrypted_password)
     
     
  3. 在Spring Boot的配置类中,使用 @EnableEncryptableProperties注解来启用属性值的加密功能。

    import org.springframework.context.annotation.Configuration;
    import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
    
    @Configuration
    @EnableEncryptableProperties
    public class AppConfig {
        // 配置类的其他代码...
    }
     
     
  4. 在需要使用加密属性值的地方,使用 @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;
    
        // 组件的其他代码...
    }
posted @ 2025-03-19 16:22  yes_go  阅读(364)  评论(0)    收藏  举报