spring cloud config安全

前面两篇介绍了spring cloud config服务端客户端的简单配置,本篇介绍如何保护config server及对应config client修改。

保护config server,主要是使用spring security进行最简单的basic安全认证(也可自定义认证方式,这里不做扩展)

 

配置服务端代码示例:

在pom文件中增加依赖:

        <dependency>
            <!-- spring security 安全认证 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

配置文件application.yml增加security配置:

security:
  basic:
    enabled: true  #启用基本认证(默认)
  user:   #配置security用户名密码,默认值为“user”的用户名和随机生成的密码
    name: user
    password: password123

启动服务,测试一下。请求http://localhost:18083/master/config-server-dev.yml,会发现弹出了security的basic登录验证:

输入上面配置的用户名密码(user/password123),返回配置信息

 

客户端代码示例:

在pom文件中增加依赖:

        <dependency>
            <!-- spring security 安全认证 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

修改配置文件bootstrap.yml:

spring:
  application:
    name: config-client
  cloud:
    config:
      profile: test   #对应spring.profiles.active
      label: master   #分支名。当使用配置服务器是git时,默认是master
#      uri: http://localhost:18083  #更改配置服务器的位置,默认地址是http://localhost:8888
      uri: http://user:password123@localhost:18083  #配置服务器增加了安全认证,这里需要提供用户名密码
      username: user  #配置服务器的用户名密码,此配置会覆盖uri中的配置
      password: password123

在上面的配置中,可以在uri上加上用户名密码,也可以直接配置spring.cloud.config.usernamespring.cloud.config.password,不过后面这种会覆盖uri中的配置(至于为什么要设置这两种配置方式,是因为当spring cloud config与注册中心配合使用时,是没有uri的,所以只能在下面配置username和password)

启动服务,测试一下。请求http://localhost:18084/profile,返回配置信息:

至此,spring cloud config安全配置完成~ 

 

posted @ 2019-03-06 15:21  仅此而已-远方  阅读(1942)  评论(0编辑  收藏  举报