|
|
|
@ -4,6 +4,7 @@ import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
@ -29,8 +30,7 @@ import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
|
|
* @author ruoyi
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
public class TokenService
|
|
|
|
|
{
|
|
|
|
|
public class TokenService {
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(TokenService.class);
|
|
|
|
|
|
|
|
|
|
// 令牌自定义标识
|
|
|
|
@ -45,6 +45,10 @@ public class TokenService
|
|
|
|
|
@Value("${token.expireTime}")
|
|
|
|
|
private int expireTime;
|
|
|
|
|
|
|
|
|
|
// 是否允许账户多终端同时登录(true允许 false不允许)
|
|
|
|
|
@Value("${token.soloLogin}")
|
|
|
|
|
private boolean soloLogin;
|
|
|
|
|
|
|
|
|
|
protected static final long MILLIS_SECOND = 1000;
|
|
|
|
|
|
|
|
|
|
protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;
|
|
|
|
@ -54,28 +58,24 @@ public class TokenService
|
|
|
|
|
@Autowired
|
|
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取用户身份信息
|
|
|
|
|
*
|
|
|
|
|
* @return 用户信息
|
|
|
|
|
*/
|
|
|
|
|
public LoginUser getLoginUser(HttpServletRequest request)
|
|
|
|
|
{
|
|
|
|
|
public LoginUser getLoginUser(HttpServletRequest request) {
|
|
|
|
|
// 获取请求携带的令牌
|
|
|
|
|
String token = getToken(request);
|
|
|
|
|
if (StringUtils.isNotEmpty(token))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isNotEmpty(token)) {
|
|
|
|
|
try {
|
|
|
|
|
Claims claims = parseToken(token);
|
|
|
|
|
// 解析对应的权限以及用户信息
|
|
|
|
|
String uuid = (String) claims.get(Constants.LOGIN_USER_KEY);
|
|
|
|
|
String userKey = getTokenKey(uuid);
|
|
|
|
|
LoginUser user = redisCache.getCacheObject(userKey);
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("获取用户信息异常'{}'", e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -85,10 +85,8 @@ public class TokenService
|
|
|
|
|
/**
|
|
|
|
|
* 设置用户身份信息
|
|
|
|
|
*/
|
|
|
|
|
public void setLoginUser(LoginUser loginUser)
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken()))
|
|
|
|
|
{
|
|
|
|
|
public void setLoginUser(LoginUser loginUser) {
|
|
|
|
|
if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken())) {
|
|
|
|
|
refreshToken(loginUser);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -96,13 +94,15 @@ public class TokenService
|
|
|
|
|
/**
|
|
|
|
|
* 删除用户身份信息
|
|
|
|
|
*/
|
|
|
|
|
public void delLoginUser(String token)
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isNotEmpty(token))
|
|
|
|
|
{
|
|
|
|
|
public void delLoginUser(String token, Long userId) {
|
|
|
|
|
if (StringUtils.isNotEmpty(token)) {
|
|
|
|
|
String userKey = getTokenKey(token);
|
|
|
|
|
redisCache.deleteObject(userKey);
|
|
|
|
|
}
|
|
|
|
|
if (!soloLogin && StringUtils.isNotNull(userId)) {
|
|
|
|
|
String userIdKey = getUserIdKey(userId);
|
|
|
|
|
redisCache.deleteObject(userIdKey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -111,8 +111,7 @@ public class TokenService
|
|
|
|
|
* @param loginUser 用户信息
|
|
|
|
|
* @return 令牌
|
|
|
|
|
*/
|
|
|
|
|
public String createToken(LoginUser loginUser)
|
|
|
|
|
{
|
|
|
|
|
public String createToken(LoginUser loginUser) {
|
|
|
|
|
String token = IdUtils.fastUUID();
|
|
|
|
|
loginUser.setToken(token);
|
|
|
|
|
setUserAgent(loginUser);
|
|
|
|
@ -129,12 +128,10 @@ public class TokenService
|
|
|
|
|
* @param loginUser
|
|
|
|
|
* @return 令牌
|
|
|
|
|
*/
|
|
|
|
|
public void verifyToken(LoginUser loginUser)
|
|
|
|
|
{
|
|
|
|
|
public void verifyToken(LoginUser loginUser) {
|
|
|
|
|
long expireTime = loginUser.getExpireTime();
|
|
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
|
if (expireTime - currentTime <= MILLIS_MINUTE_TEN)
|
|
|
|
|
{
|
|
|
|
|
if (expireTime - currentTime <= MILLIS_MINUTE_TEN) {
|
|
|
|
|
refreshToken(loginUser);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -144,13 +141,17 @@ public class TokenService
|
|
|
|
|
*
|
|
|
|
|
* @param loginUser 登录信息
|
|
|
|
|
*/
|
|
|
|
|
public void refreshToken(LoginUser loginUser)
|
|
|
|
|
{
|
|
|
|
|
public void refreshToken(LoginUser loginUser) {
|
|
|
|
|
loginUser.setLoginTime(System.currentTimeMillis());
|
|
|
|
|
loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
|
|
|
|
|
// 根据uuid将loginUser缓存
|
|
|
|
|
String userKey = getTokenKey(loginUser.getToken());
|
|
|
|
|
redisCache.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
|
|
|
|
if (!soloLogin) {
|
|
|
|
|
// 缓存用户唯一标识,防止同一帐号,同时登录
|
|
|
|
|
String userIdKey = getUserIdKey(loginUser.getUser().getUserId());
|
|
|
|
|
redisCache.setCacheObject(userIdKey, userKey, expireTime, TimeUnit.MINUTES);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -158,8 +159,7 @@ public class TokenService
|
|
|
|
|
*
|
|
|
|
|
* @param loginUser 登录信息
|
|
|
|
|
*/
|
|
|
|
|
public void setUserAgent(LoginUser loginUser)
|
|
|
|
|
{
|
|
|
|
|
public void setUserAgent(LoginUser loginUser) {
|
|
|
|
|
UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
|
|
|
|
|
String ip = IpUtils.getIpAddr();
|
|
|
|
|
loginUser.setIpaddr(ip);
|
|
|
|
@ -174,8 +174,7 @@ public class TokenService
|
|
|
|
|
* @param claims 数据声明
|
|
|
|
|
* @return 令牌
|
|
|
|
|
*/
|
|
|
|
|
private String createToken(Map<String, Object> claims)
|
|
|
|
|
{
|
|
|
|
|
private String createToken(Map<String, Object> claims) {
|
|
|
|
|
String token = Jwts.builder()
|
|
|
|
|
.setClaims(claims)
|
|
|
|
|
.signWith(SignatureAlgorithm.HS512, secret).compact();
|
|
|
|
@ -188,8 +187,7 @@ public class TokenService
|
|
|
|
|
* @param token 令牌
|
|
|
|
|
* @return 数据声明
|
|
|
|
|
*/
|
|
|
|
|
private Claims parseToken(String token)
|
|
|
|
|
{
|
|
|
|
|
private Claims parseToken(String token) {
|
|
|
|
|
return Jwts.parser()
|
|
|
|
|
.setSigningKey(secret)
|
|
|
|
|
.parseClaimsJws(token)
|
|
|
|
@ -202,8 +200,7 @@ public class TokenService
|
|
|
|
|
* @param token 令牌
|
|
|
|
|
* @return 用户名
|
|
|
|
|
*/
|
|
|
|
|
public String getUsernameFromToken(String token)
|
|
|
|
|
{
|
|
|
|
|
public String getUsernameFromToken(String token) {
|
|
|
|
|
Claims claims = parseToken(token);
|
|
|
|
|
return claims.getSubject();
|
|
|
|
|
}
|
|
|
|
@ -214,18 +211,19 @@ public class TokenService
|
|
|
|
|
* @param request
|
|
|
|
|
* @return token
|
|
|
|
|
*/
|
|
|
|
|
private String getToken(HttpServletRequest request)
|
|
|
|
|
{
|
|
|
|
|
private String getToken(HttpServletRequest request) {
|
|
|
|
|
String token = request.getHeader(header);
|
|
|
|
|
if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
|
|
|
|
|
{
|
|
|
|
|
if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
|
|
|
|
|
token = token.replace(Constants.TOKEN_PREFIX, "");
|
|
|
|
|
}
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getTokenKey(String uuid)
|
|
|
|
|
{
|
|
|
|
|
private String getTokenKey(String uuid) {
|
|
|
|
|
return CacheConstants.LOGIN_TOKEN_KEY + uuid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getUserIdKey(Long userId) {
|
|
|
|
|
return Constants.LOGIN_USERID_KEY + userId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|