Spring boot 配置文件加密

  1. 引入依赖
        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
  1. 加密相关代码
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class Encrytor {
    @Autowired
    StringEncryptor encryptor;


    @Test
    public void getPass() {
    	
    	//设置加密的盐
        System.setProperty("jasypt.encryptor.password", "");
        String url = encryptor.encrypt("jdbc:mysql://47.97.192.116:3306/sell?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8");
        String name = encryptor.encrypt("132465");
        String password = encryptor.encrypt("132456");
        System.out.println(url + "----------------");
        System.out.println(name + "----------------");
        System.out.println(password + "----------------");

        System.out.println(encryptor.decrypt("QXlVjlDyClggpdXxujkufQ=="));
        Assert.assertTrue(name.length() > 0);
        Assert.assertTrue(password.length() > 0);
    }

}

3.配置文件使用

//ENC() 是固定方法
pring.datasource.url=ENC(加密后的字符串)

配置文件
jasypt.encryptor.password=任意值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用加密算法来加密配置文件,以保护敏感信息的安全性。以下是一种常见的加密配置文件的方法: 1. 导入依赖:在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-crypto</artifactId> </dependency> ``` 2. 创建一个加密/解密工具类:可以使用`TextEncryptor`接口提供的实现类来进行加密和解密操作。例如,可以使用`Jasypt`库提供的实现。 3. 配置加密密钥:在`application.properties`或`application.yml`配置文件中,添加一个密钥属性。例如: ```properties jasypt.encryptor.password=mySecretKey ``` 4. 加密配置信息:可以使用上一步中配置的密钥来加密敏感信息。例如,可以在配置文件中使用`ENC(encryptedValue)`的形式来表示加密后的值。例如: ```properties my.property=ENC(encryptedValue) ``` 5. 解密配置信息:在需要访问敏感信息的地方,可以使用加密/解密工具类来解密配置值。例如,在Spring Boot应用程序中,可以使用`@Value`注解将解密后的值注入到属性中。 ```java @Value("${my.property}") private String myProperty; ``` 这样,敏感信息就会在配置文件中以加密形式存储,并且在应用程序中自动解密以供使用。 请注意,为了确保密钥的安全性,建议将密钥存储在安全的位置,并且不要将明文密钥直接硬编码在代码中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值