spring boot 配置拦截器要写一个配置类和一个拦截器类。在配置类里面将拦截器注册到spring boot 项目里,在拦截器里面实现拦截处理的方法。
先看配置类的代码
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * 后台管理配置类 */ /** * 后台管理配置类 */ @Configuration public class AdminConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //注册TestInterceptor拦截器 InterceptorRegistration registration = registry.addInterceptor(adminInterceptor()); registration.addPathPatterns("/sys/**"); //所有sys路径都被拦截 // registration.excludePathPatterns( //添加不拦截路径 // "你的登陆路径", //登录 // "/**/*.html", //html静态资源 // "/**/*.js", //js静态资源 // "/**/*.css", //css静态资源 // "/**/*.woff", // "/**/*.ttf" // ); }@Bean //将自定义拦截器注册到spring bean中
public AdminInterceptor adminInterceptor(){
return new AdminInterceptor();
}
}
拦截器类
public class AdminInterceptor implements HandlerInterceptor {
//重写方法
}