Injecting FilterRegistrationBean into springboot does not take effect
review
Recently, I have customized two filters, 接口请求返回加密
and sql注入处理
filters, because after encapsulating some toolkits, I plan to make an annotation after I adjust it separately, just like springboot
adding to the startup class @EnableScheduling
, and I can control it at will. When I don’t want this When the two filters are in effect, then you don't need to add this annotation.
Of course, I thought of FilterRegistrationBean
using the method to inject these two filters.
But when I finished writing and packaged it, I found that only sql注入过滤器
it took effect.
reason
The reason is that when I @Bean
injected through annotations FilterRegistrationBean
, I didn't alias them, which caused conflicts, and only one could be injected.
In my project, I wrote the following two methods:
Inject encryption filter
/**
* @return 注入加密过滤器
*/
@Bean
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new EncryptionFilter());
registration.addUrlPatterns("/*");
registration.setName("encryptionFilter");
registration.setOrder(1);
return registration;
}
inject sql injection filter
/**
* @return 注入加密过滤器
*/
@Bean
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new SqlInjectManagerFilter());
registration.addUrlPatterns("/*");
registration.setName("sqlInjectManagerFilter");
registration.setOrder(2);
return registration;
}
springboot
Although this question is simple, it does affect my thinking. At first, I was thinking about whether the method of triggering filter injection by adding annotations to the startup class I made was wrong. It was also because I looked at @EnableTransactionManagement
the injection method, and I almost suspected @Import
that The problem that it cannot be used multiple times after being packaged into a toolkit. Embarrassment~~~
When I compared these two methods, I suspected that the names conflicted. Therefore, they are named as follows:
/**
* @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;
}