其他分享
首页 > 其他分享> > Springboot-配置文件加密

Springboot-配置文件加密

作者:互联网

一、PBEWithMD5AndDES加密算法

二、springboot集成jasypt

pom

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

配置密钥

配置文件添加
jasypt.encryptor.password: demo

获取密文

//如果有多个配置文件,可用通过ActiveProfiles来指定配置文件;将匹配到配置文件:application-18011.properties
@ActiveProfiles("18011")
@SpringBootTest(classes = DemoApplication.class)
public class PigAdminApplicationTest {
	//从spring环境中获取bean,这个bean是从哪里来的呢?jasypt的starter
    @Autowired
    private StringEncryptor stringEncryptor;

    @Test
    public void testEnvironmentProperties() {
    	//将明文加密
        System.out.println(stringEncryptor.encrypt("root"));
        System.out.println(stringEncryptor.encrypt("123456"));
        //解密
        System.out.println(stringEncryptor.decrypt("bsK6LoI1XJrAFvynypY2Og=="));
    }
}

在配置文件中使用

spring.datasource.username=ENC(PquhbjSL8UlUCK91LvuJNg==)
spring.datasource.password=ENC(8r2VooGyAOd+Q2+FpgHu8Q==)

springboot启动时,几经通过密钥将密文解密,所以密钥将称为破译关键,所以需要:
java -jar xxx.jar --jasypt.encryptor.password=xxx
将密钥从项目中移除

更安全方案

1.拥有一个配置中心
2.配置中心有高度保密的配置文件
3.各项目向中心注册公钥,加密传输
4。项目内部通过私钥解密

常见问题

1.

jasypt.encryptor.password: demo缺失

java.lang.IllegalStateException: Required Encryption configuration property missing: jasypt.encryptor.password

标签:加密,Springboot,配置文件,jasypt,spring,stringEncryptor,encryptor,password
来源: https://www.cnblogs.com/codetxj/p/14788494.html