一般情况下配置文件都是很重要、很敏感的,所以需要为Config Server加上验证功能。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
在服务器端的配置文件中设置"配置服务器"的用户名和密码
#用户名
security.user.name=username
security.user.password=password
spring.cloud.config.username=username
spring.cloud.config.password=password
如果要使用对称加密,则需要设置对称加密的密钥。设置方式简单,在配置文件bootstrap.properties(需要自己创建)中加入以下代码:
#设置对称加密密钥
encrypt.key=liu
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=
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
非对称加密需要用到Java制作证书的工具keytool。可以通过keytool工具提供的命令"-genkeypair"来创建证书
keytool -genkeypair -alias "alias test" -keypass "keypass" -keyalg "RSA" -storepass "storepass"
把生成的非对称密钥复制到Spring Cloud Config项目的classpath目录下
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
启动项目后,用POST方式提交key到http://localhost:50027/encrypt即可完成加密
用POST方式提交key到http://localhost:50027/decrypt即可完成解密