SpringBoot笔记:拦截器Interceptor和过滤器Filter
一、拦截器Interceptor
SpringBoot中定义拦截器与MVC中是一样的,区别在于拦截器的配置,MVC是配置在配置文件中的,SpringBoot中则是配置在配置类中的。(SpringBoot中的配置类需要使用注解@Configuration声明)
拦截器定义(与MVC中一致):
package com.yun.demo.interceptor;
import com.yun.demo.model.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
// 拦截器定义,实现HandlerInterceptor接口并重写对应方法即可,与MVC中是一样的
public class UserInterceptor implements HandlerInterceptor {
// 请求到达controller之前进行拦截并处理
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 拦截请求,如果当前session中没有user属性值,则跳转到/error页面
User user = (User) request.getSession().getAttribute("user");
if (Objects.isNull(user)) {
response.sendRedirect(request.getContextPath() + "/error");
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
在配置类中添加拦截器:
package com.yun.demo.config;
import com.yun.demo.interceptor.UserInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
// 使用@Configuration表明这是一个Spring配置类
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
String[] addPathPatterns = {
"/user/**"
};
String[] excludePathPatterns = {
"/user/list",
"/user/login",
"/error"
};
// 注册拦截器,可以指定拦截规则,addPathPatterns方法定义要拦截的请求,excludePathPatterns方法定义不需要拦截的请求
registry.addInterceptor(new UserInterceptor()).addPathPatterns(addPathPatterns)
.excludePathPatterns(excludePathPatterns);
}
}
二、过滤器Filter
通过实现Filter接口可以定义一个过滤器,在SpringBoot中注册过滤器有两种方式,一种是通过注解@WebFilter,另一种是通过配置类注册对应的过滤器bean。
方式一:通过注解@WebFilter
定义并注册过滤器,并通过注解@ServletComponentScan指定需要扫描的过滤器包路径。
过滤器:
package com.yun.demo.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
// 定义一个过滤器(实现javax.servlet.Filter接口),
// 然后使用注解@WebFilter进行注册,并指定需要过滤的url
@WebFilter(urlPatterns = "/filter")
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Hello filter");
filterChain.doFilter(servletRequest, servletResponse);
}
}
在启动类中指定扫描路径:
package com.yun.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
// 使用注解@ServletComponentScan指定要扫描的过滤器Filter包
@ServletComponentScan(basePackages = "com.yun.demo.filter")
public class Demo016Servlet001Application {
public static void main(String[] args) {
SpringApplication.run(Demo016Servlet001Application.class, args);
}
}
方式二:通过配置类注册对应的过滤器bean
这种方式也需要定义一个Filter,只不过不需要使用注解 @WebFilter
了,只需要将定义的Filter在配置类中注册即可。
过滤器:
package com.yun.demo.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
// 定义过滤器
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Hello filter");
filterChain.doFilter(servletRequest, servletResponse);
}
}
配置过滤器:
package com.yun.demo.config;
import com.yun.demo.filter.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// 使用注解@Configuration定义配置类
@Configuration
public class FilterConfig {
// 使用注解@Bean定义并注册一个bean
@Bean
public FilterRegistrationBean myFilterRegistrationBean() {
// 定义bean并注册一个Filter
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter());
// 指定请求路径,注意这里的不能写/**,只能是一个星号
filterRegistrationBean.addUrlPatterns("/filter/*");
return filterRegistrationBean;
}
}