一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Spring Cloud Gateway使用Token驗證詳解

Spring Cloud Gateway使用Token驗證詳解

2021-07-17 12:30鄉間一壺涼白開 Java教程

這篇文章主要介紹了Spring Cloud Gateway使用Token驗證詳解,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

引入依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<dependencymanagement>
  <dependencies>
    <dependency>
      <groupid>org.springframework.cloud</groupid>
      <artifactid>spring-cloud-dependencies</artifactid>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencymanagement>
 
<dependencies>
  <dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-gateway</artifactid>
  </dependency>
</dependencies>

自定義過濾器

可以繼承 abstractgatewayfilterfactory 或實現 globalfilter 實現過濾請求功能

gatewayfilter

gatewayfilter 只能指定路徑上應用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import org.springframework.cloud.gateway.filter.gatewayfilter;
import org.springframework.cloud.gateway.filter.factory.abstractgatewayfilterfactory;
import org.springframework.http.httpstatus;
import org.springframework.http.server.reactive.serverhttpresponse;
import org.springframework.stereotype.component;
 
@component
public class authgatewayfilterfactory extends abstractgatewayfilterfactory<authgatewayfilterfactory.config> {
 
  public authgatewayfilterfactory() {
    super(config.class);
  }
 
  @override
  public gatewayfilter apply(config config) {
    return (exchange, chain) -> {
      system.out.println("welcome to authfilter.");
      string token = exchange.getrequest().getheaders().getfirst("sign");
      if (config.secret.equals(token)) {
        return chain.filter(exchange);
      }
      serverhttpresponse response = exchange.getresponse();
      response.setstatuscode(httpstatus.unauthorized);
      return response.setcomplete();
    };
  }
 
  static class config {
    static string secret = "1234";
  }
}
?
1
2
3
4
5
6
7
8
9
10
11
spring:
 cloud:
  gateway:
   routes:
   - id: service2_route
    uri: http://127.0.0.1:8082
    predicates:
    - path=/s2/**
    filters:
    - stripprefix=1 # 去掉路徑的 n 個前綴
    - auth=true # 輸入過濾器類的名稱前綴

globalfilter

globalfilter 可以在全局應用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import org.springframework.cloud.gateway.filter.gatewayfilterchain;
import org.springframework.cloud.gateway.filter.globalfilter;
import org.springframework.core.ordered;
import org.springframework.http.httpstatus;
import org.springframework.http.server.reactive.serverhttprequest;
import org.springframework.http.server.reactive.serverhttpresponse;
import org.springframework.stereotype.component;
import org.springframework.web.server.serverwebexchange;
import reactor.core.publisher.mono;
 
@component
public class authglobalfilter implements globalfilter, ordered {
  @override
  public mono<void> filter(serverwebexchange exchange, gatewayfilterchain chain) {
    system.out.println("welcome to authglobalfilter.");
    serverhttprequest request = exchange.getrequest();
    string sign = request.getheaders().get("sign").get(0);
    string token = "1234";
    if(token.equals(sign)) {
      return chain.filter(exchange);
    }
    serverhttpresponse response = exchange.getresponse();
    response.setstatuscode(httpstatus.unauthorized);
    return response.setcomplete();
  }
 
  @override
  public int getorder() {
    return 0;
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://segmentfault.com/a/1190000018310098

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91久久国产视频 | 色姑娘导航 | 女医学护士一级毛片 | 亚洲国产99在线精品一区69堂 | 白丝尤物的下面被疯狂蹂躏 | 1024国产基地永久免费 | 天天插在线视频 | 三级全黄的视频 | 国产综合视频 | 国产特黄一级一片免费 | 国产偷窥女洗浴在线观看亚洲 | avtt在线播放 | 男人的j进入女人的j免费 | 91婷婷射 | 欧美胖逼 | 精品久久久久久国产 | 国产亚洲精品一区在线播 | 日韩一级片在线免费观看 | 精品网站 | 青青青青青国产免费手机看视频 | 草莓香蕉绿巨人丝瓜榴莲污在线观看 | 欧美一区二区三区精品 | leslessexvideos日本 | 国产成人99精品免费观看 | 亚洲卡一卡2卡三卡4卡无卡三 | 午夜日本大胆裸艺术 | 4hu影院永久在线播放 | 免费看3d小舞被躁视频网站 | yellow视频在线观看免费 | 亚洲 欧美 国产 综合 播放 | 国内9lporm自拍视频区 | 美女1819xxxx | 精品视频 久久久 | 日本免费一区二区三区 | 黑人巨茎大战欧美白妇 | 九九99靖品| 日本春菜花在线中文字幕 | 国内老司机精品视频在线播出 | 香港日本三级亚洲三级 | 亚洲熟区 | 欧美高清无砖专区欧美精品 |