SpringBoot配置过滤器、监听器和拦截器

一:配置过滤器

1.创建TestFilter类并实现Filter接口

@WebFilter(filterName="testFilter",urlPatterns="/*")  //@WebFilter是定义过滤器的注解 ,urlPatterns="/*" 定义过滤器过滤的路径

public class TestFilter implements Filter{

	@Override
	public void destroy() {
		System.out.println("过滤器被销毁");
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException {
		System.out.println("过滤器被执行");
		
		arg2.doFilter(arg0, arg1);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("过滤器被初始化");
	}

}

2.需要在启动类中添加@ServletComponentScan注解@WebFilter注解方可生效

@ServletComponentScan(basePackages = {"www.zxf.com.filter","www.zxf.com.listener"})
@EnableAutoConfiguration
public class App {

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

 

二:配置监听器

1.创建TestListener类并实现ServletContextListener接口

@WebListener  //将该类定义为一个监听器
public class TestListener implements ServletContextListener{

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("监听器被销毁");
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("监听器被创建");
	}

}

2.需要在启动类中添加@ServletComponentScan注解@WebListener注解方可生效

@ServletComponentScan(basePackages = {"www.zxf.com.filter","www.zxf.com.listener"})
@EnableAutoConfiguration
public class App {

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

三:配置拦截器

1.创建拦截器一

package www.zxf.com.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class IntercepterTest1 implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("请求执行后1");
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println("请求执行后视图渲染前1");
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		System.out.println("请求执行之前1");
		return true;
	}

}

2.创建拦截器二

package www.zxf.com.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class IntercepterTest2 implements HandlerInterceptor{

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("请求执行后2");
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println("请求执行后视图渲染前2");
	}

	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		System.out.println("请求执行之前2");
		return true;
	}

}

3.配置拦截器并将拦截器一和拦截器二加入该配置中

package www.zxf.com.intercepter;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebAdepter extends WebMvcConfigurerAdapter{

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new IntercepterTest1()).addPathPatterns("/**");//配置拦截路径
		registry.addInterceptor(new IntercepterTest2()).addPathPatterns("/**");//配置拦截路径
		super.addInterceptors(registry);
	}
	
}

 

 

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Netocc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值