RedisTokenManager.java 3.05 KB
package com.xly.token;//package com.xly.token;
//
//import cn.hutool.core.util.ObjectUtil;
//import cn.hutool.core.util.StrUtil;
//import cn.hutool.json.JSONObject;
//import cn.hutool.json.JSONUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//
///**
// * 通过Redis存储和验证token的实现类
// * @author qianbao
// * @see TokenManager
// */
//@Component
//public class RedisTokenManager{
//
//    private @Autowired
//    JedisUtil jedisUtil;
//
//    public TokenModel createToken(String userId, String sBrandsId, String sSubsidiaryId, String userType , UserInfo gdslogininfo,String sLoginType) {
//        //使用uuid作为源token
//        String token = new IdGen().getNextId();
//        TokenModel model = new TokenModel(userId, sBrandsId, sSubsidiaryId, token,userType,sLoginType,gdslogininfo);
//        //存储到redis并设置过期时间
//        jedisUtil.createAndExpireMinutes(sLoginType+userId, token, Constants.TOKEN_EXPIRES_MINUTES);
//        return model;
//    }
//
//    @Override
//    public TokenModel getToken(String authentication) {
//        if(StringUtils.isEmpty(authentication)){
//            return null;
//        }else{
//            authentication= AesUtil.me().decrypt(authentication,AesUtil.me().getKey());
//            if (authentication == null || authentication.length() == 0) {
//                return null;
//            }
//            String[] param = authentication.split("_");
//            if (param.length != 6) {
//                return null;
//            }
//            //使用userId,sBrandsId,sSubsidiaryId,源token简单拼接成的token,可以增加加密措施
//            String userId = param[0];
//            String sBrandsId = param[1];
//            String sSubsidiaryId = param[2];
//            String token = param[3];
//            String userType = param[4];
//            String sLoginType = param[5];
//            UserInfo gdslogininfo= null;
//            Object v = jedisUtil.getObject(userId+"_"+sBrandsId+"_"+sSubsidiaryId);
//            if(ObjectUtil.isNotEmpty(v)){
//                gdslogininfo = JSONUtil.toBean((JSONObject) v,UserInfo.class);
//            }
//            return new TokenModel(userId, sBrandsId, sSubsidiaryId, token,userType,sLoginType,gdslogininfo);
//        }
//    }
//
//    @Override
//    public int checkToken(TokenModel model) {
//        String token = String.valueOf(jedisUtil.get(model.getsLoginType()+model.getUserId()));
//        if (StrUtil.isNullOrUndefined(token)) {
//            return 1;
//        }
//        if(!token.equals(model.getToken())) {
//            return 2;
//        }
//        //如果验证成功,说明此用户进行了一次有效操作,延长token的过期时间
//        jedisUtil.expireMinutes(model.getsLoginType()+model.getUserId(),Constants.TOKEN_EXPIRES_MINUTES);
//        return 0;
//    }
//
//    @Override
//    public void deleteToken(String userId,String sLoginType) {
//        jedisUtil.del(sLoginType+userId);
//    }
//}