servlet Filter与spring的OncePerRequestFilter

servlet 规范

javax.servlet
Filter

任何的servlet容器都要实现的,例如tomcat、undertow、jetty等等。类似于jdbc规范,制定好了一个约束,各家数据库厂商根据规范开发对应的驱动来实现访问自己的数据库。

spring 对于Filter的自定义实现

所有的Filter都位于org.springframework.web.filter

OncePerRequestFilter实现了servlet的Filter,作为抽象类子类重写其中定义的抽象方法进行自定义处理。具体实现类如下图。

需要需要进行filter层级处理数据,可以继承OncePerRequestFilter进行特殊处理。

spring boot 对于spring实现的filter进行排序处理

所有实现了OrderedFilter的filter位于org.springframework.boot.web.servlet.filter中。

filter名称排序说明
OrderedRequestContextFilter-105Spring Session过滤器后默认排序
OrderedFormContentFilter-9900确保在Spring Security之前使用此过滤器。
OrderedHiddenHttpMethodFilter-10000默认高排序,确保在Spring Security之前使用此过滤器。
OrderedCharacterEncodingFilterInteger.MIN_VALUE默认最小值

由源码中的注释得知,定义这些优先级为了适应spring自己开发的其他组件处理的。 

得知,OrderedCharacterEncodingFilter的排序值最小。其中,order值越小,等级越高。由此得知OrderedCharacterEncodingFilter的等级最高。

package org.springframework.core;

/**
 * {@code Ordered} is an interface that can be implemented by objects that
 * should be <em>orderable</em>, for example in a {@code Collection}.
 *
 * <p>The actual {@link #getOrder() order} can be interpreted as prioritization,
 * with the first object (with the lowest order value) having the highest
 * priority.
 *
 * <p>Note that there is also a <em>priority</em> marker for this interface:
 * {@link PriorityOrdered}. Consult the Javadoc for {@code PriorityOrdered} for
 * details on how {@code PriorityOrdered} objects are ordered relative to
 * <em>plain</em> {@link Ordered} objects.
 *
 * <p>Consult the Javadoc for {@link OrderComparator} for details on the
 * sort semantics for non-ordered objects.
 *
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @since 07.04.2003
 * @see PriorityOrdered
 * @see OrderComparator
 * @see org.springframework.core.annotation.Order
 * @see org.springframework.core.annotation.AnnotationAwareOrderComparator
 */
public interface Ordered {

	/**
	 * Useful constant for the highest precedence value.
	 * @see java.lang.Integer#MIN_VALUE
	 */
	int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;

	/**
	 * Useful constant for the lowest precedence value.
	 * @see java.lang.Integer#MAX_VALUE
	 */
	int LOWEST_PRECEDENCE = Integer.MAX_VALUE;


	/**
	 * Get the order value of this object.
	 * <p>Higher values are interpreted as lower priority. As a consequence,
	 * the object with the lowest value has the highest priority (somewhat
	 * analogous to Servlet {@code load-on-startup} values).
	 * <p>Same order values will result in arbitrary sort positions for the
	 * affected objects.
	 * @return the order value
	 * @see #HIGHEST_PRECEDENCE
	 * @see #LOWEST_PRECEDENCE
	 */
	int getOrder();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值