一、过滤器
@Component public class MyFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { String cookie = response.getHeader("Set-Cookie"); System.out.println("+++++++++++++++自定义过滤器++++++++++++++++"); System.out.println(cookie); System.out.println("+++++++++++++++自定义过滤器++++++++++++++++"); filterChain.doFilter(request, response); } }
二、拦截器
1.拦截器定义
@Component public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String cookie = response.getHeader("Set-Cookie"); System.out.println("*************自定义拦截器**************"); System.out.println(cookie); System.out.println("*************自定义拦截器**************"); return true; } }
2.注册拦截器
@Configuration public class RegisterInterceptorConfig implements WebMvcConfigurer { @Autowired private MyInterceptor myInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { InterceptorRegistration registration = registry.addInterceptor(myInterceptor); registration.addPathPatterns("/**"); } }