springboot中filter的配置和顺序执行
项目结构
springboot版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
Filter代码
AFilter
package com.wang.sb.filter; import javax.servlet.*; import java.io.IOException; /** * @Author wly * @Date 2018/6/14 10:54 */ public class AFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("I'm AFilter"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } }
BFilter
package com.wang.sb.filter; import javax.servlet.*; import java.io.IOException; /** * @Author wly * @Date 2018/6/14 10:54 */ public class BFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("I'm BFilter"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
CFilter
package com.wang.sb.filter; import javax.servlet.*; import java.io.IOException; /** * @Author wly * @Date 2018/6/14 10:54 */ public class CFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("I'm CFilter"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
DFilter
package com.wang.sb.filter; import javax.servlet.*; import java.io.IOException; /** * @Author wly * @Date 2018/6/14 10:54 */ public class DFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("I'm DFilter"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
EFilter
package com.wang.sb.filter; import javax.servlet.*; import java.io.IOException; /** * @Author wly * @Date 2018/6/14 10:54 */ public class EFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("I'm EFilter"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } }
Filter配置:名称、拦截位置、执行顺序等
package com.wang.sb.config;
import com.wang.sb.filter.*;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
/**
* @Description
* @Author wly
* @Date 2018/6/14 14:20
*/
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean buildAFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setOrder(6);
filterRegistrationBean.setFilter(new AFilter());
filterRegistrationBean.setName("filter1");
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
@Bean
public FilterRegistrationBean buildBFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setOrder(2);
filterRegistrationBean.setFilter(new BFilter());
filterRegistrationBean.setName("filter2");
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
@Bean
public FilterRegistrationBean buildCFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setOrder(3);
filterRegistrationBean.setFilter(new CFilter());
filterRegistrationBean.setName("filter3");
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
@Bean
public FilterRegistrationBean buildDFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setOrder(4);
filterRegistrationBean.setFilter(new DFilter());
filterRegistrationBean.setName("filter4");
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
@Bean
public FilterRegistrationBean buildEFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setOrder(5);
filterRegistrationBean.setFilter(new EFilter());
filterRegistrationBean.setName("filter5");
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
}
控制台日志(启动服务加载了filter)
"D:\dev tools\jdk\jdk8\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:59538,suspend=y,server=n -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=59537 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -Dfile.encoding=UTF-8 -classpath "D:\dev tools\jdk\jdk8\jre\lib\charsets.jar;D:\dev tools\jdk\jdk8\jre\lib\deploy.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\access-bridge-64.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\cldrdata.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\dnsns.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\jaccess.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\jfxrt.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\localedata.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\nashorn.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\sunec.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\sunjce_provider.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\sunmscapi.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\sunpkcs11.jar;D:\dev tools\jdk\jdk8\jre\lib\ext\zipfs.jar;D:\dev tools\jdk\jdk8\jre\lib\javaws.jar;D:\dev tools\jdk\jdk8\jre\lib\jce.jar;D:\dev tools\jdk\jdk8\jre\lib\jfr.jar;D:\dev tools\jdk\jdk8\jre\lib\jfxswt.jar;D:\dev tools\jdk\jdk8\jre\lib\jsse.jar;D:\dev tools\jdk\jdk8\jre\lib\management-agent.jar;D:\dev tools\jdk\jdk8\jre\lib\plugin.jar;D:\dev tools\jdk\jdk8\jre\lib\resources.jar;D:\dev tools\jdk\jdk8\jre\lib\rt.jar;E:\work\sb\target\classes;E:\work\localRepository\org\springframework\boot\spring-boot-starter-web\1.5.8.RELEASE\spring-boot-starter-web-1.5.8.RELEASE.jar;E:\work\localRepository\org\springframework\boot\spring-boot-starter\1.5.8.RELEASE\spring-boot-starter-1.5.8.RELEASE.jar;E:\work\localRepository\org\springframework\boot\spring-boot\1.5.8.RELEASE\spring-boot-1.5.8.RELEASE.jar;E:\work\localRepository\org\springframework\boot\spring-boot-starter-logging\1.5.8.RELEASE\spring-boot-starter-logging-1.5.8.RELEASE.jar;E:\work\localRepository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;E:\work\localRepository\org\slf4j\log4j-over-slf4j\1.7.25\log4j-over-slf4j-1.7.25.jar;E:\work\localRepository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;E:\work\localRepository\org\springframework\boot\spring-boot-starter-tomcat\1.5.8.RELEASE\spring-boot-starter-tomcat-1.5.8.RELEASE.jar;E:\work\localRepository\org\apache\tomcat\embed\tomcat-embed-core\8.5.23\tomcat-embed-core-8.5.23.jar;E:\work\localRepository\org\apache\tomcat\tomcat-annotations-api\8.5.23\tomcat-annotations-api-8.5.23.jar;E:\work\localRepository\org\apache\tomcat\embed\tomcat-embed-el\8.5.23\tomcat-embed-el-8.5.23.jar;E:\work\localRepository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.23\tomcat-embed-websocket-8.5.23.jar;E:\work\localRepository\org\hibernate\hibernate-validator\5.3.5.Final\hibernate-validator-5.3.5.Final.jar;E:\work\localRepository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;E:\work\localRepository\org\jboss\logging\jboss-logging\3.3.1.Final\jboss-logging-3.3.1.Final.jar;E:\work\localRepository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;E:\work\localRepository\com\fasterxml\jackson\core\jackson-databind\2.8.10\jackson-databind-2.8.10.jar;E:\work\localRepository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar;E:\work\localRepository\com\fasterxml\jackson\core\jackson-core\2.8.10\jackson-core-2.8.10.jar;E:\work\localRepository\org\springframework\spring-web\4.3.12.RELEASE\spring-web-4.3.12.RELEASE.jar;E:\work\localRepository\org\springframework\spring-aop\4.3.12.RELEASE\spring-aop-4.3.12.RELEASE.jar;E:\work\localRepository\org\springframework\spring-beans\4.3.12.RELEASE\spring-beans-4.3.12.RELEASE.jar;E:\work\localRepository\org\springframework\spring-webmvc\4.3.12.RELEASE\spring-webmvc-4.3.12.RELEASE.jar;E:\work\localRepository\org\springframework\spring-expression\4.3.12.RELEASE\spring-expression-4.3.12.RELEASE.jar;E:\work\localRepository\com\github\tobato\fastdfs-client\1.25.2-RELEASE\fastdfs-client-1.25.2-RELEASE.jar;E:\work\localRepository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\work\localRepository\org\slf4j\jcl-over-slf4j\1.7.25\jcl-over-slf4j-1.7.25.jar;E:\work\localRepository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;E:\work\localRepository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;E:\work\localRepository\org\apache\commons\commons-lang3\3.3.2\commons-lang3-3.3.2.jar;E:\work\localRepository\commons-beanutils\commons-beanutils\1.9.3\commons-beanutils-1.9.3.jar;E:\work\localRepository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;E:\work\localRepository\commons-io\commons-io\2.4\commons-io-2.4.jar;E:\work\localRepository\org\apache\commons\commons-pool2\2.4.2\commons-pool2-2.4.2.jar;E:\work\localRepository\org\springframework\spring-core\4.3.12.RELEASE\spring-core-4.3.12.RELEASE.jar;E:\work\localRepository\org\springframework\boot\spring-boot-autoconfigure\1.5.8.RELEASE\spring-boot-autoconfigure-1.5.8.RELEASE.jar;E:\work\localRepository\org\mockito\mockito-all\1.9.5\mockito-all-1.9.5.jar;E:\work\localRepository\org\springframework\spring-context\4.3.12.RELEASE\spring-context-4.3.12.RELEASE.jar;E:\work\localRepository\net\coobird\thumbnailator\0.4.8\thumbnailator-0.4.8.jar;D:\dev tools\ide\ideaIU-2017.2.5.win\lib\idea_rt.jar" com.wang.sb.SbApplication Connected to the target VM, address: '127.0.0.1:59538', transport: 'socket' . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.8.RELEASE) 2018-06-14 15:03:26.872 INFO 14760 --- [ main] com.wang.sb.SbApplication : Starting SbApplication on CFJSWLY with PID 14760 (E:\work\sb\target\classes started by admin in E:\work\sb) 2018-06-14 15:03:26.892 INFO 14760 --- [ main] com.wang.sb.SbApplication : The following profiles are active: dev 2018-06-14 15:03:27.040 INFO 14760 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3697186: startup date [Thu Jun 14 15:03:27 CST 2018]; root of context hierarchy 2018-06-14 15:03:28.755 INFO 14760 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2018-06-14 15:03:28.769 INFO 14760 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-06-14 15:03:28.770 INFO 14760 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23 2018-06-14 15:03:28.935 INFO 14760 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-06-14 15:03:28.935 INFO 14760 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1901 ms 2018-06-14 15:03:29.185 INFO 14760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-06-14 15:03:29.186 INFO 14760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-06-14 15:03:29.186 INFO 14760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-06-14 15:03:29.186 INFO 14760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-06-14 15:03:29.186 INFO 14760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'filter2' to: [/*] 2018-06-14 15:03:29.186 INFO 14760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'filter3' to: [/*] 2018-06-14 15:03:29.186 INFO 14760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'filter4' to: [/*] 2018-06-14 15:03:29.186 INFO 14760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'filter5' to: [/*] 2018-06-14 15:03:29.186 INFO 14760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'filter1' to: [/*] 2018-06-14 15:03:29.186 INFO 14760 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2018-06-14 15:03:29.401 WARN 14760 --- [ main] figurationPropertiesBindingPostProcessor : The @ConfigurationProperties bean class com.github.tobato.fastdfs.conn.TrackerConnectionManager contains validation constraints but had not been annotated with @Validated. 2018-06-14 15:03:29.401 WARN 14760 --- [ main] figurationPropertiesBindingPostProcessor : The @ConfigurationProperties bean class com.github.tobato.fastdfs.conn.TrackerConnectionManager contains validation constraints but had not been annotated with @Validated. 2018-06-14 15:03:29.402 WARN 14760 --- [ main] figurationPropertiesBindingPostProcessor : The @ConfigurationProperties bean class com.github.tobato.fastdfs.conn.TrackerConnectionManager contains validation constraints but had not been annotated with @Validated. 2018-06-14 15:03:29.852 INFO 14760 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3697186: startup date [Thu Jun 14 15:03:27 CST 2018]; root of context hierarchy 2018-06-14 15:03:29.928 INFO 14760 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/a]}" onto public java.lang.String com.wang.sb.web.HelloController.hello() 2018-06-14 15:03:29.930 INFO 14760 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/download]}" onto public void com.wang.sb.web.HelloController.downloadFile(java.lang.String,javax.servlet.http.HttpServletResponse) throws java.io.IOException 2018-06-14 15:03:29.931 INFO 14760 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/upload]}" onto public java.lang.String com.wang.sb.web.HelloController.uploadFile(org.springframework.web.multipart.MultipartFile) throws java.io.IOException 2018-06-14 15:03:29.933 INFO 14760 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-06-14 15:03:29.934 INFO 14760 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-06-14 15:03:29.970 INFO 14760 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-06-14 15:03:29.970 INFO 14760 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-06-14 15:03:30.030 INFO 14760 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-06-14 15:03:30.287 INFO 14760 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-06-14 15:03:30.288 INFO 14760 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'fdfsConnectionPool' has been autodetected for JMX exposure 2018-06-14 15:03:30.294 INFO 14760 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'fdfsConnectionPool': registering with JMX server as MBean [com.github.tobato.fastdfs.conn:name=fdfsConnectionPool,type=FdfsConnectionPool] 2018-06-14 15:03:30.360 INFO 14760 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2018-06-14 15:03:30.368 INFO 14760 --- [ main] com.wang.sb.SbApplication : Started SbApplication in 4.506 seconds (JVM running for 7.32)
请求URL(测试filter效果)
{{env}}/hello/a
控制台日志
I'm BFilter I'm CFilter I'm DFilter I'm EFilter I'm AFilter
亲测有效!