当前位置:主页 > 软件编程 > JAVA代码 >

在springboot中注入FilterRegistrationBean不生效的原因

时间:2023-02-05 09:39:40 | 栏目:JAVA代码 | 点击:

springboot注入FilterRegistrationBean不生效

回顾

最近自定义了两个过滤器,接口请求返回加密和sql注入处理过滤器,因为在封装一些工具包,我在单独调好之后,就打算做成一个注解,像springboot启动类上加@EnableScheduling一样,可以随意控制,当我不想让这俩过滤器生效的时候,那就不加这个注解就可以了。

当然我想到了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() {
        FilterRegistrationBean 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<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 {
        }
    }
}

您可能感兴趣的文章:

相关文章