String token = (String) session.getAttribute("user_token"); //取出会话 数据
if (token != null) {
AliveModel aliveModel = tokenRedis.checkTokenActive(token);
/**
* 判断 token 是否有效
*/
public AliveModel checkTokenActive(String token){
// 先判断 key 是否存在
boolean tokenIsExist = stringRedisTemplate.boundHashOps("roulette_alive").hasKey(token);
//如果 key 存在 检查是否过期
if(tokenIsExist){
//检查当前key 有效时间
String jsonActive = (String)stringRedisTemplate.opsForHash().get("roulette_alive", token);
AliveModel aliveModel = JSON.parseObject(jsonActive, AliveModel.class);
Date time = aliveModel.getTime();
long aliveTime = (new Date().getTime() - time.getTime())/1000;
//超时时间
if(aliveTime>15000){
stringRedisTemplate.opsForHash().delete("roulette_alive",token);//超时删除redis
return null;
}
return aliveModel;
/* if(expire>0){
String jsonActive = (String)stringRedisTemplate.opsForHash().get("roulette_alive", token);
AliveModel aliveModel = JSON.parseObject(jsonActive, AliveModel.class);
return aliveModel;
}*/
}
return null;
}