為了避免配置文件中關(guān)鍵的信息暴露出來,需要進(jìn)行安全控制。首先需要對配置文件中關(guān)鍵字段進(jìn)行加密,避免明文顯示。其次是需要將配置文件進(jìn)行集中儲存,并進(jìn)行安全控制。
1、配置文件中賬號和密碼進(jìn)行加密
由于在配置文件中需要配置jdbc的相關(guān)參數(shù),大多數(shù)人習(xí)慣用明文進(jìn)行配置,如下:
spring:
application:
name: athena
profiles: uat
datasource:
url: jdbc:mysql://localhost:3306/athena?autoReconnect=true&useSSL=false
username: root
password: ENC(uNwnq8guBUeEdlSe0BXgVRx9PugA+cjv) #pwd
driver-class-name: com.mysql.jdbc.Driver
這樣的配置有兩個很大的弊端:
1、一旦服務(wù)器遭受攻擊,很容易通過反編譯獲取配置文件中數(shù)據(jù)庫的賬號和密碼
2、賬號和密碼時常暴露在外面,一些沒有權(quán)限的開發(fā)人員也易于獲取相關(guān)信息,于安全無益
解決方案:
由于spring boot 已經(jīng)集成了jasypt,所以添加相關(guān)依賴即可
1.1 添加maven 依賴
<!-- 加密工具 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.8</version>
</dependency>
1.2. 在配置文件中配置加密參數(shù)(可以理解為加密的salt)
jasypt:
encryptor:
password: BdaObXaELAA #(或者用123456)
1.3 使用加密
jasypt:
encryptor:
password: e!Jd&ljyJ^e4I5oU
spring:
application:
name: athena
profiles: uat
datasource:
url: jdbc:mysql://localhost:3306/athena?autoReconnect=true&useSSL=false
username: root
password: ENC(uNwnq8guBUeEdlSe0BXgVRx9PugA+cjv) #root
driver-class-name: com.mysql.jdbc.Driver
1.4 加密密碼
public class JasypTest {
@Autowired
StringEncryptor stringEncryptor;
@Test
public void encryptPwd() {
String result = stringEncryptor.encrypt("your paasword 123456");
System.out.println(result);
}
}
2、配置文件放入spring cloud config 進(jìn)行統(tǒng)一管理
根據(jù)springCloudConfig的規(guī)范,
2.1 進(jìn)行相關(guān)配置創(chuàng)建psring cloud config Service
2.2 在應(yīng)用中配置bootstrap.yml文件
spring:
profiles:
active: uat
application:
name: athena #應(yīng)用名稱
cloud:
config:
env: uat #環(huán)境名稱
label: dev # 分支名稱
uri: http://localhost:9090 #springCloudConfig Service 服務(wù)url
3、對springCloudConfig的訪問權(quán)限進(jìn)行限制
3.1 在springCloudConfig service 中設(shè)置訪問權(quán)限
在bootstrap.yml中設(shè)置加密信息
security:
basic:
enabled: true
user:
name: user
password: pwd
3.2 在sprintCloudConfig Client 中設(shè)置訪問權(quán)限
spring:
profiles:
active: uat
application:
name: athena
cloud:
config:
env: uat
label: dev
uri: http://user:pwd@localhost:9090 #url中添加賬戶名和密碼