Three ways to achieve the Spring Boot Filter
- Use @WebFilter a comment
- @WebFilter common attributes! [Inserted here described image] (https://img-blog.csdnimg.cn/20191212101153388.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0pzb25nTmV1,size_16,color_FFFFFF,t_70 )
- Filter Implementation
- Two configuration Bean
- Three FilterRegistrationBean (recommended), you can configure the filter order
- Precautions
Use @WebFilter a comment
@WebFilter written on the class
@WebFilter common attributes
Filter Implementation
- 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() {
}
}
- @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 {
}
- Filter configured, when accessing http: // localhost: When 8080 / blog / test, the console will be printed filter
Two configuration Bean
- 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() {
}
}
- 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
- 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() {
}
}
- 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;
}
}
- 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
- The second request url /favicon.ico breakpoint icon, will filter through