Flutter开发App的未来及其在各行业的应用潜力分析
1284
2022-12-19
在springboot中注入FilterRegistrationBean不生效的原因
springboot注入FilterRegistrationBean不生效
回顾
最近自定义了两个过滤器,接口请求返回加密和sql注入处理过滤器,因为在封装一些工具包,我在单独调好之后,就打算做成一个注解,像springboot启动类上加@EnableScheduling一样,可以随意控制,当我不想让这俩过滤器生效的时候,那就不加这个注解就可以了。doLJPojNPm
当然我想到了FilterRegistrationBean的使用方法,注入这两个过滤器。
但是当我写完之后,打成包之后,发现只有sql注入过滤器生效。
原因
原因就是我通过@Bean注解注入FilterRegistrationBean时,没有对它们起别名,造成冲突,只能注入一个。
我的工程里,写了两个下边的方法:
注入加密过滤器
/**
* @return 注入加密过滤器
*/
@Bean
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new EncryptionFilter());
registration.addUrlPatterns("/*");
registration.setName("encryptionFilter");
registration.setOrder(1);
return registration;
}
注入sql注入过滤器
/**
* @return 注入加密过滤器
*/
@Bean
public FilterRegistrationBean filterRegistration() {
FilterRegisdoLJPojNPmtrationBean registration = new FilterRegistrationBean();
registration.setFilter(new SqlInjectManagerFilter());
registration.addUrlPatterns("/*");
registration.setName("sqlInjectManagerFilter");
registration.setOrder(2);
return registration;
}
这个问题虽然简单,但是确实影响到我思路了,我最初一直在考虑是不是我做的springboot启动类加注解触发过滤器注入的方式不对,也是因为我看了下@EnableTransactionManagement的注入方式,我差点就怀疑@Import在打成工具包后不能多个使用的问题。尴尬~~~
当我一比较这俩方法的时候,我才怀疑到名字冲突了。因此就分别对它们加了名称如下:
/**
* @return 注入加密过滤器
*/
@Bean(name = "encryptionFilter")
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new EncryptionFilter());
registration.addUrlPatterns("/*");
registration.setName("encryptionFilter");
registration.setOrder(1);
return registration;
}
/**
* @return 注入加密过滤器
*/
@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
直接在FilterRegistrationBean
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
FilterRegistrationBean
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 {
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~