一、声明一个拦截器实现HandlerInterceptor,并实现它的preHandle()方法
@Component
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//可以try{}catch()进行业务处理
try{
//业务逻辑
return true; //正确返回,放行
}catch(){
//业务逻辑
return false; //错误返回,拦截
}
}
二、写一个Config去注册这个拦截器
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private LoginInterceptor loginInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//可设置哪些接口不拦截 excludePathPatterns();
registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login","/user/register");
}
}
标签:拦截器,loginInterceptor,使用,Override,preHandle,Interceptor,public,HandlerInterceptor
From: https://www.cnblogs.com/kxqblogs/p/17861578.html