【Java】springboot整合jasypt

本文介绍了如何在SpringBoot项目中集成Jasypt库,包括添加Maven依赖、在启动类上添加注解、配置加密参数以及编写测试类来演示加解密过程。通过`@EnableEncryptableProperties`注解确保密码以安全方式存储。
摘要由CSDN通过智能技术生成

jasypt

保证项目中的账号密码不以明文的形式展示

springboot集成jasypt

1.引入maven依赖

<dependency>
	<groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.4</version>
</dependency>

2.启动类添加注解

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

3.yaml配置

jasypt:
  encryptor:
    password: 02700083-9fd9-4b82-a4b4-9177e0560e92
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator
my:
  username: ENC(atRC+VNwB17CQVilGftfQg==)
  password: ENC(Or0FKbtskiXsJlFtI23FxA==)

4.加解密测试类

import org.jasypt.util.text.BasicTextEncryptor;

public class Test01 {

    public static void main(String[] args) {
        //该类的选择根据algorithm:PBEWithMD5AndDE选择的算法选择
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        encryptor.setPassword("02700083-9fd9-4b82-a4b4-9177e0560e92");
        String encrypt = encryptor.encrypt("root");
        System.out.println(encrypt);
        String decrypt = encryptor.decrypt(encrypt);
        System.out.println(decrypt);
        encrypt = encryptor.encrypt("mysql");
        System.out.println(encrypt);
        decrypt = encryptor.decrypt(encrypt);
        System.out.println(decrypt);
    }

}

读取配置效果

@RestController
public class IpController implements InitializingBean {

    @Value("${my.username}")
    private String username;

    @Value("${my.password}")
    private String password;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("username:"+username+",password:"+password);
    }
}
  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值