Linux中怎么用cat命令创建文件并写入数据
257
2023-03-28
SpringBoot注册Filter的两种实现方式
springboot版本:2.2.5
一、filter注册
springboot中添加filter有两种方式:
1、实现方法一
package com.example.demo.filter1;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.stereotype.Component;
/**
* Springboot实现filter的方式一:
* 实现Filter接口,并使用@Component注解
* 需要注意的是:这个方式注册的filter的执行顺序优先级较低
* @author Administrator
*
*/
@Component
public class FilterSample1 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("这里是"+this.getClass().getName()+"的init");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("这里是"+this.getClass().getName()+"的doFilter");
chain.doFilter(request,response);
}
@Override
public void destroy() {
System.out.println("这里是"+this.getClass().getName()+"的doFilter");
}
}
2、实现方法二
package com.example.demo.filter2;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class FilterSample2 implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("这里是"+this.getClass().getName()+"的doFilter");
chain.doFilter(request,response);
}
}
package com.example.demo.filter2;
import java.util.ArrayList;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Springboot注册filter方法二:
* 同样要实现filter接口,但filter注册使用@Bean的方式进行注册,可以设置filter顺序
* @author Administrator
*
*/
@Configuration
public class WebConfig {
@Bean
public FilterRegistrationBean
FilterRegistrationBean
FilterSample2 myFilter = new FilterSample2();
registrationBean.setFilter(myFilter);
ArrayList
urls.add("/*");//配置过滤规则
registrationBean.setUrlPatterns(urls);
registrationBean.setOrder(3);
return registrationBean;
}
}
二、Springboot自带filter
Springboot默认启用了三个filter,他们是requestContextFilter,characterEncodingFilter,formContentFilter,可以通过在application.yml中设置debug: true来验证如下:
2020-03-17 22:13:17.254 DEBUG 2700 --- [ main] o.s.b.w.s.f.OrderedRequestContextFilter : Filter 'requestContextFilter' configured for use
2020-03-17 22:13:17.255 DEBUG 2700 --- [ main] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2020-03-17 22:13:17.255 DEBUG 2700 --- [ BwJiYQ; main] o.s.b.w.s.f.OrderedFormContentFilter : Filter 'formContentFilter' configured for use
三、查看应用中注册了哪些filter
#application.yml
debug: true
如上图,启用debug模式,查看日志可得:
标蓝部分详细内容如下:
2020-03-17 22:13:17.244 DEBUG 2700 --- [ main] o.s.b.w.s.ServletContextInitializerBeans :
Mapping filters:
filterRegistrationBean urls=[/*] order=3,
characterEncodingFilter urls=[/*] order=-2147483648,
formContentFilter urls=[/*] order=-9900,
requestContextFilter urls=[/*] order=-105,
filterSample1 urls=[/*] order=2147483647
可以看出,该应用注册了五个filter,执行顺序由order决定
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~