springBoot 2.X-自定义拦截器

package com.cx.springboot.myInter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * @作者 陈先生
 * @创建时间 2018年7月13日
 * @功能描述 自定义拦截器
 */
@Configuration
public class MyInterceptor implements HandlerInterceptor {

    /**
     *  在请求处理之前进行调用(Controller方法调用之前)
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // 进行逻辑判断,如果ok就返回true,不行就返回false,返回false就不会处理改请求
        
        String name = request.getParameter("name");
        System.err.println(name +"-拦截器");
        return true;
    }

    /**
     * 请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        //
    }
     /**
     * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        
    }

}

1) 创建类 并实现 HandlerInterceptor 接口, 重写接口三个方法,具体方法执行时机见代码注释

package com.cx.springboot.myInter;

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.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 
 * @作者 陈先生
 * @创建时间 2018年7月13日
 * @功能描述 拦截器配置
 */
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
    // 以下WebMvcConfigurerAdapter 比较常用的重写接口
    // /** 解决跨域问题 **/
    // public void addCorsMappings(CorsRegistry registry) ;
    // /** 添加拦截器 **/
    // void addInterceptors(InterceptorRegistry registry);
    // /** 这里配置视图解析器 **/
    // void configureViewResolvers(ViewResolverRegistry registry);
    // /** 配置内容裁决的一些选项 **/
    // void configureContentNegotiation(ContentNegotiationConfigurer
    // configurer);
    // /** 视图跳转控制器 **/
    // void addViewControllers(ViewControllerRegistry registry);
    // /** 静态资源处理 **/
    // void addResourceHandlers(ResourceHandlerRegistry registry);
    // /** 默认静态资源处理器 **/
    // void configureDefaultServletHandling(DefaultServletHandlerConfigurer
    // configurer);

    @Autowired
    private MyInterceptor myInterceptor;

    /**
     * 表示这些配置的表示静态文件所处路径, 不用拦截
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**")
                .addResourceLocations("classpath:/templates/");
        super.addResourceHandlers(registry);
    }

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                // addPathPatterns 用于添加拦截规则 , 先把所有路径都加入拦截, 再一个个排除
                .addPathPatterns("/**")
                // excludePathPatterns 表示改路径不用拦截
                .excludePathPatterns("/");
        super.addInterceptors(registry);
    }
}

2)配置拦截器 , 一般具体配置参数使用配置文件的方式配置

原文地址:https://www.cnblogs.com/cx987514451/p/9305231.html