搞一个包 --- 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);
}
}
效果
网友评论