【设置了拦截器时候,我的账号传不进数据库 】 | IT修真院·坑乎
问题已收录 设置了拦截器时候,我的账号传不进数据库
我也踩过这个坑( 1 )
已统计您的踩坑,无需重复点击
回答(1)
详细描述
错误截图
源码
编辑于2024-11-24
  • [成都|荣耀师兄]JAVA-先小波
    0

    prehandle要return true,不然是不会进行后面的操作的。下面是我的拦截器的例子:

    @Component
    public class LoginInterceptor implements HandlerInterceptor {
       private static Logger log = Logger.getLogger(LoginInterceptor.class);
       @Autowired
       private CookieUtil cookieUtil;
       @Autowired
       private JwtUtil jwtUtil;
       @Override
       public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
           String JwtToken = cookieUtil.getCookie(request);
           if(JwtToken == null) {
               response.sendRedirect("/a/login");
               return  false;
           }
           Integer userId = jwtUtil.getUserId(JwtToken);
           if(userId == null){
               response.sendRedirect("/a/login");
               return  false;
           }
           return  true;
       }
       @Override
       public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception{
    //        System.out.println("postHandle");
       }
       @Override
       public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    //        System.out.println("afterCompletion");
       }

    }


    编辑于2018-12-03