首页 > 编程语言 > 详细

spring boot 配置拦截器 interceptor

时间:2020-12-10 17:41:57      阅读:30      评论:0      收藏:0      [点我收藏+]

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");

    }
}

  

 

spring boot 配置拦截器 interceptor

原文:https://www.cnblogs.com/blogxiao/p/14115775.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!