ulisesbocchio對配置檔案加密
阿新 • • 發佈:2020-08-06
GitHub地址:https://github.com/ulisesbocchio/jasypt-spring-boot
maven依賴
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency>
以下依賴需要加@EnableEncryptableProperties註解
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot</artifactId> <version>2.1.0</version> </dependency>
Jasypt Spring Boot 為 Spring Boot 專案中的屬性源(property sources)提供加密支援。
有三種方法可以在專案中整合 jasypt-spring-boot:
-
如果 Spring Boot 專案中使用了 @SpringBootApplication 或者 @EnableAutoConfiguration ,在專案裡新增jasypt-spring-boot-starter 依賴會自動對專案中整個屬性(包括系統屬性、環境屬性、命令列引數、application.properties, yaml)啟動加密。
-
如果專案裡沒有使用 @SpringBootApplication 或者 @EnableAutoConfiguration ,可以手動在 Configuration 類上添加註解 @EnableEncryptableProperties ,來在整個環境的屬性啟用屬性加密。
-
如果想指定加密檔案,可以使用 @EncryptablePropertySource 指定單個屬性源。
簡單點說就是 如果springboot專案mven依賴引入starter帶則不用加@EnableEncryptableProperties , 引入<artifactId>jasypt-spring-boot-starter</artifactId>則需要加@EnableEncryptableProperties註解
yml配置檔案
jasypt:
encryptor:
password: e!Jd&ljyJ^e4I5oU #隨便定義
datasource: driver-class-name: com.mysql.jdbc.Driver url: ENC(EFwaxIyXCXYwJo0tP0jY9jD1MfXLFJNIYG8kRpTLZGskQYrnfMrazE7APFfYWUSzySGQFqk66tP9/sJMuJJrZCpm36HRiSU53vT42/K/Z8KH2rCfSFw8zhPIVltH5FPfCAP9fJbJhwwScwCwbGXegthu5RpSgaE8cRIXt8E=) username: ENC(kEbiaqGlnNvRy7RKHhg==) password: ENC(4AG83a7fTXnmN5CCXVltrN0bWHv)