Spring Boot 3 集成 Jasypt:如何开启自动解密

我知道在 Spring Boot 3 中集成 Jasypt 可以帮助我们实现加密的配置文件和敏感信息。但是,如何开启自动解密呢?今天,我们要谈论的是如何在启动类上添加注解 @EnableEncryptableProperties 来开启自动解密。

什么是@EnableEncryptableProperties

@EnableEncryptableProperties 是一个注解,在 Spring Boot 3 中提供的,可以用来开启自动解密。它会扫描所有配置文件,并将加密后的值自动解密。

如何使用@EnableEncryptableProperties

在启动类上添加 @EnableEncryptableProperties 注解即可开启自动解密:

@SpringBootApplication
@EnableEncryptableProperties
public class DemoApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

源码解析

在 spring-boot-autoconfigure-3.0.0-SNAPSHOT.jar 中,找到 org.springframework.boot.autoconfigure.EnableAutoConfigureProperties.java 文件:

public @interface EnableAutoConfigureProperties {
    
    String[] value() default {};
    
    Class<?>[] basePackages() default {};
    
}

在这个文件中,我们可以看到 EnableAutoConfigureProperties 注解的定义。我们可以通过这个注解来开启自动解密。

如何使用@EnableEncryptableProperties

在 application.properties 或 application.yml 文件中添加加密后的值:

# application.properties
encrypt.password=123456

或者:

# application.yml
spring:
  encrypt:
    password: 123456

应用案例

在实际项目中,我们可以通过 @EnableEncryptableProperties 来开启自动解密,避免手动解密的麻烦。

总之,通过本文,我们应该能够轻松地使用 @EnableEncryptableProperties 来开启自动解密,在 Spring Boot 3 中集成 Jasypt。


来源: 互联网
本文观点不代表源码解析立场,不承担法律责任,文章及观点也不构成任何投资意见。

赞 ()

相关推荐

发表回复

评论列表

点击查看更多

    联系我们

    在线咨询: QQ交谈

    微信:13450247865

    邮件:451255340#qq.com

    工作时间:周一至周五,9:30-18:30,节假日休息

    微信