Categories
Uncategorized

Spring Boot two ways to configure the filter

Filter (Filter) Servlet is a commonly used technology that enables the user before accessing a target resource access requests and responses to intercept, frequently used scene have a login verification, access control, sensitive word filtering, here Spring in two ways Boot arranged filters.

Article directory

A, @ WebFilter annotation Second way, @ Bean notes Three ways to view operating results

A, @ WebFilter annotation mode

Use @WebFilter annotated as declaration of this class filter, the first filter a name for the parameter, the second parameter is a diagram of the request address to block, the current need to achieve Filter interface, there are three methods, namely the filter initialization, and a filter filtration method destruction.

@Slf4j
@WebFilter(filterName = "myFilter1", urlPatterns = "/*")
public class MyFilter1 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info(filterConfig.getFilterName() + " init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        log.info("myFilter1 begin");
        try {
            log.info(

"Business method performs"

);
            chain.doFilter(request, response);
        } catch (Exception e) {
            log.error("error!", e);
        }
        log.info("myFilter1 end");
    }

    @Override
    public void destroy() {
    }
}

Start adding @ServletComponentScan class annotation, annotation @ ServletComponentScan package path must contain the scanned Filter, code is as follows:

@SpringBootApplication
@ServletComponentScan(basePackages = "com.example.demo.filter")
public class DemoApplication {

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

Two, @ Bean annotation mode

New MyFilter2.java class, do not add comment @WebFilter, code is as follows:

@Slf4j
public class MyFilter2 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info(filterConfig.getFilterName() + " init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
        log.info("myFilter2 begin");
        try {
            log.info(

"Business method performs"

);
            chain.doFilter(request, response);
        } catch (Exception e) {
            log.error("error!", e);
        }
        log.info("myFilter2 end");
    }

    @Override
    public void destroy() {
    }
}

New configuration class WebConfig.java, configuration bean, the following code:

@Configuration
public class WebConfig {

    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean(new MyFilter2());
        registration.addUrlPatterns("/test"); //
        registration.setName("myFilter2");
        return registration;
    }
}

Third, view operating results

After the project launch a browser to http: // localhost: 8080 / test, you can see that the filter has taken effect, background printing log is as follows:

[nio-8080-exec-1] com.example.demo.filter.MyFilter1        : myFilter1 begin
[nio-8080-exec-1] com.example.demo.filter.MyFilter1        : 业务方法执行
[nio-8080-exec-1] com.example.demo.filter.MyFilter1        : myFilter1 end 

This Spring Boot Configuration filters have all been achieved in two ways, there are questions please leave a message to communicate Oh! Complete Source Address: https://github.com/suisui2019/springboot-study

Recommended Reading

1.Spring Boot unified exception handling actual 2. The following analysis from a technical point of view Why do not send “artwork” on the Internet 3. Use Spring Boot + zxing, generate two-dimensional code can be so simple 4.Spring Boot of Profile– fast get multi-use environments and switch 5. Spring Boot + WxJava achieve integration site login feature WeChat


Limited Time receive a free Java-related information, covering Java, Redis, MongoDB, MySQL, Zookeeper, Spring Cloud, Dubbo / Kafka, Hadoop, Hbase, Flink high concurrent distributed, big data and machine learning techniques. Watch below the number to receive a free public:

Java annoying chatters public No.

 

Leave a Reply