目录
一、简介
上一篇文章中简单搭建了一个配置中心,但是github对于非付费用户是完全公开的,因此如果配置文件中有敏感信息,比如spring security的用户名密码,数据库的连接地址,用户名密码等。这些信息我们不希望别人通过配置中心服务暴露的接口进行查询。因此我们需要进行一些安全加固。
二、登录验证
这个很简单,可以使用spring security,在通过api获取配置文件时进行登录验证,客户端要想连接配置中心,同样需要用户名密码。
引入spring-boot-starter-security
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
配置用户名密码
spring:
security:
user:
name: 用户名
password: 密码
客户端配置配置中心连接时加上用户名密码
spring:
application:
name: config-demo
cloud:
config:
uri: http://用户名:密码@localhost:8868/manage/serverConfig
三、加密
我们将配置文件放到了github上,如果不是付费用户的话,任何人都可以看到上面的配置文件,因此除了上面提到的登录验证以外,我们还要对配置文件中的敏感信息加密。
配置key
encrypt:
key: key
不对加密和解密进行csrf防护
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.requestMatchers(EndpointRequest.toAnyEndpoint()).authenticated()
.and()
.csrf().ignoringAntMatchers("/encrypt/**", "/decrypt/**");
super.configure(http);
}
}
调用/encrypt可以加密,调用/decrypt可以解密
修改配置文件,将加密信息用加密后的暗文替换,同时加上{cipher}前缀,表示这是加密信息
luminary:
test: '{cipher}5fefae1ba4e31e7240356d274a787585555482eec06a6df53ad6f74c53b8af34'