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 依赖

<!-- 加密工具 -->
<dependency>
       <groupId>com.github.ulisesbocchio</groupId>
       <artifactId>jasypt-spring-boot-starter</artifactId>
       <version>1.8</version>
</dependency>        

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中添加账户名和密码
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,578评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,275评论 6 342
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 100,708评论 9 468
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,094评论 25 709
  • 初春的清晨,薄薄的雾伴着淡淡的月光 路灯在身后闪烁着 昨夜的梦,我还没醒 梦中的酒,我还没喝 酒中的人,还没看清 ...
    蓝蓝月亮阅读 179评论 0 1

友情链接更多精彩内容