配置中敏感信息加密(jasypt)

本文介绍如何利用Jasypt在项目中实现配置文件的加密功能,包括引入依赖、添加注解、改造启动类、使用指令或代码进行加密及在配置文件中使用加密后的密文。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.启动模块中引入jasypt的依赖

<!--配置加解密--> 
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>2.1.2</version>
</dependency>

2.启动类中添加注解@EnableEncryptableProperties

3.如果使用xml注入参数的方式,根据官方文档描述,除引入jar包外,还需要改造启动类main方法

 public static void main(String[] args) {

        try {
            //SpringApplication.run(HsaHuaweiConsumerApplication.class, args);

            new SpringApplicationBuilder()
                .environment(new StandardEncryptableEnvironment())
                .sources(HsaHuaweiConsumerApplication.class).run(args);

        } catch (Exception e) {
            e.printStackTrace();
        }

}

4.使用jar包指令或者代码的方式进行加密

jar包指令:

java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="明文" password=hxmec algorithm=PBEWithMD5AndDES

        input:为需加密的明文参数。
        password : 是自己自定义的密钥。
        algorithm : 使用的加密算法

代码方式:

BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt(盐)
textEncryptor.setPassword("hxmec");
//要加密的数据(数据库的用户名或密码)
String username = textEncryptor.encrypt("dp");
String password = textEncryptor.encrypt("dp123456!");
System.out.println("username:"+username);
System.out.println("password:"+password);

5.配置中的需要加密的信息加密后,将密文用ENC()包起来

password: ENC(D5sqCB7gj4OmVYos44zyj6NHSxeG7t3T)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值