设置配置中心的安全
阅读 (9844287)
分享
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即可完成解密
需要
登录
才可以提问哦
: