servlet Filter与spring的OncePerRequestFilter

servlet 规范

javax.servlet
Filter

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

spring 对于Filter的自定义实现

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

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

servlet Filter与spring的OncePerRequestFilter_第1张图片

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

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

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

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

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

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

package org.springframework.core;

/**
 * {@code Ordered} is an interface that can be implemented by objects that
 * should be orderable, for example in a {@code Collection}.
 *
 * 

The actual {@link #getOrder() order} can be interpreted as prioritization, * with the first object (with the lowest order value) having the highest * priority. * *

Note that there is also a priority marker for this interface: * {@link PriorityOrdered}. Consult the Javadoc for {@code PriorityOrdered} for * details on how {@code PriorityOrdered} objects are ordered relative to * plain {@link Ordered} objects. * *

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. *

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). *

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

你可能感兴趣的:(java,servlet,spring,java)