SpringBoot 使用拦截器
引导类
继承 WebMvcConfigurer
接口,重写 addInterceptors
方法
@SpringBootApplication
public class ClassScheduleApplication implements WebMvcConfigurer {
public static void main(String[] args) {
SpringApplication.run(ClassScheduleApplication.class, args);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor());
}
}
拦截器类 Interceptor
继承 HandlerInterceptor
接口
public class LoginInterceptor implements HandlerInterceptor {
private static final String USER_CODE = "userCode";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String uri = request.getRequestURI();
Cookie[] cookies = request.getCookies();
String userCode = null;
for (Cookie cookie : cookies) {
if (USER_CODE.equals(cookie.getName())) {
userCode = cookie.getValue();
}
}
String[] uris = {"/Login.html", "/Register.html", "/users/register", "/users/login"};
boolean flag = true;
for (String s : uris) {
if (s.equals(uri)) {
flag = false;
break;
}
}
if (flag) {
if (request.getSession().getAttribute(USER_CODE) != null && request.getSession().getAttribute(USER_CODE).equals(userCode)) {
return true;
}
response.sendRedirect("/Login.html");
} else {
return true;
}
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}