Three ways to achieve the Spring Boot Filter

Use @WebFilter a comment

@WebFilter written on the class

@WebFilter common attributesHere Insert Picture Description

Filter Implementation

  1. Javax.servlet.Filter write filter class implements an interface, plus @WebFilter notes, can not set the filter execution order
package com.jsong.wiki.blog.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter(filterName = "testfilter",urlPatterns ={"/blog/*"})
public class AnnotatedRequestFitler implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.printf("filter");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

  1. @ServletComponentScan added in the configuration class has @Configuration also be directly added to the starting class

@ServletComponentScan not automatically scan the default packet where the current class parameter, the packet may be designated scan

package com.jsong.wiki.blog.filter;

import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Configuration;

@ServletComponentScan(value = "com.jsong.wiki.blog.filter")
@Configuration
public class TestConfig {
}

  1. Filter configured, when accessing http: // localhost: When 8080 / blog / test, the console will be printed filter

Two configuration Bean

  1. Write a filter class that implements the interface javax.servlet.Filter
package com.jsong.wiki.blog.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

public class RequestFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.printf("hello");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

  1. In the bean configuration instantiate this class Filter
package com.jsong.wiki.blog.config;

import com.jsong.wiki.blog.filter.RequestFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

@Configuration
public class FilterConfig {

    @Bean
    public Filter registerRequestFilter() {
        return new RequestFilter();
    }
}

Three FilterRegistrationBean (recommended), you can configure the filter order

  1. And about the same way, first create a filter class that implements the interface javax.servlet.Filter
    Note To write a comment on @Component class, this class to container-managed
package com.jsong.wiki.blog.filter;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

@Component
public class RequestFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.printf("hello");
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

  1. In the configuration class by the filter management FilterRegistrationBean
package com.jsong.wiki.blog.config;

import com.jsong.wiki.blog.filter.RequestFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

@Configuration
public class FilterConfig {

//    @Bean
//    public Filter registerRequestFilter() {
//        return new RequestFilter();
//    }

    @Bean
    public FilterRegistrationBean registration(RequestFilter filter) {
        FilterRegistrationBean bean = new FilterRegistrationBean(filter);
        bean.addUrlPatterns("/blog/*");
        // 排序
        bean.setOrder(1);
        return bean;
    }
}

  1. bean.setOrder (1); a filter disposed execution order, the smaller the first execution

Precautions

Be sure to set the filter urlParten, whether those filters will go twice or more times an incorrect address will be requested through the filter.

chestnut

When the note off urlParten

DoFilter break point in the method, in the request at
http: // localhost: 8080 / blog / test

  • The first breakpoint url / blog / test
    Here Insert Picture Description
  • The second request url /favicon.ico breakpoint icon, will filter through
    Here Insert Picture Description
Published 83 original articles · won praise 21 · views 50000 +

Guess you like

Origin blog.csdn.net/JsongNeu/article/details/103503971