Listener��Filter��Servlet��Java Web寮���杩�绋�涓�甯哥�ㄧ��涓�涓�缁�浠讹��朵腑Filter缁�浠剁��浣跨�ㄩ�����楂�锛�缁�甯歌��ㄦ�ュ��绠���������澶�����璇锋�澶磋�婊ゅ���叉��XSS***绛���濡�����浠�浣跨�ㄧ����浼�缁���Spring MVC杩�琛�寮���锛��d�����瑕���Tomcat��web.xml��浠朵腑杩�琛�濡�涓���缃��冲��锛�

<!-- ��缃�Listener -->
<listener>
    <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> 

<!--��缃�Filter锛�杩�杈归��缃�河渠�涓�涓�Filter锛�浣����归��河渠�澶�涓�url-pattern-->
<!-- 浠�url-partern�瑰���缃���filter涓�锛�濡�����澶�涓�涓�褰���璇锋��归��锛�����web.xml涓�filter-mapping�雾�扮演��椤哄��ヨ�琛�-->
<filter>  
    <filter-name>filter1</filter-name>  
    <filter-class>com.csx.MyFilter</filter-class>  
</filter>  
<filter-mapping>
    <filter-name>filter1</filter-name>  
    <url-pattern>/url/a/*</url-pattern>  
</filter-mapping>  
<filter-mapping>  
    <filter-name>filter1</filter-name>  
    <url-pattern>/url/b/*</url-pattern>  
</filter-mapping>

<!--��缃�Servlet-->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc.spring.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!-- 杩�杈逛�寤鸿������/* -->
    <url-pattern>/</url-pattern>
</servlet-mapping>

PS锛��ㄥ�瑰�ㄥ���ㄧ���跺��锛�涓���涓�涓�缁�浠跺���ㄧ��椤哄���Listener --> Filter --> Servlet锛�杩�杈瑰���╀�涓�璁板����规�锛������ㄩ『搴�璁板�������锛�Listener锛���锛�Filter锛�甯�锛�Servlet锛����冲����

��web.xml涓���缃�杩�涓�涓�缁�浠舵��杈�绠���锛�浣���浣跨��Spring-Boot寮����朵娇�ㄧ����稳定��ュ�疏浚瑰��锛�骞舵病��web.xml��浠惰�╂��浠�杩�琛���缃����d���Spring-Boot涓��板�瑕���涔���缃�Listener��Filter��Servlet绛�缁�浠跺��锛�

��绡���疏浚�浠�Filter涓哄��锛�浠�缁�涓���Spring-Boot涓���涔���缃�Listener��Filter��Servlet绛�缁�浠躲��

�瑰�涓�锛�广大�Filter澹版��涓�bean

杩�杈规��浠����ヨ��疏浚�涔�涓�涓�Filter锛�杩�涓�Filter��浣��ㄦ��缁�璁′�涓��ュ�g��璋��ㄦ�堕�淬��

public class TimeConsumingCalculationFilter implements Filter {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest httpRequest=(HttpServletRequest)request;
            long startTime = System.nanoTime();
            logger.info(">>>>>>> Begin ["+httpRequest.getRequestURI()+"]...");
            try{
                chain.doFilter(request, response);
            }finally {
                long endTime = System.nanoTime();
                logger.info(">>>>>>> End ["+httpRequest.getRequestURI()+"]["+(endTime-startTime)/1000/1000.0+"ms].");
            }
        }

    }

��Spring-Boot涓���缃�涓��㈢��Filter锛���浠�����瑕���@Configuration��浠朵腑��濡�涓���缃��冲��锛�

@Configuration
public class WebConfig {

    private static Logger logger = LoggerFactory.getLogger(WebConfig.class);

    @Bean
    public Filter filter1(){
        return new TimeConsumingCalculationFilter();
    }
}

涓��㈢��Filter榛�璁や���������璇锋�����濡���浠��宠���缃�澶�涓������ㄧ��璇�锛���瑕�����涓�涓�Bean�规�广大卞��浠ヤ���

@Configuration
public class WebConfig {

    private static Logger logger = LoggerFactory.getLogger(WebConfig.class);

    @Bean
    public Filter filter1(){
        return new TimeConsumingCalculationFilter();
    }

    @Bean
    public Filter filter2() {
        return new TimeConsumingCalculationFilter2();
    }
}

涓��㈢����缃�浠g����缃�河渠�涓や锅Filter锛�涓や锅Filter榛�璁ら�戒���������璇锋�锛�������椤哄���锛�filter1-->filter2��杩�杈圭���昏�������缃���Filter������锛�����缃���Filter��������褰��讹�濡�����浠��虫��纭���疏浚�����椤哄���璇���浠ュ����@Order娉ㄨВ��浣�����瑕�娉ㄦ������杩�涓�娉ㄨВ涓�疏浚�瑕����ㄥ��涔���绫讳�����

@Order(Ordered.LOWEST_PRECEDENCE - 2)
public class TimeConsumingCalculationFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
       //your logic
    }

}
@Order(Ordered.LOWEST_PRECEDENCE - 1)
public class TimeConsumingCalculationFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
       //your logic
    }

}

PS: @Order涓���value�艰�澶э��ц���浼���绾ц�广大����艰�广大�锛�浼���绾ц�澶с��褰���浠���疏浚�涔��ц�椤哄����跺��锛�寤鸿��浣跨��@Order(Ordered.LOWEST_PRECEDENCE - 1)杩�绉�褰㈠���缃�锛�Ordered.LOWEST_PRECEDENCE���荤���艰�澶э�浼���绾ц�楂�锛�杩��风��璧锋�ユ��杈��磋���

��澶�锛�Spring杩���岛�河渠�涓�涓�OrderedFilter�ュ�o�杩�涓��ュ�f��Filter��Ordered��缁����ュ�o�������涓��㈢����涓��风����澶у�跺��浠ョ�����仪式娇�ㄣ��

涓��㈢��杩�绉���缃��瑰�浼��规����缃�璧锋�ラ��甯哥����锛�浣���缂虹�逛�姣�杈����撅�广大辨����缃�涓�澶��垫椿锛�榛�璁や���������璇锋���

�瑰�河渠�锛�@WebFilter�瑰�

@WebFilter娉ㄨВ��Servlet涓���岛���娉ㄨВ锛�Spring涔�����杩�涓�娉ㄨВ��@WebFilter�借�琛�缁�绮�搴���杩�琛���缃�锛�姣�涓��㈢���瑰��村���垫椿��

@Configuration
public class WebConfig {
    //��浠ヨ��疏浚�涔�url-pattern
    @WebFilter(urlPatterns="/*")
    @Order(Ordered.LOWEST_PRECEDENCE - 2)
    //杩�杈瑰���涓���`@Configuration`,��瑕���杩�`@ServletComponentScan`����`Listener`��`Filter`��`Servlet`杩�涓�涓�缁�浠�
    @Configuration
    public class TimeConsumingCalculationFilter implements Filter {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            //your logic
        }
    }   

    @WebFilter(urlPatterns="/*")
    @Order(Ordered.LOWEST_PRECEDENCE - 2)
    //杩�杈瑰���涓���`@Configuration`,��瑕���杩�`@ServletComponentScan`����`Listener`��`Filter`��`Servlet`杩�涓�涓�缁�浠�
    @Configuration
    public class TimeConsumingCalculationFilter2 implements Filter {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            //your logic
        }
    }   

}

��杩��@WebFilter�娉ㄨВ���瑰���缃�Filter绠���锛���涓��藉���疏浚�涔�url-pattern������椤哄���

�瑰�涓�锛�浣跨��FilterRegistrationBean��缃�

@Configuration
public class WebConfig {

    private static Logger logger = LoggerFactory.getLogger(WebConfig.class);

    @Bean
        public FilterRegistrationBean<Filter> filter1() {
            FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
            registrationBean.setName("filter1");
            registrationBean.setOrder(Ordered.LOWEST_PRECEDENCE - 2);
            registrationBean.setFilter(new TimeConsumingCalculationFilter());
            registrationBean.addUrlPatterns("/foo/*");
            return registrationBean;
        }

        @Bean
        public FilterRegistrationBean<Filter> filter2() {
            FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
            registrationBean.setName("filter2");
            registrationBean.setOrder(Ordered.LOWEST_PRECEDENCE - 3);
            registrationBean.setFilter(new TimeConsumingCalculationFilter1());
            registrationBean.addUrlPatterns("/*");
            registrationBean.addInitParameter("key1","value1");
            registrationBean.addInitParameter("key2","value2");
            //��杩�Servlet name�归��Filter锛�涓�寤鸿��浣跨��
            registrationBean.addServletNames("name1");
            return registrationBean;
        }

}

娉ㄦ���戊寅�

  • FilterRegistrationBean 涓� Filter 涔��存��涓�瀵逛��崇郴��
  • 濡���海洋��ㄥ�涓� FilterRegistrationBean ��瑕�璋��ㄥ�� setName(String name) 涓哄�跺0����涓���绉帮���������绗�涓�涓�娉ㄥ��������������
  • 濡�����瑕�淇�璇�璋��ㄩ『搴�����杩�璋��ㄥ�� setOrder(int order) �规�杩�琛�璁剧疆��
  • �瑰���锛�浣跨��DelegatingFilterProxyRegistrationBean�瑰�

    @Configuration
    public class WebConfig {
    
        @Bean("myFilter")
        //��缃�河渠�DelegatingFilterProxyRegistrationBean��,杩�绉��瑰���缃���Filter涓�浼�����河渠�锛���浼�����/foo/*��璇锋�
        public Filter myFilter(){
            return new TimeConsumingCalculationFilter();
        }
    
        @Bean
        public DelegatingFilterProxyRegistrationBean delegatingFilterProxyRegistrationBean(){
            DelegatingFilterProxyRegistrationBean filterProxy = new DelegatingFilterProxyRegistrationBean("myFilter");
            filterProxy.addUrlPatterns("/foo/*");
            filterProxy.addInitParameter("targetFilterLifecycle","true");
            filterProxy.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico");
            filterProxy.setDispatcherTypes(DispatcherType.REQUEST);
            return filterProxy;
        }
    }

    FilterRegistrationBean��DelegatingFilterProxyRegistrationBean�哄��锛�

  • FilterRegistrationBean��杩�onStartup�规��存�ユ拓宽��filter��
  • DelegatingFilterProxyRegistrationBean��广大�DelegatingFilterProxy娉ㄥ����Servlet3.0+��疏浚瑰�ㄤ腑锛����跺���颁�ApplicationContextAware�ュ�o�疏浚�岛�ApplicationContext��杩���杩�浼��ヨ��疏浚�涔�filter����绉版�ユ�惧�瑰���bean锛�骞剁�����稿�bean��浠g��瀵硅薄��
  • 瑙�绫绘����

  • 娣诲����疏浚�涔�Servlet 涔������ㄦ�规�涓�@WebServlet ����ServletRegistrationBean
  • 娣诲����疏浚�涔�Listener涔���浠ラ���ㄦ�规�涓� @WebListener����ServletListenerRegistrationBean ,娉ㄦ������河渠�浠舵��娉���
  • �朵��稿�崇被

  • ServletComponentRegisteringPostProcessor
  • ServletComponentHandler
  • WebListenerHandler
  • WebFilterHandler
  • WebServletHandler