public DsfUserTokenEntity createToken(long userId) {
String token = null;
DsfUserTokenEntity tokenEntity = this.dsfTokenRedis.getByUserId(userId);
if (tokenEntity != null && this.multiUser) {
token = tokenEntity.getToken();
} else {
token = TokenGenerator.generateValue();
}
Date now = new Date();
Date expireTime = new Date(now.getTime() + (long)(this.expire * 1000));
if (tokenEntity == null) {
tokenEntity = new DsfUserTokenEntity();
tokenEntity.setUserId(userId);
}
tokenEntity.setToken(token);
tokenEntity.setUpdateTime(now);
tokenEntity.setExpireTime(expireTime);
this.dsfTokenRedis.saveOrUpdate(tokenEntity);
return tokenEntity;
}
public DsfUserTokenEntity createToken(long userId, String token) {
DsfUserTokenEntity tokenEntity = this.dsfTokenRedis.getByUserId(userId);
Date now = new Date();
Date expireTime = new Date(now.getTime() + (long)(this.expire * 1000));
if (tokenEntity == null) {
tokenEntity = new DsfUserTokenEntity();
tokenEntity.setUserId(userId);
}
tokenEntity.setToken(token);
tokenEntity.setUpdateTime(now);
tokenEntity.setExpireTime(expireTime);
this.dsfTokenRedis.saveOrUpdate(tokenEntity);
return tokenEntity;
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.com.do1.dsf.component.shiro.oauth2;
import cn.com.do1.dsf.common.exception.RestException;
import java.security.MessageDigest;
import java.util.UUID;
public class TokenGenerator {
private static final char[] HEX_CODE = "0123456789abcdef".toCharArray();
public TokenGenerator() {
}
public static String generateValue() {
return generateValue(UUID.randomUUID().toString());
}
public static String toHexString(byte[] data) {
if (data == null) {
return null;
} else {
StringBuilder r = new StringBuilder(data.length * 2);
byte[] var2 = data;
int var3 = data.length;
for(int var4 = 0; var4 < var3; ++var4) {
byte b = var2[var4];
r.append(HEX_CODE[b >> 4 & 15]);
r.append(HEX_CODE[b & 15]);
}
return r.toString();
}
}
public static String generateValue(String param) {
try {
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(param.getBytes());
byte[] messageDigest = algorithm.digest();
return toHexString(messageDigest);
} catch (Exception var3) {
throw new RestException("生成Token失败", var3);
}
}
}
package cn.com.do1.dsf.redis;
import cn.com.do1.dsf.common.exception.RestException;
import cn.com.do1.dsf.component.redis.RedisKeys;
import cn.com.do1.dsf.component.redis.RedisUtil;
import cn.com.do1.dsf.modules.dsf.entity.DsfConfigEntity;
import cn.com.do1.dsf.modules.dsf.entity.DsfUserTokenEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Set;
/**
* @author carwen
* @create 2018/5/24 14:21
* @desc 系统管理-会话配置Redis
**/
@Component
public class DsfTokenRedis {
@Autowired
RedisUtil redisUtil;
public void saveOrUpdate(DsfUserTokenEntity config) {
if(config == null){
return ;
}
String tokenKey = RedisKeys.getTokenKey(config.getToken());
String userTokenKey = RedisKeys.getUserTokenKey(config.getUserId().toString());
//分别保存两个token:do1:token,do1:user-token,用于按token和按userId查找
redisUtil.set(tokenKey, config);
redisUtil.set(userTokenKey, config);
}
public void delete(String configKey) {
String tokenKey = RedisKeys.getTokenKey(configKey);
redisUtil.delete(tokenKey);
}
public void delete(Long userId, String token) {
String tokenKey = RedisKeys.getTokenKey(token);
String userTokenKey = RedisKeys.getUserTokenKey(userId.toString());
//同步删除两个token
redisUtil.delete(tokenKey);
redisUtil.delete(userTokenKey);
}
public DsfUserTokenEntity getByUserId(Long configKey){
String key = RedisKeys.getUserTokenKey(configKey.toString());
return redisUtil.get(key, DsfUserTokenEntity.class);
}
public DsfUserTokenEntity get(String configKey){
String key = RedisKeys.getTokenKey(configKey);
return redisUtil.get(key, DsfUserTokenEntity.class);
}
public void saveOrUpdate(DsfConfigEntity config) {
if(config == null){
return ;
}
String key = RedisKeys.getConfigKey(config.getConfigkey());
redisUtil.set(key, config);
}
public DsfConfigEntity getDsfconfig(String configKey){
String key = RedisKeys.getConfigKey(configKey);
return redisUtil.get(key, DsfConfigEntity.class);
}
}