SpringBoot--实现拦截器
2019-10-22 本文已影响0人
磨陀货_
搞一个包 --- interceptor 定义拦截器
public class LoginCheckInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("拦截器被执行了....:"+request.getRequestURI());
return super.preHandle(request, response, handler);
}
}
在搞一个 --- config --- 覆写.配置给Spring
@Configuration
public class WebConfig implements WebMvcConfigurer{
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginCheckInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/login");
}
@Bean
public LoginCheckInterceptor loginCheckInterceptor(){
return new LoginCheckInterceptor();
}
}
启动
@SpringBootApplication
@Import(WebConfig.class)
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class,args);
}
}
效果
![](https://img.haomeiwen.com/i17114228/6446518fd26aa071.png)