springboot-整合filter的两种方式

1.springboot通过扫描注解,@ServletComponentScan和@WebFilter实现

1.1编写FirstFilter类实现Filter接口

package com.zhong.spring_boot_filter_03.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;

/**
 * springboot 整合filter方式之一
 * 
 * 以前web.xml配置:
 * 
 *      FirstFilter
 *      com.zhong.spring_boot_filter_03.filter.FirstFilter
 * 
 * 
 * 
 *       FirstFilter
 *       /first
 * 
 * 
 * FirstFilter
 */
@WebFilter(value="FirstFilter",urlPatterns = {"/filter"})
public class FirstFilter 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("进入 FirstFilter ");
                chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }

    
}

1.2编写启动类,注意启动类加上@ServletComponentScan注解

package com.zhong.spring_boot_filter_03;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * SpringBoot 整合filter方式1
 */
@SpringBootApplication
@ServletComponentScan //
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

2.通过FilterRegistrationBean注册Filter

2.1编写SecondFilter类,实现Filter接口。(注意:没有添加@WebFilter注解)

package com.zhong.spring_boot_filter_03.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;

/**
 * SecondFilter
 */
public class SecondFilter 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("进入SecondFilter");
            chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }

    
}

2.2编写启动类App2.class,并且添加FilterRegistrationBean

package com.zhong.spring_boot_filter_03;

import com.zhong.spring_boot_filter_03.filter.SecondFilter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

/**
 * App2 springboot 整合filter方式2
 */
@SpringBootApplication
public class App2 {

    public static void main(String[] args) {
        SpringApplication.run(App2.class, args);
    }
    /**
     * 注册filter
     * @return
     */
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean  bean = new FilterRegistrationBean(new SecondFilter());
        //bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
        bean.addUrlPatterns("/firstServlet");
        return bean;
    }
}

项目地址:https://github.com/gougou1993/spring_boot_filter_03

你可能感兴趣的:(SpringBoot)