在日前安全形勢愈來愈嚴重的狀況下,讓我意識到在項目中存在一個咱們常常忽略的漏洞,那就是咱們的項目的配置文件中配置信息的安全,尤爲是數據庫鏈接的用戶名和密碼的安全。因此這裏咱們就須要對數據庫的用戶名和密碼進行加密,這也是本文的由來。本文采用Jasypt對Spring Boot配置文件加密的相關方法,其實呢,也還有其餘方案,具體的會在後面的相關文章中說明。java
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
@Test public void contextLoads() { BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); //加密所需的salt(鹽) textEncryptor.setPassword("1Qaz0oKm"); //要加密的數據(數據庫的用戶名或密碼) String username = textEncryptor.encrypt("root"); String password = textEncryptor.encrypt("root"); System.out.println("username:"+username); System.out.println("password:"+password); }
輸出信息mysql
username:NZmLHOOHX0SEjc285iG9YQ== password:1JByM5wu5o+9H1Ba2o++Pg== 2019-06-14 14:55:49.863 INFO 8904 --- [ Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 2019-06-14 14:55:49.863 INFO 8904 --- [ Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2019-06-14 14:55:49.863 INFO 8904 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2019-06-14 14:55:49.878 INFO 8904 --- [ Thread-3] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password=1Qaz0oKm algorithm=PBEWithMD5AndDES input=root
輸出信息git
----ENVIRONMENT----------------- Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.171-b11 ----ARGUMENTS------------------- input: root algorithm: PBEWithMD5AndDES password: 1Qaz0oKm ----OUTPUT---------------------- NZmLHOOHX0SEjc285iG9YQ==
拷貝-OUTPUT-下的結果便可github
將生成的加密串配置ENC(加密串)到application.properties中spring
server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true username: ENC(GHK23XVFNHoQQ97vIW523Q==) password: ENC(aTKef0XcG05Cfzao92EqqQ==) data-username: com.mysql.cj.jdbc.Driver jpa: show-sql: true database-platform: org.hibernate.dialect.MySQL5InnoDBDialect database: MYSQL hibernate: ddl-auto: update jasypt: encryptor: password: 1Qaz0oKm #加密所需的salt(鹽) #algorithm: PBEWithMD5AndDES # 默認加密方式PBEWithMD5AndDES,能夠更改成PBEWithMD5AndTripleDES
加密方式對應的類爲BasicTextEncryptor和StrongTextEncryptorsql
private final StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); public BasicTextEncryptor() { this.encryptor.setAlgorithm("PBEWithMD5AndDES"); }
private final StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); public StrongTextEncryptor() { this.encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); }
1. 爲了防止salt(鹽)泄露,反解出密碼.能夠在項目部署的時候使用命令傳入salt(鹽)值數據庫
java -jar -Djasypt.encryptor.password=1Qaz0oKm xxx.jar
2. 或者在服務器的環境變量裏配置,進一步提升安全性vim
打開/etc/profile文件 vim /etc/profile 文件末尾插入 export JASYPT_PASSWORD = G0CvDz7oJn6 編譯 source /etc/profile 運行 java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar
下面是一個我本身的具體實現:https://github.com/eelve/jasypt,使用Jasypt對數據庫用信息加密後,能夠成功鏈接上數據庫
安全