聊聊feign的RequestInterceptor

16次阅读

共计 6640 个字符,预计需要花费 17 分钟才能阅读完成。

本文主要研究一下 feign 的 RequestInterceptor

RequestInterceptor

feign-core-10.2.3-sources.jar!/feign/RequestInterceptor.java

public interface RequestInterceptor {

  /**
   * Called for every request. Add data using methods on the supplied {@link RequestTemplate}.
   */
  void apply(RequestTemplate template);
}
  • RequestInterceptor 接口定义了 apply 方法,其参数为 RequestTemplate;它有一个抽象类为 BaseRequestInterceptor,还有几个实现类分别为 BasicAuthRequestInterceptor、FeignAcceptGzipEncodingInterceptor、FeignContentGzipEncodingInterceptor

BasicAuthRequestInterceptor

feign-core-10.2.3-sources.jar!/feign/auth/BasicAuthRequestInterceptor.java

public class BasicAuthRequestInterceptor implements RequestInterceptor {

  private final String headerValue;

  /**
   * Creates an interceptor that authenticates all requests with the specified username and password
   * encoded using ISO-8859-1.
   *
   * @param username the username to use for authentication
   * @param password the password to use for authentication
   */
  public BasicAuthRequestInterceptor(String username, String password) {this(username, password, ISO_8859_1);
  }

  /**
   * Creates an interceptor that authenticates all requests with the specified username and password
   * encoded using the specified charset.
   *
   * @param username the username to use for authentication
   * @param password the password to use for authentication
   * @param charset the charset to use when encoding the credentials
   */
  public BasicAuthRequestInterceptor(String username, String password, Charset charset) {checkNotNull(username, "username");
    checkNotNull(password, "password");
    this.headerValue = "Basic" + base64Encode((username + ":" + password).getBytes(charset));
  }

  /*
   * This uses a Sun internal method; if we ever encounter a case where this method is not
   * available, the appropriate response would be to pull the necessary portions of Guava's
   * BaseEncoding class into Util.
   */
  private static String base64Encode(byte[] bytes) {return Base64.encode(bytes);
  }

  @Override
  public void apply(RequestTemplate template) {template.header("Authorization", headerValue);
  }
}
  • BasicAuthRequestInterceptor 实现了 RequestInterceptor 接口,其 apply 方法往 RequestTemplate 添加名为 Authorization 的 header

BaseRequestInterceptor

spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/encoding/BaseRequestInterceptor.java

public abstract class BaseRequestInterceptor implements RequestInterceptor {

    /**
     * The encoding properties.
     */
    private final FeignClientEncodingProperties properties;

    /**
     * Creates new instance of {@link BaseRequestInterceptor}.
     * @param properties the encoding properties
     */
    protected BaseRequestInterceptor(FeignClientEncodingProperties properties) {Assert.notNull(properties, "Properties can not be null");
        this.properties = properties;
    }

    /**
     * Adds the header if it wasn't yet specified.
     * @param requestTemplate the request
     * @param name the header name
     * @param values the header values
     */
    protected void addHeader(RequestTemplate requestTemplate, String name,
            String... values) {if (!requestTemplate.headers().containsKey(name)) {requestTemplate.header(name, values);
        }
    }

    protected FeignClientEncodingProperties getProperties() {return this.properties;}

}
  • BaseRequestInterceptor 定义了 addHeader 方法,往 requestTemplate 添加非重名的 header

FeignAcceptGzipEncodingInterceptor

spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/encoding/FeignAcceptGzipEncodingInterceptor.java

public class FeignAcceptGzipEncodingInterceptor extends BaseRequestInterceptor {

    /**
     * Creates new instance of {@link FeignAcceptGzipEncodingInterceptor}.
     * @param properties the encoding properties
     */
    protected FeignAcceptGzipEncodingInterceptor(FeignClientEncodingProperties properties) {super(properties);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void apply(RequestTemplate template) {

        addHeader(template, HttpEncoding.ACCEPT_ENCODING_HEADER,
                HttpEncoding.GZIP_ENCODING, HttpEncoding.DEFLATE_ENCODING);
    }

}
  • FeignAcceptGzipEncodingInterceptor 继承了 BaseRequestInterceptor,它的 apply 方法往 RequestTemplate 添加了名为 Accept-Encoding,值为 gzip,deflate 的 header

FeignContentGzipEncodingInterceptor

spring-cloud-openfeign-core-2.2.0.M1-sources.jar!/org/springframework/cloud/openfeign/encoding/FeignContentGzipEncodingInterceptor.java

public class FeignContentGzipEncodingInterceptor extends BaseRequestInterceptor {

    /**
     * Creates new instance of {@link FeignContentGzipEncodingInterceptor}.
     * @param properties the encoding properties
     */
    protected FeignContentGzipEncodingInterceptor(FeignClientEncodingProperties properties) {super(properties);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void apply(RequestTemplate template) {if (requiresCompression(template)) {
            addHeader(template, HttpEncoding.CONTENT_ENCODING_HEADER,
                    HttpEncoding.GZIP_ENCODING, HttpEncoding.DEFLATE_ENCODING);
        }
    }

    /**
     * Returns whether the request requires GZIP compression.
     * @param template the request template
     * @return true if request requires compression, false otherwise
     */
    private boolean requiresCompression(RequestTemplate template) {final Map<String, Collection<String>> headers = template.headers();
        return matchesMimeType(headers.get(HttpEncoding.CONTENT_TYPE))
                && contentLengthExceedThreshold(headers.get(HttpEncoding.CONTENT_LENGTH));
    }

    /**
     * Returns whether the request content length exceed configured minimum size.
     * @param contentLength the content length header value
     * @return true if length is grater than minimum size, false otherwise
     */
    private boolean contentLengthExceedThreshold(Collection<String> contentLength) {

        try {if (contentLength == null || contentLength.size() != 1) {return false;}

            final String strLen = contentLength.iterator().next();
            final long length = Long.parseLong(strLen);
            return length > getProperties().getMinRequestSize();
        }
        catch (NumberFormatException ex) {return false;}
    }

    /**
     * Returns whether the content mime types matches the configures mime types.
     * @param contentTypes the content types
     * @return true if any specified content type matches the request content types
     */
    private boolean matchesMimeType(Collection<String> contentTypes) {if (contentTypes == null || contentTypes.size() == 0) {return false;}

        if (getProperties().getMimeTypes() == null
                || getProperties().getMimeTypes().length == 0) {
            // no specific mime types has been set - matching everything
            return true;
        }

        for (String mimeType : getProperties().getMimeTypes()) {if (contentTypes.contains(mimeType)) {return true;}
        }

        return false;
    }

}
  • FeignContentGzipEncodingInterceptor 继承了 BaseRequestInterceptor,其 apply 方法先判断是否需要 compression,即 mimeType 是否符合要求以及 content 大小是否超出阈值,需要 compress 的话则添加名为 Content-Encoding,值为 gzip,deflate 的 header

小结

  • RequestInterceptor 接口定义了 apply 方法,其参数为 RequestTemplate;它有一个抽象类为 BaseRequestInterceptor,还有几个实现类分别为 BasicAuthRequestInterceptor、FeignAcceptGzipEncodingInterceptor、FeignContentGzipEncodingInterceptor
  • BasicAuthRequestInterceptor 实现了 RequestInterceptor 接口,其 apply 方法往 RequestTemplate 添加名为 Authorization 的 header
  • BaseRequestInterceptor 定义了 addHeader 方法,往 requestTemplate 添加非重名的 header;FeignAcceptGzipEncodingInterceptor 继承了 BaseRequestInterceptor,它的 apply 方法往 RequestTemplate 添加了名为 Accept-Encoding,值为 gzip,deflate 的 header;FeignContentGzipEncodingInterceptor 继承了 BaseRequestInterceptor,其 apply 方法先判断是否需要 compression,即 mimeType 是否符合要求以及 content 大小是否超出阈值,需要 compress 的话则添加名为 Content-Encoding,值为 gzip,deflate 的 header

doc

  • RequestInterceptor

正文完
 0