SpringBoot配置拦截器,指定拦截或放行的请求路径
配置类:@Configurationpublic class AppConfig implements WebMvcConfigurer {@Beanpublic MyInterceptor myInterceptor() {return new MyInterceptor();}/*** 注册自定义拦截器* 指定要拦截的路径* /api/blog/** 拦截请求前缀为api/blog的请求,其他
·
配置类:
@Configuration
public class AppConfig implements WebMvcConfigurer {
@Bean
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}
/**
* 注册自定义拦截器
* 指定要拦截的路径
* /api/blog/** 拦截请求前缀为api/blog的请求,其他请求放行
* /user/** 放行请求的路径
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor()).addPathPatterns("/api/blog/**").excludePathPatterns("/user/**");
}
}
拦截器:
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
更多推荐
已为社区贡献3条内容
所有评论(0)