Springboot-配置文件加密

1、PBEWithMD5AndDES加密算法

2、springboot集成jasypt

pomjava

        com.github.ulisesbocchiojasypt-spring-boot-starter2.1.0

配置密鑰git

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

獲取密文github

//若是有多個配置文件,可用經過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
將密鑰從項目中移除spring

更安全方案安全

1.擁有一個配置中心
2.配置中心有高度保密的配置文件
3.各項目向中心註冊公鑰,加密傳輸
4。項目內部經過私鑰解密

常見問題

1.

jasypt.encryptor.password: demo缺失springboot

java.lang.IllegalStateException: Required Encryption configuration property missing: jasypt.encryptor.password
相關文章
相關標籤/搜索