redis+token实现登录状态

                               redis+token实现登录状态
   /*
    * 登录后存储用户对象的redis函数
    */
    @RequestMapping(value = "/loginRedis",method=RequestMethod.POST)
    public AppResult loginRedis(UserInfo userInfo){
    
    
        String uuid = Utils.getUUID();
        String value = JSON.toJSONString(userInfo);
        stringRedisTemplate.opsForValue().set(uuid,value);
        //30分钟后自动退出
        stringRedisTemplate.expire(uuid,30, TimeUnit.MINUTES);
        return  AppResult.successData("token",uuid);

    }

    /*
     * 调用接口的时候的登录状态的授权
     */
    @RequestMapping(value = "/isLogin",method=RequestMethod.GET)
    @ResponseBody
    public UserInfo isLogin(@RequestParam("token") String token){
    
    
        String userInfoStr = stringRedisTemplate.opsForValue().get(token).toString();
        if (userInfoStr==""||userInfoStr==null){
    
    
            return null;
        }else {
    
    
            //刷新redis的值30分钟
            stringRedisTemplate.expire(token,30, TimeUnit.MINUTES);
           //返回登录的用户对象
            UserInfo userInfo =  JSON.parseObject(userInfoStr,UserInfo.class);
           return  userInfo;
        }
    }

猜你喜欢

转载自blog.csdn.net/m0_52936310/article/details/112971412