| package com.matrix.component.redis; | 
|   | 
| import com.google.gson.Gson; | 
| import com.matrix.core.constance.SystemErrorCode; | 
| import com.matrix.core.exception.GlobleException; | 
| import com.matrix.core.tools.LogUtil; | 
| import com.matrix.core.tools.StringUtils; | 
| import com.matrix.core.tools.UUIDUtil; | 
| import com.matrix.core.tools.WebUtil; | 
| import com.matrix.system.hive.plugin.message.StringUtil; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.stereotype.Component; | 
|   | 
|   | 
| /** | 
|  * 用户登录信息保存在redis上 | 
|  * | 
|  * @author JIANGYOUYAO | 
|  * @email 935090232@qq.com | 
|  * @date 2018年1月19日 | 
|  */ | 
| @Component | 
| public class RedisUserLoginUtils { | 
|   | 
|     @Autowired | 
|     RedisClient redisClient; | 
|     private static final String TOKEN = "token"; | 
|   | 
|     @Value("${debug}") | 
|     private String isDebug; | 
|   | 
|     /** | 
|      * 判断用户是否已经登录过 | 
|      * | 
|      * @author JIANGYOUYAO | 
|      * @email 935090232@qq.com | 
|      * @date 2018年1月19日 | 
|      *            会从http请求投中获取token进行验证 | 
|      * @return | 
|      * | 
|      */ | 
|     public boolean isUserLogin() { | 
|         String token = getUserToken(); | 
|   | 
|         if (StringUtils.isNotBlank(token)) { | 
|             String user = redisClient.getCachedValue(token); | 
|             LogUtil.debug("redis 获取到token={},用户={}", token, user); | 
|             return user != null; | 
|         } else { | 
|             LogUtil.debug("redis token未获取到token"); | 
|             return false; | 
|         } | 
|     } | 
|   | 
|   | 
|   | 
|   | 
|     public String getUserToken() { | 
|         String token = WebUtil.getRequest().getHeader("token"); | 
|         // debug模式可以从url参数中获取token | 
|         if (StringUtils.isBlank(token) && "true".equals(isDebug)) { | 
|             if (token == null) { | 
|                 token = WebUtil.getRequest().getParameter("token"); | 
|             } | 
|         } | 
|         return token; | 
|     } | 
|   | 
|     /** | 
|      * 获取登录用户信息,如果 获取失败将会抛出异常 | 
|      * | 
|      * @author JIANGYOUYAO | 
|      * @email 935090232@qq.com | 
|      * @date 2018年1月19日 | 
|      * @return | 
|      */ | 
|     public <T> T getLoginUser(Class<T> clazz) { | 
|         String userToken = getUserToken(); | 
|         LogUtil.debug("redisClient获取到用户token={}", userToken); | 
|         if (userToken != null) { | 
|             String json = redisClient.getCachedValue(userToken); | 
|             LogUtil.info("从redis中获取的值为:"+json); | 
|             if(StringUtils.isNotBlank(json)){ | 
|                 //重新设置key过期时间 | 
|                 redisClient.resetExpire(userToken); | 
|                 Gson g = new Gson(); | 
|                 return g.fromJson(json, clazz); | 
|             }else{ | 
|                 throw new GlobleException(SystemErrorCode.REQUEST_INVALID); | 
|             } | 
|   | 
|         } else { | 
|             throw new GlobleException(SystemErrorCode.REQUEST_INVALID); | 
|         } | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 在redis中保存用户信息,并且返回保存的token | 
|      * | 
|      * @author JIANGYOUYAO | 
|      * @email 935090232@qq.com | 
|      * @date 2018年1月19日 | 
|      * @return 返回保存用户信息的token | 
|      */ | 
|     public String saveUserInfo(Object obj) { | 
|         String token = UUIDUtil.getRandomID(); | 
|   | 
|         redisClient.saveValue(token, obj); | 
|         LogUtil.debug("redis 保存用户返回token={}", token); | 
|         return token; | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 用户退出 | 
|      * | 
|      * @author:吕敬瑛 | 
|      * @date:2018年1月19日下午3:17:14 | 
|      */ | 
|     public void loginOut() { | 
|         String toke = getUserToken(); | 
|         redisClient.removeObject(toke); | 
|     } | 
|   | 
|   | 
|   | 
| } |