spring boot 配置拦截器


           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 {
//重写方法
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM