springCloud(19):統一管理微服務配置-配置內容的加解密和與Eureka配合使用

1、配置內容的加解密
html

對於某些敏感的配置內容(如:帳號、密碼)應當加密存儲。java

Config Server爲配置內容的加密和解密提供了支持。git

1.一、安裝JCEspring

Config Server的加密解密功能依賴Java Cryptography Extension(JCE).
bootstrap

Java8 JCE的地址是:http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html oracle


替換jdk/jre/lib/security目錄中的兩個jarapp

1.二、Config Server的加解密端點ide

Config Server提供了加密與解碼的端點,分別是/encrypt與/decrypt。使用的是對稱加密微服務

a、建立項目config-server-encryption測試

b、配置application.yml配置文件

server:
  port: 5020
#端點的配置
endpoints:
  sensitive: true
  shutdown:
    enabled: true
management:
  security:
    enabled: false
spring:
  application:
    name: config-server-encryption
  cloud:
    config:
      server:
        git:
          uri: https://git.oschina.net/wadjz/spring-cloud-config.git
          username: 
          password: 
          clone-on-start: true
encrypt:
  key: wadjz # 設置對稱加密的密鑰

c、測試

加密:http://localhost:5020/encrypt,如加密:zhangsan

  wKiom1mmNlbSDdbxAABiKkRM450850.jpg

解密:http://localhost:5020/decrypt,如解密:87ba2ef5645aec689fde5a1c812eccee0226f66a8cc114a2d44a48d0aa92753b 

 wKioL1mmPKKT_-xZAABbkxxEeDs775.jpg

1.三、存儲加密的內容

加密後的內容,可以使用{cipher}密文的形式存儲。

一、準備一個配置文件encryption.yml

spring:
  datasource: 
    username: root
    password: '{cipher}87ba2ef5645aec689fde5a1c812eccee0226f66a8cc114a2d44a48d0aa92753b'

將其push到Git上

注意:yml文件這裏的{cipher}必定要用單引號,properties則不用。

二、使用http://localhost:5020/encryption-default.yml可獲取以下內容:

spring:
  datasource:
    password: zhangsan
    username: root

說明Config Server能自動解密配置內容。


一些場景下,想要讓Config Server直接返回密文自己,而不是解密後的內容,可設置spring.cloud.config.server.encrypt.enabled=false,這時可由Config Client自行解密。

1.四、非對稱加密

前面說的都是對稱加密,Spring cloud一樣支持非對稱加密。

a、建立項目config-server-encryption-rsa

b、執行如下命令,並按照提示操做,便可建立一個Key Store

keytool -genkeypair -alias mytestkey -keyalg RSA -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" -keypass changeme -keystore d:/server.jks -storepass letmein

c、將生成的server.jks文件複製到項目的classpath下

d、在application.yml中添加以下配置

encrypt:
  key-store:
    alias: mytestkey      # alias
    secret: changeme      # keypass
    password: letmein     # storepass
    location: classpath:/server.jks # jks文件的路徑

e、測試,

 訪問http://localhost:5020/encrypt,加密zhangsan

  wKioL1mmUoqCkS4lAADgZkLH2To261.jpg

 訪問http://localhost:5020/decrypt,解密

  wKiom1mmUxjjK4PGAADeSDSLImI703.jpg

2、Spring Cloud Config與Eureka配合使用

將Config Server和Config Client都註冊到Eureka Server上。

一、Config Server的application.yml配置以下:

server:
  port: 5028
#端點的配置
endpoints:
  sensitive: true
  shutdown:
    enabled: true
management:
  security:
    enabled: false
spring:
  application:
    name: config-server-eureka
  cloud:
    config:
      server:
        git:
          uri: https://git.oschina.net/wadjz/spring-cloud-config.git
          username: 
          password: 
          clone-on-start: true
        health:
          repositories:
            a-foo: 
              label: v2.0
              profiles: dev
              name: spring-cloud-demo
eureka:
  client:
    service-url:
      defaultZone: http://liuy2:5010/eureka/
  instance:
    prefer-ip-address: true

二、Config Client的bootstrap.yml配置以下,端口5021:

spring:
  application:
    # 對應config Server所獲取的配置文件的{application}
    name: spring-cloud-demo 
  cloud:
    config:
      profile: dev
      label: master
      discovery:
        # 表示使用微服務發現組件中的Config Server,而不是本身指定Config Server的URI,默認false
        enabled: true
        # 指定Config Server在服務發現中的serviceId,默認是configserver
        service-id: config-server-eureka
eureka:
  client:
    service-url:
      defaultZone: http://liuy2:5010/eureka/

三、測試訪問http://localhost:5021/profile,效果:

 wKioL1mn38_T8nWEAAAOnqrm3U4137.png

相關文章
相關標籤/搜索