• 周五. 8月 19th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

springboot过滤器和拦截器

admin

11月 28, 2021

 一、过滤器

@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("/**");
    }
}

发表回复

您的电子邮箱地址不会被公开。