【Spring Boot】Filter

【Spring Boot】Filter

=======================================================

1、自动扫描

2、手动配置

3、@WebFilter 注解

4、跨域过滤器

=======================================================

1、自动扫描

定义Filter

@Slf4j
@WebFilter(filterName = "authFilter", urlPatterns = {"/*"})
@Order(1) 
public class AuthFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

扫描Filter

@Slf4j
@ServletComponentScan(basePackages = {"cn.ycx.common.filter"}) //扫描Filter
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        log.debug("rest server start success.");
    }
}

 

2、手动配置

定义Filter

@Slf4j
@Component
public class AuthFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

Java配置Filter

@Configuration
public class FilterConfig {
    @Autowired
    private AuthFilter authFilter;
    @Bean
    public FilterRegistrationBean registerAuthFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(authFilter);
        registration.addUrlPatterns("/*");
        registration.setName("authFilter");
        registration.setOrder(1);  //值越小,Filter越靠前。
        return registration;
    }
}

web.xml配置Filter,执行顺序是按照Filter在web.xml中的先后顺序执行

<filter>
     <filter-name>AuthFilter</filter-name>
     <filter-class>cn.ycx.common.filter.AuthFilter</filter-class>
</filter>
<filter-mapping>
     <filter-name>AuthFilter</filter-name>
     <url-pattern>/*</url-pattern>
</filter-mapping>

 

3、@WebFilter 注解

@WebFilter 的属性

属性 类型 描述
filterName String 指定过滤器的 name 属性,等价于 <filter-name>
value String[] 该属性等价于 urlPatterns 属性。但是两者不应该同时使用。
urlPatterns String[] 指定一组过滤器的 URL 匹配模式。等价于 <url-pattern> 标签。
servletNames String[] 指定过滤器将应用于哪些 Servlet。取值是 @WebServlet 中的 name 属性的取值,或者是 web.xml 中 <servlet-name> 的取值。
dispatcherTypes DispatcherType 指定过滤器的转发模式。具体取值包括: ASYNC、ERROR、FORWARD、INCLUDE、REQUEST。
initParams WebInitParam[] 指定一组过滤器初始化参数,等价于 <init-param> 标签。
asyncSupported boolean 声明过滤器是否支持异步操作模式,等价于 <async-supported> 标签。
description String 该过滤器的描述信息,等价于 <description> 标签。
displayName String 该过滤器的显示名,通常配合工具使用,等价于 <display-name> 标签。
largeIcon String 大图标
smallIcon String 小图标

 

 

 

 

 

 

 

 

 

 

 

 

 

 

4、跨域过滤器

package cn.ycx.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 跨域访问
 */
@WebFilter(filterName="/bcrossDomainFilter", urlPatterns="/*")
public class CrossDomainFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        String path = httpServletRequest.getServletPath();
        System.out.println("CrossDomainFilter->" + path);
        // 跨区请求 crossDomainFilter
        httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
        httpServletResponse.addHeader("Access-Control-Allow-Methods", "POST");
        httpServletResponse.addHeader("Access-Control-Allow-Methods", "GET");
        httpServletResponse.addHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
        chain.doFilter(request, response);
    }
}

 

posted @ 2020-10-10 14:52  翠微  阅读(186)  评论(0编辑  收藏  举报