自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Cloud 提供的這種網(wǎng)關(guān)Gateway實(shí)現(xiàn)方式,你知道嗎?

開(kāi)發(fā) 前端
對(duì)于“敏感”標(biāo)頭(默認(rèn)情況下為cookie和authorization)和“代理”(x-forward-*)頭,提供了非常好的支持,這些頭不會(huì)向下游傳遞。

環(huán)境:SpringBoot2.5.13

Spring Cloud Gateway提供了一個(gè)名為ProxyExchange的實(shí)用程序?qū)ο?。你可以在常?guī)Spring web處理程序中使用它作為方法參數(shù)。它通過(guò)鏡像HTTP動(dòng)詞的方法支持基本的下游HTTP交換。在MVC中,它還支持通過(guò)forward()方法轉(zhuǎn)發(fā)到本地處理程序。要使用ProxyExchange,需要在classpath中包含正確的模塊(spring-cloud-gateway-mvc(3.1.5)或spring-cloud-gateway-webflux)。

下面的MVC示例將請(qǐng)求代理到/test下游到遠(yuǎn)程服務(wù)器:

@RestController
@SpringBootApplication
public class GatewaySampleApplication {


@Value("${remote.home}")
private URI home;


@GetMapping("/test")
public ResponseEntity<?> proxy(ProxyExchange<byte[]> proxy) throws Exception {
return proxy.uri(home.toString() + "/image/png").get();
}


}

下面的例子對(duì)Webflux做了相同的事情:

@RestController
@SpringBootApplication
public class GatewaySampleApplication {


@Value("${remote.home}")
private URI home;


@GetMapping("/test")
public Mono<ResponseEntity<?>> proxy(ProxyExchange<byte[]> proxy) throws Exception {
return proxy.uri(home.toString() + "/image/png").get();
}


}

ProxyExchange上的便利方法使處理程序方法能夠發(fā)現(xiàn)并增強(qiáng)傳入請(qǐng)求的URI路徑。例如,你可能想提取路徑末尾的元素并將其傳遞到下游:

@GetMapping("/proxy/path/**")
public ResponseEntity<?> proxyPath(ProxyExchange<byte[]> proxy) throws Exception {
// 如這里請(qǐng)求的/proxy/path/666,那么這里path = 666
String path = proxy.path("/proxy/path/");
return proxy.uri(home.toString() + "/foos/" + path).get();
}

Spring MVC和Webflux的所有特性都可以用于網(wǎng)關(guān)處理程序方法。因此,可以注入請(qǐng)求頭和查詢參數(shù),例如,可以使用映射注釋中的聲明來(lái)約束傳入的請(qǐng)求。如下:

目標(biāo)服務(wù)接口

@RestController
@RequestMapping("/business")
public class BusinessController {


@PostMapping("/index")
public Object index(@RequestBody Map<String ,Object> body) {
System.out.println("業(yè)務(wù)接口接收到的內(nèi)容:" + body) ;
Map<String, Object> result = new HashMap<>() ;
result.put("code", 0) ;
result.put("data", "業(yè)務(wù)處理成功 - " + LocalDateTime.now().getNano()) ;
result.put("message", "success") ;
return result ;
}

}

網(wǎng)關(guān)服務(wù)接口

@RestController
@RequestMapping("/proxy/api")
public class GatewayController {


@GetMapping("")
public Object order(@RequestHeader("token") String token,
Integer id, ProxyExchange<Map<String, Object>> exchange) {
System.out.println("token = " + token + ", id = " + id) ;
Map<String, Object> body = new HashMap<>() ;
body.put("id", id) ;
body.put("token", token) ;
return exchange.uri("http://localhost:9000/business/index").body(body).post() ;
}

}

調(diào)用結(jié)果

圖片

Postman請(qǐng)求

圖片

控制臺(tái)輸出

你還可以使用ProxyExchange的header()方法向下游響應(yīng)添加header。

exchange.uri("http://localhost:9000/business/index").header("key", "123123").body(body).post() ;

你還可以通過(guò)在get()方法(以及其他方法)中添加一個(gè)mapper來(lái)操作響應(yīng)頭(以及響應(yīng)中的其他任何內(nèi)容)。mapper是一個(gè)Function,接收傳入的ResponseEntity并將其轉(zhuǎn)換為傳出的ResponseEntity,如下:

exchange.uri("http://localhost:9000/business/index").header("key", "123123").body(body).post(result -> {
System.out.println("Resposne Header: " + result.getHeaders()) ;
return ResponseEntity.ok("success") ;
}) ;

對(duì)于“敏感”標(biāo)頭(默認(rèn)情況下為cookie和authorization)和“代理”(x-forward-*)頭,提供了非常好的支持,這些頭不會(huì)向下游傳遞。如:

當(dāng)我們的請(qǐng)求中有Authorization 請(qǐng)求Header信息時(shí),默認(rèn)將不會(huì)向下游傳遞,這是默認(rèn)行為還有cookie。我們可以通過(guò)修改配置文件覆蓋

spring:
cloud:
gateway:
proxy:
sensitive:
- ''

完畢!??!

責(zé)任編輯:武曉燕 來(lái)源: 實(shí)戰(zhàn)案例錦集
相關(guān)推薦

2023-09-15 08:18:49

cookie網(wǎng)關(guān)代理

2023-02-15 08:12:19

http超時(shí)過(guò)濾器

2022-07-05 08:05:00

策略模式接口實(shí)現(xiàn)類

2024-07-30 08:22:47

API前端網(wǎng)關(guān)

2023-04-23 09:50:50

@BeanSpring

2023-04-28 12:37:59

Spring@Bean使用方式

2023-08-15 08:01:12

2024-02-05 12:08:07

線程方式管理

2023-12-12 08:41:01

2024-11-26 00:45:29

free區(qū)域字段

2025-03-11 00:35:00

Spring事件機(jī)制

2019-02-12 11:15:15

Spring設(shè)計(jì)模式Java

2024-11-26 14:29:48

2024-06-12 08:05:06

2023-02-27 07:56:55

IngressKubernetes

2021-11-04 10:11:02

Sentinel網(wǎng)關(guān)限流

2021-07-29 06:55:03

Spring@AutowriedbyType注入

2022-03-10 08:25:27

JavaScrip變量作用域

2019-12-12 09:23:29

Hello World操作系統(tǒng)函數(shù)庫(kù)

2021-10-14 06:52:47

算法校驗(yàn)碼結(jié)構(gòu)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)