SpringCloud集成Security安全(Config配置中心)


1.說明

為了保護配置中心的敏感數據,
需要對Config Server進行安全保護,
本文基於Spring Security方案,
為Config Server增加最簡單的Basic安全認證。

2.Config Server添加安全依賴

修改pom.xml,添加spring-boot-starter-security依賴:

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

3.Config Server配置用戶名密碼

修改application.yml,配置用戶名密碼:

spring:
  security:
    user:
      name: config
      password: config123456

4.啟動Config Server,測試驗證

先只啟動Config Server,
通過瀏覽器的URL訪問配置中心,
http://localhost:9009/master/application-dev.yml
首先會跳到登錄界面,
要求輸入用戶名密碼:


認證成功后,訪問到對應的配置:

5.Config Client配置用戶名密碼

客戶端要訪問配置中心,
需要修改bootstrap.yml,
配置上面的用戶名密碼:

spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
      username: config
      password: config123456

上面是通過service-id指定配置中心,
如果使用uri方式指定配置中心,
不僅可以通過username和password配置用戶名密碼:

spring:
  cloud:
    config:
      uri: http://localhost:9009
      username: config
      password: config123456

還可以在uri中配置用戶名密碼:

spring:
  cloud:
    config:
      uri: http://config:config123456@localhost:9009

如果在uri中指定了用戶名密碼,
又在下面設置username和password,
則以username和password配置的為准:

spring:
  cloud:
    config:
      label: master
      name: config-client-demo
      profile: test
      uri: http://config:configXXX@localhost:9009
      username: config
      password: config123456

上面的配置中uri的密碼錯誤了,
但是password配置的是正確的,
Config Client仍然能夠正確訪問配置中心。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM