在springboot中注入FilterRegistrationBean不生效的原因
短信预约 -IT技能 免费直播动态提醒
springboot注入FilterRegistrationBean不生效
回顾
最近自定义了两个过滤器,接口请求返回加密和sql注入处理过滤器,因为在封装一些工具包,我在单独调好之后,就打算做成一个注解,像springboot启动类上加@EnableScheduling一样,可以随意控制,当我不想让这俩过滤器生效的时候,那就不加这个注解就可以了。
当然我想到了FilterRegistrationBean的使用方法,注入这两个过滤器。
但是当我写完之后,打成包之后,发现只有sql注入过滤器生效。
原因
原因就是我通过@Bean注解注入FilterRegistrationBean时,没有对它们起别名,造成冲突,只能注入一个。
我的工程里,写了两个下边的方法:
注入加密过滤器
@Bean
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new EncryptionFilter());
registration.addUrlPatterns("
@Bean
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new SqlInjectManagerFilter());
registration.addUrlPatterns("
@Bean(name = "encryptionFilter")
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new EncryptionFilter());
registration.addUrlPatterns("
@Bean(name = "sqlInjectManagerFilter")
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new SqlInjectManagerFilter());
registration.addUrlPatterns("/*");
registration.setName("sqlInjectManagerFilter");
registration.setOrder(2);
return registration;
}
springboot @webfilter FilterRegistrationBean「T」
1、@webfilter过滤指定url
在启动类加注解@ServletComponentScan
2、Filter类:
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 javax.servlet.annotation.WebFilter;
@WebFilter(urlPatterns="/task")
public class DemoFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("Filter demo task");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
3、@webfilter过滤全部路径
在filter类上加 @Component 或 @Configuration 注解:
4、FilterRegistrationBean<T> 过滤指定url 或 全部路径
直接在FilterRegistrationBean<T>中配置即可:
import org.apache.catalina.filters.RemoteIpFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Configuration
public class ConfigurationFilter {
@Bean
public RemoteIpFilter remoteIpFilter() {
return new RemoteIpFilter();
}
@Bean
public FilterRegistrationBean<MyFilter> testFilterRegistration() {
FilterRegistrationBean<MyFilter> registration = new FilterRegistrationBean<MyFilter>();
registration.setFilter(new MyFilter());//添加过滤器
registration.addUrlPatterns("/user");//设置过滤路径,/*所有路径
//registration.addInitParameter("name", "alue");//添加默认参数
registration.setName("MyFilter");//设置优先级
registration.setOrder(2);//设置优先级
return registration;
}
public class MyFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain
filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) srequest;
System.out.println("this is MyFilter,url :" + request.getRequestURI());
filterChain.doFilter(srequest, sresponse);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341