【问题标题】:spring cloud gateway, Is Request size limit filter is availablespring cloud gateway, Is Request size limit filter 可用
【发布时间】:2018-11-20 23:30:17
【问题描述】:

我正在与spring-cloud-gateway 合作一个项目。我看到请求大小限制过滤器尚不可用。但我需要开发它。任何想法,它来了吗?还是我应该开始自己的开发。

我知道很难得到任何答案,因为除了开发人员之外,还有一些人正在研究它。

【问题讨论】:

  • 没有,欢迎拉取请求。

标签: spring-cloud netflix-zuul spring-cloud-netflix spring-cloud-gateway


【解决方案1】:

我创建了一个名为RequestSizeGatewayFilterFactory 的过滤器,到目前为止,它在我们的应用程序中运行良好。但不确定这是否可以成为spring-cloud-gateway 项目的一部分。

package com.api.gateway.somename.filter;

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.ServerHttpRequest;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * This filter blocks the request, if the request size is more than
 * the permissible size.The default request size is 5 MB
 *
 * @author Arpan
 */
public class RequestSizeGatewayFilterFactory
        extends AbstractGatewayFilterFactory<RequestSizeGatewayFilterFactory.RequestSizeConfig> {

    private static String PREFIX = "kMGTPE";
    private static String ERROR = "Request size is larger than permissible limit." +
            "Request size is %s where permissible limit is %s";

    public RequestSizeGatewayFilterFactory() {
        super(RequestSizeGatewayFilterFactory.RequestSizeConfig.class);
    }

    @Override
    public GatewayFilter apply(RequestSizeGatewayFilterFactory.RequestSizeConfig requestSizeConfig) {
        requestSizeConfig.validate();
        return (exchange, chain) -> {
            ServerHttpRequest request = exchange.getRequest();
            String contentLength = request.getHeaders().getFirst("content-length");
            if (!StringUtils.isEmpty(contentLength)) {
                Long currentRequestSize = Long.valueOf(contentLength);
                if (currentRequestSize > requestSizeConfig.getMaxSize()) {
                    exchange.getResponse().setStatusCode(HttpStatus.PAYLOAD_TOO_LARGE);
                    exchange.getResponse().getHeaders().add("errorMessage",
                            getErrorMessage(currentRequestSize, requestSizeConfig.getMaxSize()));
                    return exchange.getResponse().setComplete();
                }
            }
            return chain.filter(exchange);
        };
    }

    public static class RequestSizeConfig {

        // 5 MB is the default request size
        private Long maxSize = 5000000L;

        public RequestSizeGatewayFilterFactory.RequestSizeConfig setMaxSize(Long maxSize) {
            this.maxSize = maxSize;
            return this;
        }

        public Long getMaxSize() {
            return maxSize;
        }

        public void validate() {
            Assert.isTrue(this.maxSize != null && this.maxSize > 0,
                    "maxSize must be greater than 0");
            Assert.isInstanceOf(Long.class, maxSize, "maxSize must be a number");
        }
    }

    private static String getErrorMessage(Long currentRequestSize, Long maxSize) {
        return String.format(ERROR,
                getHumanReadableByteCount(currentRequestSize),
                getHumanReadableByteCount(maxSize));
    }

    private static String getHumanReadableByteCount(long bytes) {
        int unit = 1000;
        if (bytes < unit) return bytes + " B";
        int exp = (int) (Math.log(bytes) / Math.log(unit));
        String pre = Character.toString(PREFIX.charAt(exp - 1));
        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
    }
}

过滤器的配置是: 当它用作默认过滤器时:

spring:
  application:
    name: somename
  cloud:
    gateway:
      default-filters:
      - Hystrix=default
      - RequestSize=7000000

何时需要在某些 API 中应用

# ===========================================
  - id: request_size_route
    uri: ${test.uri}/upload
    predicates:
    - Path=/upload
    filters:
    - name: RequestSize
      args:
        maxSize: 5000000

您还需要在项目中的某个组件可扫描类中配置 bean,即 GatewayAutoConfiguration 用于 spring-cloud-gateway-core 项目。

@Bean
public RequestSizeGatewayFilterFactory requestSizeGatewayFilterFactory() {
   return new RequestSizeGatewayFilterFactory();
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-07-23
  • 2019-03-02
  • 2021-10-20
  • 2022-12-11
  • 2022-11-13
  • 2020-03-18
  • 2019-06-24
  • 2020-06-22
相关资源
最近更新 更多