阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

设置配置中心的安全

153次阅读
没有评论

共计 1620 个字符,预计需要花费 5 分钟才能阅读完成。

1、设置配置中心的验证

一般情况下配置文件都是很重要、很敏感的,所以需要为 Config Server 加上验证功能。

1.1、引入依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>

1.2、配置 ” 配置服务器 ” 的用户名和密码

在服务器端的配置文件中设置 ” 配置服务器 ” 的用户名和密码

# 用户名
security.user.name=username
security.user.password=password

1.3、在客户端的配置文件中设置 ” 配置服务器 ” 的用户名和密码

spring.cloud.config.username=username
spring.cloud.config.password=password

2、加 / 解密配置文件

2.1、配置对称加密密钥

2.1.1、设置对称加 / 解密配置文件

如果要使用对称加密,则需要设置对称加密的密钥。设置方式简单,在配置文件 bootstrap.properties(需要自己创建) 中加入以下代码:

# 设置对称加密密钥
encrypt.key=liu

2.1.2、添加配置

spring.application.name=config-server
server.port=50027
# 配置 git 仓库的地址
spring.cloud.config.server.git.uri=https://github.com/lingfengxeon/spring-config
# git 仓库地址下的相对地址,可以配置多个,用, 分割。spring.cloud.config.server.git.search-paths=spring-config
# git 仓库的账号
username=
# git 仓库的密码
password=

2.1.3、修改启动类

@SpringBootApplication @EnableConfigServer public class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args); } }

2.1.4、访问:http://localhost:50027/encrypt/status

设置配置中心的安全

2.1.5、使用 Post Man 访问:http://localhost:50027/encrypt

设置配置中心的安全

2.2、用非对称加密方式加 / 解密配置文件

2.2.1、生成 keystore

非对称加密需要用到 Java 制作证书的工具 keytool。可以通过 keytool 工具提供的命令 ”-genkeypair” 来创建证书

keytool -genkeypair -alias "alias test" -keypass "keypass" -keyalg "RSA" -storepass "storepass"

2.2.2、复制密钥文件

把生成的非对称密钥复制到 Spring Cloud Config 项目的 classpath 目录下

2.2.3、添加配置信息

encrypt.key-store.location=classpath/test.jks
#alias
encrypt.key-store.alias=alias test
#密钥 key
encrypt.key-store.secret=keypass
#密码是
encrypt.key-store.password=storepass

2.2.4、测试加密

启动项目后,用 POST 方式提交 key 到 http://localhost:50027/encrypt 即可完成加密

设置配置中心的安全

2.2.5、解密

用 POST 方式提交 key 到 http://localhost:50027/decrypt 即可完成解密

设置配置中心的安全

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-06-06发表,共计1620字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中