瀏覽代碼

全局过滤器

gaodm 6 年之前
父節點
當前提交
afca8030ea
共有 1 個文件被更改,包括 27 次插入0 次删除
  1. 27 0
      gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

+ 27 - 0
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -0,0 +1,27 @@
+package com.diagbot.filter;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.cloud.gateway.filter.GatewayFilterChain;
+import org.springframework.cloud.gateway.filter.GlobalFilter;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.http.server.reactive.ServerHttpRequest;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+/**
+ * @Description: 全局过滤器
+ * @author: gaodm
+ * @time: 2018/9/7 13:55
+ */
+@Configuration
+@Slf4j
+public class GlobalGatewayFilter implements GlobalFilter {
+    @Override
+    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
+        log.info("check token and url permission....");
+        ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
+        builder.header("Authorization","Authorization Bearer token");
+        chain.filter(exchange.mutate().request(builder.build()).build());
+        return chain.filter(exchange.mutate().request(builder.build()).build());
+    }
+}