content: package com.wenmrong.community1.community.interceptor;
  
  import com.wenmrong.community1.community.exception.CustomizeErrorCode;
  import com.wenmrong.community1.community.exception.CustomizeException;
  import com.wenmrong.community1.community.model.User;
  import org.springframework.stereotype.Service;
  import org.springframework.web.servlet.HandlerInterceptor;
  import org.springframework.web.servlet.ModelAndView;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  @Service
  public class LoginInterceptor implements HandlerInterceptor {
      @Override
      public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
          //未登录进行拦截，防止回退历史操作
          User user = (User) request.getSession().getAttribute("user");
          if (user == null) {
              throw new CustomizeException(CustomizeErrorCode.NO_LOGIN);
          }
          return true;
      }
  
      @Override
      public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
  
      }
  
      @Override
      public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
  
      }
  }
  