spring boot 配置文件安全策略

为了避免配置文件中关键的信息暴露出来,需要进行安全控制。首先需要对配置文件中关键字段进行加密,避免明文显示。其次是需要将配置文件进行集中储存,并进行安全控制。

1、配置文件中账号和密码进行加密

由于在配置文件中需要配置jdbc的相关参数,大多数人习惯用明文进行配置,如下:

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、一旦服务器遭受攻击,很容易通过反编译获取配置文件中数据库的账号和密码
2、账号和密码时常暴露在外面,一些没有权限的开发人员也易于获取相关信息,于安全无益

解决方案:
由于spring boot 已经集成了jasypt,所以添加相关依赖即可

1.1 添加maven 依赖



       com.github.ulisesbocchio
       jasypt-spring-boot-starter
       1.8
        

1.2. 在配置文件中配置加密参数(可以理解为加密的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 进行统一管理

根据springCloudConfig的规范,

2.1 进行相关配置创建psring cloud config Service

2.2 在应用中配置bootstrap.yml文件

spring:
  profiles:
    active: uat

  application:
    name: athena #应用名称
  cloud:
    config:
       env: uat #环境名称
       label: dev # 分支名称
       uri: http://localhost:9090  #springCloudConfig Service 服务url

3、对springCloudConfig的访问权限进行限制

3.1 在springCloudConfig service 中设置访问权限

在bootstrap.yml中设置加密信息

security:
  basic:
    enabled: true
  user:
    name: user
    password: pwd

3.2 在sprintCloudConfig Client 中设置访问权限

spring:
  profiles:
    active: uat

  application:
    name: athena
  cloud:
    config:
       env: uat
       label: dev
       uri: http://user:pwd@localhost:9090  #url中添加账户名和密码

你可能感兴趣的:(spring boot 配置文件安全策略)