当前位置:主页 > 查看内容

SpringBoot中的拦截器Interceptor(随笔)

发布时间:2021-06-03 00:00| 位朋友查看

简介:SpringBoot中的拦截器Interceptor随笔 1定义拦截器定义一个拦截器类实现HandlerInterceptor接口重写preHangle()方法 public class OneInterceptor implements HandlerInterceptor { Override public boolean preHandle ( HttpServletRequest request , HttpS……

SpringBoot中的拦截器Interceptor(随笔)

1)定义拦截器:定义一个拦截器类实现HandlerInterceptor接口,重写preHangle()方法;

public class OneInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获得拦截的uri并输出
        System.out.println("这里经过了拦截器"+request.getRequestURI());
        return true;
    }
}

2)注册拦截器:定义一个类继承自WebMvcConfigurationSupport,重写addInterceptors()方法,并在该类上使用@Configuration注解,表明该类为配置类。

@Configuration   //表明当前类为配置类
public class OneInterceptorConfig extends WebMvcConfigurationSupport {

    //注册拦截器
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new OneInterceptor())
           // .addPathPatterns("/rjxy/**");  //拦截/rjxy相关的uri请求
            .excludePathPatterns("/rjxy/**"); //指定不进行拦截的uri,但拦截其他uri请求
    }
}
;原文链接:https://blog.csdn.net/guo_jun_123/article/details/115559807
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐