一 介绍
Config Server是允许匿名访问的。为了防止配置内容的外泄,应该保护Config Server的安全。有多种方式可以做到这一点,例如通过物理网络安全,或者Config Server添加用户认证等。
二 为Config Server添加基于HTTP Basic的用户认证
1 新建项目microservice-config-server-eureka-authenticating
2 为项目添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
3 在application.yml中添加如下内容:
server:
port: 8080
spring:
application:
name: microservice-config-server-eureka
cloud:
config:
server:
git:
uri:https://git.oschina.net/itmuch/spring-cloud-config-repo# 配置Git仓库的地址
username: # Git仓库的账号
password: # Git仓库的密码
eureka:
client:
serviceUrl:
defaultZone:http://localhost:8761/eureka/
security:
basic:
enabled: true # 开启基于HTTP basic的认证
user:
name: user # 配置登录的账号是user
password: password123 # 配置登录的密码是password123
4 启动该服务,并输入
http://localhost:8080/microservice-foo/dev
5 输入用户名user和密码password123
三 Config Client连接需用户认证的Config Server
Config Client有两种方式使用需要用户认证的Config Server
1 使用curl风格的URL
spring:
cloud:
config:
uri: http://user:password123@localhost:8080/
2 指定Config Server的账号和密码
spring:
cloud:
config:
uri: http://localhost:8080/
username:user
password:password123
需要注意的是spring.cloud.config.password和spring.cloud.config.username的优先级较高,它们会覆盖URL中包含的账号和密码。
四 Config Client认证Config Server项目实战
1 新建项目microservice-config-client-eureka-authenticating
2 bootstrap.yml配置如下
spring:
application:
name: microservice-foo # 对应config server所获取的配置文件的{application}
cloud:
config:
username: user
password: password123
profile: dev
label: master
discovery:
enabled: true # 表示使用服务发现的configserver配置,而不是自己配置的Config Server的uri,默认false
service-id: microservice-config-server-eureka # 指定Config Server在服务发现中的serviceId,默认是configserver
eureka:
client:
serviceUrl:
defaultZone:http://localhost:8761/eureka/
3 启动microservice-config-client-eureka-authenticating