SpringBoot 之Filter 常用配置方式解析

符号 阅读:258 2021-03-31 17:00:59 评论:0

SpringBoot 配置Filter过滤器方式一:

1、通过 @WebFilter 注解来配置

建立UrlFilter类,此例我们模拟判断用户请求地址是否有误:UrlFilter代码如下

@WebFilter(filterName = "urlFilter", urlPatterns = "/*") 
public class UrlFilter implements Filter { 
  
    @Override 
    public void init(FilterConfig filterConfig) throws ServletException { 
  
        System.out.println("----------------------->过滤器被创建"); 
    } 
  
    @Override 
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
  
        HttpServletRequest req = (HttpServletRequest) servletRequest; 
        String requestURI = req.getRequestURI(); 
        System.out.println("--------------------->过滤器:请求地址"+requestURI); 
        if(!requestURI.contains("info")){ 
            servletRequest.getRequestDispatcher("/failed").forward(servletRequest, servletResponse); 
        }else{ 
            filterChain.doFilter(servletRequest, servletResponse); 
        } 
    } 
  
    @Override 
    public void destroy() { 
  
        System.out.println("----------------------->过滤器被销毁"); 
    } 
}

注意:@WebFilter注解为声明此类为filter,第一个参数为该filter起一个名字,第二个参数为说明要拦截的请求地址。该类实现了Filter接口,里面有三个方法,分别为过滤器创建、过滤方法和过滤器销毁。

main函数类上添加@ServletComponentScan(basePackages = "此处写明Filter地址,格式为包名+类名(如com.digipowert.ucas.filter)")

package com.digipower; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.servlet.ServletComponentScan; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
 
@SpringBootApplication 
@ServletComponentScan("com.digipowert.ucas.filter") 
public class Application extends SpringBootServletInitializer { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		 SpringApplication.run(Application.class, args); 
	} 
	 
	@Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
        // 注意这里要指向原先用main方法执行的Application启动类 
        return builder.sources(Application.class); 
    } 
} 

2、通过 @Bean 注解来配置

public class UrlFilter implements Filter { 
  
    @Override 
    public void init(FilterConfig filterConfig) throws ServletException { 
  
        System.out.println("----------------------->过滤器被创建"); 
    } 
  
    @Override 
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
  
        HttpServletRequest req = (HttpServletRequest) servletRequest; 
        String requestURI = req.getRequestURI(); 
        System.out.println("--------------------->过滤器:请求地址"+requestURI); 
        if(!requestURI.contains("info")){ 
            servletRequest.getRequestDispatcher("/failed").forward(servletRequest, servletResponse); 
        }else{ 
            filterChain.doFilter(servletRequest, servletResponse); 
        } 
    } 
  
    @Override 
    public void destroy() { 
  
        System.out.println("----------------------->过滤器被销毁"); 
    } 
}

在 @SpringBootApplication 注解的类中,添加自定义Filter。

package com.digipower; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.servlet.FilterRegistrationBean; 
import org.springframework.boot.web.servlet.ServletComponentScan; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.context.annotation.Bean; 
 
@SpringBootApplication 
public class Application extends SpringBootServletInitializer { 
 
	public static void main(String[] args) { 
		// TODO Auto-generated method stub 
		 SpringApplication.run(Application.class, args); 
	} 
	 
	@Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
        // 注意这里要指向原先用main方法执行的Application启动类 
        return builder.sources(Application.class); 
    } 
	 
	@Bean 
	public FilterRegistrationBean urlFilterRegistration() { 
		FilterRegistrationBean registration = new FilterRegistrationBean(new UrlFilter()); 
		registration.addUrlPatterns("/*"); //url 拦截路径 
		registration.setName("urlFilter"); // 拦截器名称 
		registration.setOrder(1);   //启动序号 
		return registration; 
	} 
 
}

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

关注我们