Springboot 注册拦截器
Posted Draymonder
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot 注册拦截器相关的知识,希望对你有一定的参考价值。
拦截器
创建myInterceptor类
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class MyInterceptor1 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("MyInterceptor >>> preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("MyInterceptor >>> postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) throws Exception {
System.out.println("MyInterceptor >>> afterHandle");
}
}
配置拦截器
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/**")
.excludePathPatterns("/hello");
}
}
以上是关于Springboot 注册拦截器的主要内容,如果未能解决你的问题,请参考以下文章
嘿从零开始基于SpringBoot 打造在线聊天室(4.4W字最长博文)