ITeye博客

spring cloud gateway 全局过滤器。

xiaofancn · 2019-03-17 23:11 · J2EE · 开源软件 · Markdown

spring cloud gateway 网关yaml配置。

 

 

 

server:
  port: 10010
spring:
  application:
    name: gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true   #从注册中心寻找
      routes:
        - id: hystrix_route
          uri: lb://web-service   #web微服务
          predicates:
            - Path=/api/web/**    #访问微服务的路径
          filters:
            - StripPrefix=2       #去除两个前缀目录

eureka:
  client:
    service-url:
      defaultZone: http://root123:root123@127.0.0.1:8761/eureka
    registry-fetch-interval-seconds: 5
  instance:
    instance-id: ${spring.application.name}:${server.port}
    prefer-ip-address: true  #当你获取host时,返回的不是主机名,而是ip
    ip-address: 127.0.0.1




 

 

 

 

过滤器配置。

 

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.fansxnet.common.R;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.NettyWriteResponseFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 
 */
@Slf4j
@Configuration
public class GlobalFiltersConfig {

    @Component
    public class AuthFilter implements GlobalFilter, Ordered {
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            ServerHttpRequest request = exchange.getRequest();
            String token = request.getHeaders().getFirst("token");
            if (StringUtils.isEmpty(token)) {
                //验证失败,提示登录
                exchange.getResponse().getHeaders().add("Content-Type", "application/json;charset=UTF-8");
                return exchange.getResponse().writeAndFlushWith(Mono.just(Mono.just(exchange.getResponse().bufferFactory().wrap(JSON.toJSONBytes(R.just(-2, "请登录"))))));
            }
            log.info("header: \n{}\n", JSON.toJSONString(request.getHeaders(), SerializerFeature.PrettyFormat));
            //继续
            return chain.filter(exchange).
                    then(Mono.fromRunnable(() ->
                    {
                        log.info("third post filter");
                    }));
        }

        /**
         NettyWriteResponseFilter过滤器前面,
         */
        @Override
        public int getOrder() {
            return NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER-1;
        }
    }
}

 

 

 

 

https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.0.3.RELEASE/single/spring-cloud-gateway.html

https://github.com/xiaofancn/spring-cloud-learn

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。