当前位置:   article > 正文

SpringCloudGateway 实现微服务名称隐藏, url保护, token鉴权_springcloud gateway 网关 隐藏服务名

springcloud gateway 网关 隐藏服务名

微服务中我们最好是要讲微服务名字隐藏起来, 这个demo中我使用了url转发实现,
yml 和 过滤器代码如下.

server:
  port: 8888
spring:
  application:
    name: pns-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    #        register-enabled: true
    gateway:
      discovery:
        locator:
          enabled: true
      httpclient:
        connect-timeout: 3000 #连接超时时间 ,动态
        response-timeout: 10s #请求读取超时时间,动态
        pool:
          max-connections: 1500

      routes:
        - id: getToken
          uri: lb://pns-server-service
          #          filters:
          #            - Authentication=a,b
          predicates:
            - Path=/rest/oauth/direct
        - id: auth
          uri: lb://microServiceName
          filters:
            - Authentication=a,b  # 参数必须传
          predicates:
            - Before=2030-01-20T17:42:47.789-07:00   # 谓词 也就是if条件 这里是指2030前的请求都可以

# 健康检查
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
  • 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
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

我这里为了方便演示, 将AbstractNameValueGatewayFilterFactory 和 GatewayFilter 二合一了

@Component //一定要让Spring管理这个bean
public class AuthenticationGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory implements GatewayFilter, Ordered {
    Logger logger = LoggerFactory.getLogger(AuthenticationGatewayFilterFactory.class);

    /**
     * 权限校验  token, channel通道权限
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        logger.info("进入权限校验过滤器");
        if (!PnsAuthenticator.checkAuth(exchange.getRequest())) {
            ServerHttpResponse response = exchange.getResponse();
            response.setStatusCode(HttpStatus.NOT_ACCEPTABLE); //这个状态码是406
            ResponseDecorator decorator = new ResponseDecorator(exchange.getResponse());
            //exchange.getResponse().response(decorator).build();
            return exchange.mutate().response(decorator).build().getResponse().setComplete();
        }
        //放行
        return chain.filter(exchange);
    }

    /**
     * 这是Ordered接口的中的方法
     * 过滤器有一个优先级的问题,这个值越小,优先级越高
     *
     * @return 优先级
     */
    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return new AuthenticationGatewayFilterFactory();
    }
}
  • 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
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

最后一个小建议, 如果是高并发的请求, 调用链路能短则短, 能本地就不微服务调用, 能rpc就不用http, 能redis就不用数据库, 能二级缓存就不用redis.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/301324
推荐阅读
相关标签
  

闽ICP备14008679号