溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring Cloud Gateway使用Token驗(yàn)證詳解

發(fā)布時(shí)間:2020-08-20 01:19:05 來源:腳本之家 閱讀:166 作者:鄉(xiāng)間一壺涼白開 欄目:編程語言

引入依賴

<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 或?qū)崿F(xiàn) GlobalFilter 實(shí)現(xiàn)過濾請求功能

GatewayFilter

GatewayFilter 只能指定路徑上應(yīng)用

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";
  }
}
spring:
 cloud:
  gateway:
   routes:
   - id: service2_route
    uri: http://127.0.0.1:8082
    predicates:
    - Path=/s2/**
    filters:
    - StripPrefix=1 # 去掉路徑的 n 個(gè)前綴
    - Auth=true # 輸入過濾器類的名稱前綴

GlobalFilter

GlobalFilter 可以在全局應(yīng)用

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;
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI