使用過SpringBoot配置文件的朋友都知道,資源文件中的內容一般狀況下是明文顯示,安全性就比較低一些。打開application.properties或application.yml,好比mysql登錄密碼,redis登錄密碼以及第三方的密鑰等等盡收眼底,這裏介紹一個加解密組件,提升一些屬性配置的安全性。mysql
jasypt由一個國外大神寫的一個springboot下的工具包。Git地址:https://github.com/ulisesbocchio/jasypt-spring-bootgit
直接上代碼:github
一、pom文件中增長依賴:redis
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
二、application.yml文件中增長jasypt的祕鑰(該祕鑰自定義的):spring
jasypt:
encryptor:
#加密祕鑰
password: EbfYkitulv73I2p0mXI50JMXoaxZTKJ7
三、測試類:sql
@RestController public class IndexController { @Autowired private StringEncryptor encryptor; /** * 測試jasypt加密解密 */ @GetMapping("/jasypt") public void testJasypt() { String password = "123456"; String encryptPwd = encryptor.encrypt(password); System.out.println("加密::" + encryptPwd); System.out.println("解密:" + encryptor.decrypt(encryptPwd)); } }
四、啓動服務,在控制檯能夠看到jaspyt的默認配置(好比加密方式是PBEWithMD5AndDES):數據庫
五、在瀏覽器請求(端口號自定義):http://localhost:18081/jasypt ,在控制檯打印信息:瀏覽器
加密: stqvirrvG8TcLz9mqflBDQ== 解密:123456
jasypt因爲其使用的是PBEWithMD5AndDES加密方式,因此每次加密出來的結果都不同,可是解密都是同樣的,因此很適合對數據進行加密安全
六、將加密的結果替換配置文件中的敏感字段(這裏以數據庫密碼爲例,數據庫密碼是123456):springboot
spring: application: name: service-provider datasource: driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mydb?autoReconnect=true&failOverReadOnly=false&createDatabaseIfNotExist=true&useSSL=false&useUnicode=true&characterEncoding=utf8 username: root password: ENC(stqvirrvG8TcLz9mqflBDQ==)
上面的 ENC()是固定寫法,()裏面是加密後的信息。
七、在測試類中增長代碼:
@Value("${spring.datasource.password}") private String dbPassword; // 數據庫密碼 /** * 測試配置文件字段加密後,項目中該字段的值 */ @GetMapping("/password") public String password() { return dbPassword; }
請求http://localhost:18081/password,會發現結果是123456,說明直接將加密文件解密出來了
擴展
a、自定義上面第7點中ENC()的固定寫法。在配置文件中增長jasypt.encryptor.property:
jasypt: encryptor: #加解密祕鑰 password: EbfYkitulv73I2p0mXI50JMXoaxZTKJ7 #設置前綴後綴 property: prefix: "ENC@[" suffix: "]"
b、將加解密祕鑰放在配置文件中是不安全的,有以下幾種解決辦法:
一、在啓動類上賦值祕鑰:
@SpringBootApplication public class ProviderApplication { public static void main(String[] args) { /** 配置加解密祕鑰,與配置文件的密文分開放 */ System.setProperty("jasypt.encryptor.password", "travel-app"); // System.setProperty("jasypt.encryptor.password", "EbfYkitulv73I2p0mXI50JMXoaxZTKJ7"); SpringApplication.run(ProviderApplication.class, args); } }
二、自定義StringEncryptor:
/** * 配置StringEncryptor */ @Bean("jasyptStringEncryptor") public StringEncryptor stringEncryptor() { PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); SimpleStringPBEConfig config = new SimpleStringPBEConfig(); config.setPassword("EbfYkitulv73I2p0mXI50JMXoaxZTKJ7"); config.setAlgorithm("PBEWithMD5AndDES"); config.setKeyObtentionIterations("1000"); config.setPoolSize("1"); config.setProviderName("SunJCE"); config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); config.setIvGeneratorClassName("org.jasypt.salt.NoOpIVGenerator"); config.setStringOutputType("base64"); encryptor.setConfig(config); return encryptor; }
三、使用jar的方式啓動
踩過的坑
a、必須配置加解密用的祕鑰,即jasypt.encryptor.password,否則啓動會報錯
b、springboot2.x如下的版本,只能用jasypt1.x的版本。若是用jasypt2.x的版本,啓動會報錯,詳見https://github.com/ulisesbocchio/jasypt-spring-boot/issues/97