Spring Cloud 如何保證微服務(wù)內(nèi)安全
在微服務(wù)的架構(gòu)下,我們需要把系統(tǒng)的業(yè)務(wù)劃分成多個(gè)單一的微服務(wù)。每個(gè)微服務(wù)都會(huì)提供接口供其他微服務(wù)調(diào)用,在Dubbo中可以通過(guò)rmi、nio等實(shí)現(xiàn),Spring Cloud中是通過(guò)http調(diào)用的。
但有些時(shí)候,我們只希望用戶(hù)通過(guò)我們的網(wǎng)關(guān)調(diào)用微服務(wù),不允許用戶(hù)直接請(qǐng)求微服務(wù)。這時(shí)我們就可以借助Spring Security來(lái)保障安全。
二、使用步驟2.1 在提供接口的微服務(wù)項(xiàng)目中配置Spring Security1 首先在pom.xml引入Spring Security的相關(guān)配置,如下
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId></dependency>
2 在qpplication.yml中配置賬號(hào)密碼,如下
security: basic: enabled: true user: name: sunbufu password: 123456
3 此時(shí)訪(fǎng)問(wèn)接口發(fā)現(xiàn)已經(jīng)需要認(rèn)證了。
輸入正確的賬號(hào)和密碼后就可以訪(fǎng)問(wèn)了。
2.2在調(diào)用微服務(wù)項(xiàng)目中配置Feign的賬號(hào)密碼1 在application.yml中配置賬號(hào)密碼
security: user: name: sunbufu password: 123456
2 添加Feign的配置文件
package com.sunbufu.config;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import feign.auth.BasicAuthRequestInterceptor;@Configurationpublic class FeignConfiguration { @Value('${security.user.name}') private String userName; @Value('${security.user.password}') private String passWord; @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){return new BasicAuthRequestInterceptor(userName, passWord); }}
3 這樣完成后,就可以正常的訪(fǎng)問(wèn)了。
git源碼地址:https://github.com/sunbufu/sunbufu-cloud
下面是這4個(gè)工程的說(shuō)明:1. sunbufu-erueka:Eureka服務(wù)的工程
2. sunbufu-hello-face:服務(wù)接口的定義工程,其中包括定義微服務(wù)需要實(shí)現(xiàn)什么功能,其他微服務(wù)怎么調(diào)用,以及feign的配置
3. sunbufu-hello-impl:服務(wù)接口的實(shí)現(xiàn)工程,實(shí)現(xiàn)了sunbufu-hello-face定義的功能
4. sunbufu-hello-web:服務(wù)的網(wǎng)關(guān)工程,主要為了調(diào)用sunbufu-hello-face
Spring Cloud服務(wù)安全連接Spring Cloud可以增加HTTP Basic認(rèn)證來(lái)增加服務(wù)連接的安全性。
1、加入security啟動(dòng)器在maven配置文件中加入Spring Boot的security啟動(dòng)器。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId></dependency>
這樣,就開(kāi)啟對(duì)服務(wù)連接的安全保護(hù),系統(tǒng)默認(rèn)為生成一個(gè)用戶(hù)名為”user”及一個(gè)隨機(jī)密碼,隨機(jī)密碼在服務(wù)啟動(dòng)的時(shí)候在日志中會(huì)打印出來(lái)。
2、自定義用戶(hù)名密碼隨機(jī)密碼沒(méi)什么實(shí)際意義,我們需要一個(gè)固定的連接用戶(hù)名和密碼。
在應(yīng)用配置文件中加入以下配置即可。
security: user: name: admin password: admin123456
這樣配置完后在連接這個(gè)服務(wù)的時(shí)候就會(huì)要求輸入用戶(hù)名和密碼,如果認(rèn)證失敗會(huì)返回401錯(cuò)誤。
{ 'timestamp': 1502689874556, 'status': 401, 'error': 'Unauthorized', 'message': 'Bad credentials', 'path': '/test/save'}3、安全連接
1、注冊(cè)中心安全連接
username:password@ipaddress
2、Feign申明式服務(wù)安全連接
@FeignClient(name = 'SERVICE', configuration = FeignAuthConfig.class)public interface OrderService extends OrderAPI {}
@Configurationpublic class FeignAuthConfig { @Bean public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {return new BasicAuthRequestInterceptor('admin','admin123456'); }}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. 如何在jsp界面中插入圖片3. 詳解盒子端CSS動(dòng)畫(huà)性能提升4. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)5. asp批量添加修改刪除操作示例代碼6. .NET6打包部署到Windows Service的全過(guò)程7. Ajax返回值類(lèi)型與用法實(shí)例分析8. Vue element ui用戶(hù)展示頁(yè)面的實(shí)例9. css代碼優(yōu)化的12個(gè)技巧10. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼
