|
|
|
@ -9,6 +9,7 @@ import com.hxhq.common.security.utils.SecurityUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import com.hxhq.common.core.constant.CacheConstants; |
|
|
|
import com.hxhq.common.core.constant.SecurityConstants; |
|
|
|
@ -44,6 +45,12 @@ public class TokenService |
|
|
|
private final static Long TOKEN_REFRESH_THRESHOLD_MINUTES = CacheConstants.REFRESH_TIME * MILLIS_MINUTE; |
|
|
|
|
|
|
|
/** |
|
|
|
* 是否允许账户多终端同时登录(true允许 false不允许) |
|
|
|
*/ |
|
|
|
@Value("${token.soloLogin}") |
|
|
|
private boolean soloLogin; |
|
|
|
|
|
|
|
/** |
|
|
|
* 创建令牌 |
|
|
|
*/ |
|
|
|
public Map<String, Object> createToken(LoginUser loginUser) |
|
|
|
@ -130,13 +137,16 @@ public class TokenService |
|
|
|
/** |
|
|
|
* 删除用户缓存信息 |
|
|
|
*/ |
|
|
|
public void delLoginUser(String token) |
|
|
|
public void delLoginUser(String token, Long userId) |
|
|
|
{ |
|
|
|
if (StringUtils.isNotEmpty(token)) |
|
|
|
{ |
|
|
|
if (StringUtils.isNotEmpty(token)) { |
|
|
|
String userkey = JwtUtils.getUserKey(token); |
|
|
|
redisService.deleteObject(getTokenKey(userkey)); |
|
|
|
} |
|
|
|
if (!soloLogin && StringUtils.isNotNull(userId)) { |
|
|
|
String userIdKey = getUserIdKey(userId); |
|
|
|
redisService.deleteObject(userIdKey); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -166,10 +176,21 @@ public class TokenService |
|
|
|
// 根据uuid将loginUser缓存 |
|
|
|
String userKey = getTokenKey(loginUser.getToken()); |
|
|
|
redisService.setCacheObject(userKey, loginUser, TOKEN_EXPIRE_TIME, TimeUnit.MINUTES); |
|
|
|
if (!soloLogin) |
|
|
|
{ |
|
|
|
// 缓存用户唯一标识,防止同一帐号,同时登录 |
|
|
|
String userIdKey = getUserIdKey(loginUser.getSysUser().getUserId()); |
|
|
|
redisService.setCacheObject(userIdKey, userKey, TOKEN_EXPIRE_TIME, TimeUnit.MINUTES); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private String getTokenKey(String token) |
|
|
|
{ |
|
|
|
return ACCESS_TOKEN + token; |
|
|
|
} |
|
|
|
|
|
|
|
private String getUserIdKey(Long userId) |
|
|
|
{ |
|
|
|
return CacheConstants.LOGIN_USERID_KEY + userId; |
|
|
|
} |
|
|
|
} |