spring boot 配置拦截器 interceptor

1.创建一个类继承 HandlerInterceptor

 

public class CardOrderInterceptor implements HandlerInterceptor {

    private static Logger logger= LoggerFactory.getLogger(CardOrderInterceptor.class);

    private static final String CARD_PREFIX="card_";

    static final String CARD_REPEAT_ID="card_repeat_id";

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    static final int EXPIRE_TIME=60;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       //TODO 处理前置拦截
    }

    @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 {

    }

 2.创建一个类配置存放注册的拦截器

 

@Configuration
public class InterceptorConfig implements WebMvcConfigurer{


   /**
     * 注册拦截器
     *
     * @param registry 注册器
     * @author coderxiao
     * @date 
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自定义拦截器 拦截基础请求
        registry.addInterceptor(new InterceptorConfig()).addPathPatterns("/**")
                .excludePathPatterns("/index.html/**")
                .excludePathPatterns("/css/**")
                .excludePathPatterns("/js/**");

        //拦截会员卡重复订单 
        registry.addInterceptor(new CardOrderInterceptor()).addPathPatterns("/**/cardApi/createOrder");

    }
}

  

 

posted @ 2020-12-10 16:24  zero_and_one  阅读(360)  评论(0编辑  收藏  举报