nacos配置敏感数据加密(使用jasypt)

1.引入依赖
<!-- 加密工具 -->
<dependency>    
    <groupId>com.github.ulisesbocchio</groupId>    
    <artifactId>jasypt-spring-boot-starter</artifactId>    
    <version>3.0.5</version>
</dependency>

<!-- 使用单元测试生成密钥 -->
<dependency>    
    <groupId>org.springframework.boot</groupId>    
    <artifactId>spring-boot-starter-test</artifactId>
 </dependency>
2.配置yml里面的加密算法
jasypt:
  encryptor:
    #自定义 加密因子。随便写
    password: ALK9382LDF234UIJ79GJ0F5HD
    # 官方默认的加密算法是 PBEWITHHMACSHA512ANDAES_256 但是如果用的是jdk1.8 不支持需要改成 PBEWithMD5AndDES
    algorithm: PBEWithMD5AndDES
    # 配置格式(不写默认ENC)
    property:
      prefix: "ENC["
      suffix: "]"
3.生成密文
import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;

@SpringBootTest
public class Encrypt {

    @Resource 
    private StringEncryptor stringEncryptor;    
    @Test 
    public void encryptPwd() {
        String user = stringEncryptor.encrypt("root");        
        System.out.println("user: " + user);        
        String pass = stringEncryptor.encrypt("135789");        
        System.out.println("pass: " + pass);   
    }

}
4.nacos配置修改
spring:
  datasource:
    druid:
      datasource:
          master:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
            username: ENC(DzmdJtfNcBngn0Ya+UHVOUJ0fSuFMO6n)
            password: ENC(sxEwCbBiaY1JNBUjr8RHKgdqB6ET9avktVNsvSe4Pa4=)
5.启动类增加注解 @EnableEncryptableProperties
@EnableEncryptableProperties
@SpringBootApplication
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(BusinessApplication.class, args);    }
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Nacos配置中心提供了配置文件加密功能,可以对配置文件中的敏感信息进行加密处理。这样可以保护敏感信息,如数据库连接方式或第三方存储系统的相关信息。在Nacos的2.1版本中,新增了配置文件加密功能,无需自己实现,可以轻松实现配置内容的AES加密。\[1\] 要使用Nacos配置中心的加密功能,可以在启动配置类bootstrap.yml中添加相关信息。可以通过自定义加解密类来实现加解密操作,也可以使用jasypt默认的StringEncryptor进行加解密。在配置文件中添加jasypt的相关配置,包括加解密类的bean和盐值。\[2\] 配置文件会存储在Nacos的config_info表中,配置内容存储在content字段中。如果配置加密,content字段中存储的是密文,但在Nacos客户端展示配置时会以明文形式显示。这样可以保证配置的安全性,同时方便查看配置内容。\[3\] #### 引用[.reference_title] - *1* *3* [【Nacos配置中心】对配置文件内容进行加密](https://blog.csdn.net/weixin_43888891/article/details/126783468)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [nacos配置文件中敏感信息加密](https://blog.csdn.net/goldenminers/article/details/120488006)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值