springCloud微服務系列——配置中心第三篇——安全加固

目錄

一、簡介

二、登錄驗證

三、加密


一、簡介

      上一篇文章中簡單搭建了一個配置中心,但是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'

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章