Spring Boot使用-SpringMVC拦截器
可以在Spring Boot项目中配置自定义SpringMVC拦截器
- 编写拦截器(实现HandlerInterceptor);
- 编写配置类实现 WebMvcConfigurer,在该类中添加各种组件;
具体实现:
import com.myself.interceptor.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
//注册拦截器
@Bean
public MyInterceptor myInterceptor(){
return new MyInterceptor();
}
//添加拦截器到spring mvc拦截器链
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor()).addPathPatterns("/*");
}
}