springboot配置拦截器和资源映射
程序员文章站
2022-06-02 15:14:36
...
- 创建class类实现org.springframework.web.servlet.HandlerInterceptor
@Slf4j//lombak方便打日志
@Component//注册到spring容器中方便注入
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("前置拦截");
return true;//根据布尔值设置拦截和放行
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info("后置拦截");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("最终拦截");
}
}
- 创建配置类实现org.springframework.boot.autoconfigure.web.servlet.WebMvcConfigurer 接口
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Autowired
private MyInterceptor myInterceptor;//注入拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
//配置拦截器,并且设置拦截路径
registry.addInterceptor(myInterceptor).addPathPatterns("/**");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//设置资源映射 location 用 file://,window用/E:/ww表示资源路径
registry.addResourceHandler("/files/**")
.addResourceLocations("file:///E:/ww/demos/source/");
}
}
拦截器和资源映射的最终效果
上一篇: Pandoc 实用教程
推荐阅读
-
springboot多模块多环境配置文件问题(动态配置生产和开发环境)
-
springboot配置tomcat配置乱码(配置乱码原因和对应解决法)
-
springboot配置tomcat配置乱码(配置乱码原因和对应解决法)
-
apache虚拟机配置步骤和修改访问端口的方法(虚拟机端口映射)
-
SpringBoot静态资源css,js,img配置方案
-
ThinkPHP中公共函数路径和配置项路径的映射分析
-
SpringBoot起飞系列-拦截器和统一错误处理(七)
-
三、解决Spring MVC拦截器导致静态资源访问失败(基于java注解配置)
-
1-4. SpringBoot配置自定义拦截器,实现日志管理
-
spring boot加载资源路径配置和classpath问题解决