文章共 503字,閱讀大約須要 2分鐘 !spring
Eureka Server 在實際使用過程當中必須考慮安全問題,好比 未認證的用戶 不容許其隨意調用 Eureka Server的 API;還有一個則是 未認證的 Eureka Client 也禁止其註冊到 Eureka Server中來,這些都是能夠在工程中進行配置的,固然這也是最最基本的安全認證措施,本文實踐之。安全
本文實驗環境以下:bash
注: 本文首發於 My Personal Blog:CodeSheep·程序羊,歡迎光臨 小站app
Eureka Server 開啓 Spring Security Basic認證首先須要在 Eureka Server中引入 Spring Security組件:spring-boot
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
複製代碼
接下來關鍵的一步則是配置 Eureka Server工程的 yml配置文件,加入和認證相關的信息:學習
server:
port: 1111
spring:
security:
user:
name: codesheep
password: www.codesheep.cn
eureka:
client:
registerWithEureka: false
fetchRegistry: false
複製代碼
spring.security
配置的意圖應該很明確了吧,須要用戶名和密碼方可認證經過。fetch
既然上面的 Eureka Server已開啓認證環節,則相應的 Eureka Client也須要對應的配置,方可經過認證再註冊到 Eureka Server中來spa
搭建好 Eureka Client工程後,須要在項目配置文件中加入相似 Eureka Server的配置:code
server:
port: 1112
spring:
application:
name: eureka-client
eureka:
client:
security:
basic:
user: codesheep
password: www.codesheep.cn
serviceUrl:
defaultZone: http://${eureka.client.security.basic.user}:${eureka.client.security.basic.password}@localhost:1111/eureka/
複製代碼
這樣就完成了基於 Spring Security Basic的基礎認證cdn
因爲能力有限,如有錯誤或者不當之處,還請你們批評指正,一塊兒學習交流!
可 長按 或 掃描 下面的 當心心 來訂閱做者公衆號 CodeSheep,獲取更多 務實、能看懂、可復現的 原創文 ↓↓↓