顾名思义,过滤器,就是用来过滤web请求的,所以关注点是web请求,
1)过滤器可以过滤所有的web 请求,包括静态页面
2)过滤器基于servlet规定的,所以依赖于web容器,所以只能过滤web请求
一般用来过滤敏感词、URL级别的访问权限控制等
springboot中实现
添加controller
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="/test")
public class HelloController {
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
return "hello!";
}
}
添加过滤器
import javax.servlet.*;
import java.io.IOException;
public class FilterDemoClass 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 {
System.out.println("==========过滤器开始执行==========");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("==========过滤器结束执行==========");
}
@Override
public void destroy() {
System.out.println("==========过滤器销毁==========");
}
}
对过滤器进行注册
import com.xyzh.filterdemo.filter.FilterDemoClass;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class FilterDemoApplication {
public static void main(String[] args) {
SpringApplication.run(FilterDemoApplication.class, args);
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new FilterDemoClass()); // 自己的filter
List urlPatterns = new ArrayList<>();
urlPatterns.add("/test/*");
registrationBean.setUrlPatterns(urlPatterns);
return registrationBean;
}
}
在过滤器类的上面加上注解:
@WebFilter(filterName = "suFilter",urlPatterns = "/test/*")
@Configuration
可以去掉启动类中的 bean的部分。
1)可以认为拦截器只能拦截action请求,不拦截静态页面等,
2)拦截器基于反射机制实现,所以不依赖于web容器
3)拦截器可以获取action上下文的ModelAndView对象的值
用于处理action,比如处理日志等
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j
@Component
public class MyInterceptor implements HandlerInterceptor {
private final static Logger logger= LoggerFactory.getLogger(MyInterceptor.class);
//预处理回调方法
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler)
throws Exception{
System.out.println("拦截器开始拦截....");
return false;
}
//处理逻辑,在controller后调用
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("拦截器工作中....");
}
//处理完成后处理逻辑
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("拦截器已经结束....");
}
}
import com.xyzh.filterdemo.filter.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {
//这个方法是用来配置拦截器的
@Autowired
private MyInterceptor interceptor;
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(interceptor).addPathPatterns("/**").excludePathPatterns("/login,/register");
}
}
启动后看下控制台输出:
==========过滤器开始执行==========
拦截器开始拦截....
==========过滤器结束执行==========
监听器实际上是一个特殊的servlet。他的作用很单一,就是监听servletContext、HttpSession、ServeltRequest等对象的的创建或者销毁以及属性值发生改变的事件
一般用于统计在线人数、访问量、埋点(用户访问路径)等
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class MyListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
System.out.println("监听器销毁请求");
}
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
System.out.println("监听器初始化请求");
}
}
@ServletComponentScan(basePackages = "com.hkjs.filterdemo.*")