成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Spring Cloud Gateway提供的簡易網關實現方式,你使用過嗎?

開發 前端
對于“敏感”標頭(默認情況下為cookie和authorization)和“代理”(x-forward-*)頭,提供了非常好的支持,這些頭不會向下游傳遞。如:當我們的請求中有Authorization 請求Header信息時,默認將不會向下游傳遞,這是默認行為還有cookie。我們可以通過修改配置文件覆蓋。

環境:SpringBoot2.5.13

Spring Cloud Gateway提供了一個名為ProxyExchange的實用程序對象。你可以在常規Spring web處理程序中使用它作為方法參數。它通過鏡像HTTP動詞的方法支持基本的下游HTTP交換。在MVC中,它還支持通過forward()方法轉發到本地處理程序。要使用ProxyExchange,需要在classpath中包含正確的模塊(spring-cloud-gateway-mvc(3.1.5)spring-cloud-gateway-webflux)。

下面的MVC示例將請求代理到/test下游到遠程服務器:

@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();
  }


}

下面的例子對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上的便利方法使處理程序方法能夠發現并增強傳入請求的URI路徑。例如,你可能想提取路徑末尾的元素并將其傳遞到下游:

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

目標服務接口

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


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

網關服務接口

@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() ;
  }
  
}

調用結果

圖片圖片

Postman請求

控制臺輸出控制臺輸出

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

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

你還可以通過在get()方法(以及其他方法)中添加一個mapper來操作響應頭(以及響應中的其他任何內容)。mapper是一個Function,接收傳入的ResponseEntity并將其轉換為傳出的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") ;
}) ;

對于“敏感”標頭(默認情況下為cookieauthorization)和“代理”(x-forward-*)頭,提供了非常好的支持,這些頭不會向下游傳遞。如:

當我們的請求中有Authorization 請求Header信息時,默認將不會向下游傳遞,這是默認行為還有cookie。我們可以通過修改配置文件覆蓋。

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

完畢!!!

責任編輯:武曉燕 來源: Spring全家桶實戰案例源碼
相關推薦

2023-02-13 08:10:40

Gateway網關Spring

2023-11-09 08:31:56

Spring微服務網關

2021-11-04 10:11:02

Sentinel網關限流

2023-02-20 10:13:00

灰度發布實現

2023-02-15 08:12:19

http超時過濾器

2019-08-22 09:55:17

RedisAPI數據

2024-01-29 08:00:00

架構微服務開發

2024-05-13 18:35:06

負載均衡主機端口

2018-12-19 15:05:55

Spring Clou網關 Gateway

2023-04-14 09:01:25

2023-09-12 07:19:06

微服務網關架構

2024-12-27 08:43:17

2024-01-09 08:20:05

2023-02-28 08:57:06

Spring上下線緩存

2022-03-28 20:57:31

私有屬性class屬性和方法

2023-09-13 09:20:00

日志配置Spring

2023-04-28 12:37:59

Spring@Bean使用方式

2023-04-23 09:50:50

@BeanSpring

2024-04-03 08:08:15

謂詞網關開發

2023-03-08 09:03:55

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99亚洲精品 | 午夜视频免费在线观看 | 国产精品1区2区3区 男女啪啪高潮无遮挡免费动态 | 污免费网站| 成人网av | 亚洲国产免费 | 国内毛片毛片毛片毛片 | 亚洲最新在线视频 | 亚洲一区二区在线视频 | 精品久久久久久红码专区 | 亚洲精品一二三区 | 性高湖久久久久久久久3小时 | 精品乱码一区二区三四区 | 成人在线黄色 | 日产精品久久久一区二区福利 | 黄色毛片免费视频 | 欧美在线免费 | 一区二区久久 | av在线电影网 | 国产高清精品一区二区三区 | 成人av看片 | 中文字幕在线国产 | 美日韩免费视频 | 天堂av资源 | 亚洲精品久久久久久久久久久久久 | 国产精彩视频在线观看 | 欧美a级成人淫片免费看 | 成人自拍视频网站 | 久久久久久a | 99久久婷婷| 国产日韩精品在线 | 美女在线视频一区二区三区 | 狠狠草视频 | 日韩免费一二三区 | 欧美成人一区二免费视频软件 | 一级做a爰片性色毛片16 | 日韩在线不卡 | 在线看片国产精品 | 国产精品免费小视频 | 91国内精精品久久久久久婷婷 | 久久久www成人免费精品 |