@RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView loginProcess(Student student, HttpServletRequest request, HttpServletResponse response) {        ModelAndView modelAndView = new ModelAndView();        String salt = student.getUsername();
        String password = student.getPassword();
        String password1 = MD5Util.getMd5withSalt(password, salt);        student.setUsername(salt);
        logger.info(salt);
        student.setPassword(password1);
        logger.info(password1);
        Student s1 = studentDao.login(student);        if (s1 != null) {
            modelAndView = new ModelAndView("welcome");
            modelAndView.addObject("student",s1);
            String str = s1.getId() + ":" + System.currentTimeMillis();
            logger.info("str==================" + str);            String token = DESUtil.encrypt(str,DESUtil.getkey());
            logger.info("token===========================" + token);
            response.addCookie(CookieUtil.getLoginCookie("token",token));            String token1= DESUtil.decrypt(token,DESUtil.getkey());
            logger.info("token1=============="+token1);        } else {
            modelAndView = new ModelAndView("redirect:login");
            logger.info("帐号或者密码不存在");
        }        return modelAndView;
    }
上面的代码中,我的登录验证正常,生成token也正常,然后我使用response.addCookie(CookieUtil.getLoginCookie("token",token));把token 放入了cookie ,请问 该怎么从拦截器类里面 实现自动登录的功能,就是在
 拦截器里通过Cookie中判断Token的有效性来判断用户是否登录。 该怎么做 谢谢大家

解决方案 »

  1.   

    你的意思是登陆以后,在访问其他页面要校验一下是否登陆过了。
    这个一般都是把登陆信息放到session中,然后通过filter校验session中是否存在登陆信息。
    当然把登陆信息放到cookie中也是可以的。
      

  2.   

    你这不知道了嘛 就拦截器获取token啊
      

  3.   

    首先你得把token存起来
    其次token跟你的用户映射(同时一个终端登录,那就存用户表,同时多个终端登录,再建个token表跟用户表关联,这个应该清楚吧)
    最后过滤器拿到cookie中的token,去数据库中验证
    想快一点就用缓存
    别的细节就不多说,取决于你的需求