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;
    
        // 组件的其他代码...
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值