SpringCloud確保服務(wù)只能通過gateway轉(zhuǎn)發(fā)訪問禁止直接調(diào)用接口訪問_第1頁
SpringCloud確保服務(wù)只能通過gateway轉(zhuǎn)發(fā)訪問禁止直接調(diào)用接口訪問_第2頁
SpringCloud確保服務(wù)只能通過gateway轉(zhuǎn)發(fā)訪問禁止直接調(diào)用接口訪問_第3頁
SpringCloud確保服務(wù)只能通過gateway轉(zhuǎn)發(fā)訪問禁止直接調(diào)用接口訪問_第4頁
SpringCloud確保服務(wù)只能通過gateway轉(zhuǎn)發(fā)訪問禁止直接調(diào)用接口訪問_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

SpringCloud確保服務(wù)只能通過gateway轉(zhuǎn)發(fā)訪問,禁止直接調(diào)用接口訪問前言在微服務(wù)體系架構(gòu)中,網(wǎng)關(guān)承擔(dān)著重要的角色,在網(wǎng)關(guān)中可以添加各種過濾器,過濾請求,保證請求參數(shù)安全,限流等等。如果請求繞過了網(wǎng)關(guān),那就等于繞過了重重關(guān)卡,直搗黃龍,所以,在分布式架構(gòu)中,我們需要有一定的防范,來確保各個服務(wù)相互之間安全調(diào)用。正文思路1、在網(wǎng)關(guān)中給所有請求加上一個請求密鑰2、在服務(wù)添加過濾器,驗證密鑰首先在網(wǎng)關(guān)服務(wù)(gateway)中添加過濾器,給放行的請求頭上加上判斷package

com.hpsyche.hpsychegatewayserver.filter;

import

com.hpsyche.hpsychegatewayserver.utils.RedisUtil;

import

org.springframework.beans.factory.annotation.Autowired;

import

org.springframework.cloud.gateway.filter.GatewayFilterChain;

import

org.springframework.cloud.gateway.filter.GlobalFilter;

import

org.springframework.http.HttpStatus;

import

org.springframework.http.server.reactive.ServerHttpRequest;

import

org.springframework.util.StringUtils;

import

org.springframework.web.server.ServerWebExchange;

import

reactor.core.publisher.Mono;

/**

*

@author

Hpsyche

*/

public

class

TokenFilter

implements

GlobalFilter

{

@Autowired

private

RedisUtil

redisUtil;

@Override

public

Mono

filter(ServerWebExchange

exchange,

GatewayFilterChain

chain)

{

//token(用戶身份)判斷

................

ServerHttpRequest

req

=

exchange.getRequest().mutate()

.header("from",

"gateway").build();

return

chain.filter(exchange.mutate().request(req.mutate().build()).build());

}

}如果考慮到安全性的話,這里header的值也可以使用UUID,并保存至redis中,在其他服務(wù)中通過redis讀取判斷身份。搜索后端架構(gòu)師公眾號回復(fù)“架構(gòu)整潔”,送你一份驚喜禮包。在主服務(wù),如auth授權(quán)服務(wù)上,添加全局攔截器,判斷header上是否有對應(yīng)的value,是則否則,反之攔截。package

erceptor;

import

lombok.extern.slf4j.Slf4j;

import

mons.lang.StringUtils;

import

org.springframework.web.servlet.HandlerInterceptor;

import

javax.servlet.http.HttpServletRequest;

import

javax.servlet.http.HttpServletResponse;

import

java.io.PrintWriter;

/**

*

@author

Hpsyche

*/

@Slf4j

public

class

GlobalInterceptor

implements

HandlerInterceptor

{

@Override

public

boolean

preHandle(HttpServletRequest

request,

HttpServletResponse

response,

Object

obj)

throws

Exception

{

String

secretKey

=

request.getHeader("from");

if(!StringUtils.isNotBlank(secretKey)||secretKey.equals("gateway"){

response.setContentType("application/json;

charset=utf-8");

PrintWriter

writer

=

response.getWriter();

writer.write("error");

return

false;

}

return

true;

}

}此時,繞過網(wǎng)關(guān),直接訪問auth服務(wù)的接口會返回error,即必須通過網(wǎng)關(guān),我們的任務(wù)看似完成了。問題在項目中,發(fā)現(xiàn)了feign遠程調(diào)用會失敗,因為在fegin中調(diào)用不通過gateway,缺少from的請求頭,會被攔截器攔截,導(dǎo)致調(diào)用失敗。解決方案在auth-client(服務(wù)暴露方)添加請求頭,在供其他服務(wù)調(diào)用時添加header。package

com.hpsyche.hpsycheauthclient.config;

import

feign.RequestInterceptor;

import

feign.RequestTemplate;

import

org.springframework.context.annotation.Configuration;

/**

*

Feign調(diào)用的時候添加請求頭from

*/

@Configuration

public

class

FeignConfiguration

implements

RequestInterceptor

{

@Override

public

void

apply(RequestTemplate

requestTemplate)

{;

requestTemplate.header("from",

"gateway");

}

}同時,需要在service接口中添加configuration配置:package

com.hpsyche.hpsycheauthclient.api;

import

com.hpsyche.hpsychecommonscommon.vo.BaseResponse;

import

com.hpsyche.hpsycheauthclient.config.FeignConfiguration;

import

org.springframework.cloud.openfeign.FeignClient;

import

org.springframework.web.bind.annotation.PostMapping;

import

org.springframework.web.bind.annotation.RequestParam;

/**

*

@author

Hpsyche

*/

@FeignClient(value

=

"hpsyche-auth-server",configuration

=

FeignConfiguration.class)

public

interface

AuthService

{

@PostMapping("/auth/verifyUser")

BaseResponse

verifyUser(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論