feign中RequestInterceptor的原理和作用

发布时间:2021-07-02 15:18:41 作者:chen
来源:亿速云 阅读:1470

这篇文章主要讲解了“feign中RequestInterceptor的原理和作用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“feign中RequestInterceptor的原理和作用”吧!

本文主要研究一下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);
}

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);
  }
}

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;
	}

}

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);
	}

}

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;
	}

}

小结

感谢各位的阅读,以上就是“feign中RequestInterceptor的原理和作用”的内容了,经过本文的学习后,相信大家对feign中RequestInterceptor的原理和作用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. SpringCloud中Feign组件的作用是什么
  2. Feign原理是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

feign

上一篇:Python中如何使用struct模板

下一篇:python中怎么实现一个带参数函数装饰器

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》