说明:1、修改token配置,把token抽出到配置文件
This commit is contained in:
parent
0fdf9e9537
commit
75c99ea561
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.ruoyi.common.security.service;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xiejs
|
||||||
|
* @desc token相关配置
|
||||||
|
* @create 2021-12-30
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
@ConfigurationProperties(prefix = "jwt")
|
||||||
|
public class TokenProperties {
|
||||||
|
|
||||||
|
|
||||||
|
private Long expireTime;
|
||||||
|
|
||||||
|
private String loginTokenKey;
|
||||||
|
|
||||||
|
private Long refreshTime;
|
||||||
|
|
||||||
|
public Long getExpireTime() {
|
||||||
|
return expireTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExpireTime(Long expireTime) {
|
||||||
|
this.expireTime = expireTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLoginTokenKey() {
|
||||||
|
return loginTokenKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLoginTokenKey(String loginTokenKey) {
|
||||||
|
this.loginTokenKey = loginTokenKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getRefreshTime() {
|
||||||
|
return refreshTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRefreshTime(Long refreshTime) {
|
||||||
|
this.refreshTime = refreshTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,5 @@
|
||||||
package com.ruoyi.common.security.service;
|
package com.ruoyi.common.security.service;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import com.ruoyi.common.core.constant.CacheConstants;
|
import com.ruoyi.common.core.constant.CacheConstants;
|
||||||
import com.ruoyi.common.core.constant.SecurityConstants;
|
import com.ruoyi.common.core.constant.SecurityConstants;
|
||||||
import com.ruoyi.common.core.utils.IdUtils;
|
import com.ruoyi.common.core.utils.IdUtils;
|
||||||
|
|
@ -16,33 +10,43 @@ import com.ruoyi.common.core.utils.ip.IpUtils;
|
||||||
import com.ruoyi.common.redis.service.RedisService;
|
import com.ruoyi.common.redis.service.RedisService;
|
||||||
import com.ruoyi.common.security.utils.SecurityUtils;
|
import com.ruoyi.common.security.utils.SecurityUtils;
|
||||||
import com.ruoyi.system.api.model.LoginUser;
|
import com.ruoyi.system.api.model.LoginUser;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* token验证处理
|
* token验证处理
|
||||||
*
|
*
|
||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class TokenService
|
public class TokenService {
|
||||||
{
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisService redisService;
|
private RedisService redisService;
|
||||||
|
|
||||||
protected static final long MILLIS_SECOND = 1000;
|
protected final long MILLIS_SECOND = 1000;
|
||||||
|
|
||||||
protected static final long MILLIS_MINUTE = 60 * MILLIS_SECOND;
|
protected final long MILLIS_MINUTE = 60 * MILLIS_SECOND;
|
||||||
|
|
||||||
private final static long expireTime = CacheConstants.EXPIRATION;
|
@Value("${jwt.expireTime}")
|
||||||
|
private long expireTime;
|
||||||
|
|
||||||
private final static String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY;
|
private static final String ACCESS_TOKEN = CacheConstants.LOGIN_TOKEN_KEY;
|
||||||
|
|
||||||
private final static Long MILLIS_MINUTE_TEN = CacheConstants.REFRESH_TIME * MILLIS_MINUTE;
|
@Value("${jwt.refreshTime}")
|
||||||
|
private long refreshTime;
|
||||||
|
|
||||||
|
private final Long MILLIS_MINUTE_TEN = refreshTime * MILLIS_MINUTE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建令牌
|
* 创建令牌
|
||||||
*/
|
*/
|
||||||
public Map<String, Object> createToken(LoginUser loginUser)
|
public Map<String, Object> createToken(LoginUser loginUser) {
|
||||||
{
|
|
||||||
String token = IdUtils.fastUUID();
|
String token = IdUtils.fastUUID();
|
||||||
Long userId = loginUser.getSysUser().getUserId();
|
Long userId = loginUser.getSysUser().getUserId();
|
||||||
String userName = loginUser.getSysUser().getUserName();
|
String userName = loginUser.getSysUser().getUserName();
|
||||||
|
|
@ -70,8 +74,7 @@ public class TokenService
|
||||||
*
|
*
|
||||||
* @return 用户信息
|
* @return 用户信息
|
||||||
*/
|
*/
|
||||||
public LoginUser getLoginUser()
|
public LoginUser getLoginUser() {
|
||||||
{
|
|
||||||
return getLoginUser(ServletUtils.getRequest());
|
return getLoginUser(ServletUtils.getRequest());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,8 +83,7 @@ public class TokenService
|
||||||
*
|
*
|
||||||
* @return 用户信息
|
* @return 用户信息
|
||||||
*/
|
*/
|
||||||
public LoginUser getLoginUser(HttpServletRequest request)
|
public LoginUser getLoginUser(HttpServletRequest request) {
|
||||||
{
|
|
||||||
// 获取请求携带的令牌
|
// 获取请求携带的令牌
|
||||||
String token = SecurityUtils.getToken(request);
|
String token = SecurityUtils.getToken(request);
|
||||||
return getLoginUser(token);
|
return getLoginUser(token);
|
||||||
|
|
@ -92,20 +94,15 @@ public class TokenService
|
||||||
*
|
*
|
||||||
* @return 用户信息
|
* @return 用户信息
|
||||||
*/
|
*/
|
||||||
public LoginUser getLoginUser(String token)
|
public LoginUser getLoginUser(String token) {
|
||||||
{
|
|
||||||
LoginUser user = null;
|
LoginUser user = null;
|
||||||
try
|
try {
|
||||||
{
|
if (StringUtils.isNotEmpty(token)) {
|
||||||
if (StringUtils.isNotEmpty(token))
|
|
||||||
{
|
|
||||||
String userkey = JwtUtils.getUserKey(token);
|
String userkey = JwtUtils.getUserKey(token);
|
||||||
user = redisService.getCacheObject(getTokenKey(userkey));
|
user = redisService.getCacheObject(getTokenKey(userkey));
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
@ -113,10 +110,8 @@ public class TokenService
|
||||||
/**
|
/**
|
||||||
* 设置用户身份信息
|
* 设置用户身份信息
|
||||||
*/
|
*/
|
||||||
public void setLoginUser(LoginUser loginUser)
|
public void setLoginUser(LoginUser loginUser) {
|
||||||
{
|
if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken())) {
|
||||||
if (StringUtils.isNotNull(loginUser) && StringUtils.isNotEmpty(loginUser.getToken()))
|
|
||||||
{
|
|
||||||
refreshToken(loginUser);
|
refreshToken(loginUser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -124,10 +119,8 @@ public class TokenService
|
||||||
/**
|
/**
|
||||||
* 删除用户缓存信息
|
* 删除用户缓存信息
|
||||||
*/
|
*/
|
||||||
public void delLoginUser(String token)
|
public void delLoginUser(String token) {
|
||||||
{
|
if (StringUtils.isNotEmpty(token)) {
|
||||||
if (StringUtils.isNotEmpty(token))
|
|
||||||
{
|
|
||||||
String userkey = JwtUtils.getUserKey(token);
|
String userkey = JwtUtils.getUserKey(token);
|
||||||
redisService.deleteObject(getTokenKey(userkey));
|
redisService.deleteObject(getTokenKey(userkey));
|
||||||
}
|
}
|
||||||
|
|
@ -138,12 +131,10 @@ public class TokenService
|
||||||
*
|
*
|
||||||
* @param loginUser
|
* @param loginUser
|
||||||
*/
|
*/
|
||||||
public void verifyToken(LoginUser loginUser)
|
public void verifyToken(LoginUser loginUser) {
|
||||||
{
|
|
||||||
long expireTime = loginUser.getExpireTime();
|
long expireTime = loginUser.getExpireTime();
|
||||||
long currentTime = System.currentTimeMillis();
|
long currentTime = System.currentTimeMillis();
|
||||||
if (expireTime - currentTime <= MILLIS_MINUTE_TEN)
|
if (expireTime - currentTime <= MILLIS_MINUTE_TEN) {
|
||||||
{
|
|
||||||
refreshToken(loginUser);
|
refreshToken(loginUser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -153,8 +144,7 @@ public class TokenService
|
||||||
*
|
*
|
||||||
* @param loginUser 登录信息
|
* @param loginUser 登录信息
|
||||||
*/
|
*/
|
||||||
public void refreshToken(LoginUser loginUser)
|
public void refreshToken(LoginUser loginUser) {
|
||||||
{
|
|
||||||
loginUser.setLoginTime(System.currentTimeMillis());
|
loginUser.setLoginTime(System.currentTimeMillis());
|
||||||
loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
|
loginUser.setExpireTime(loginUser.getLoginTime() + expireTime * MILLIS_MINUTE);
|
||||||
// 根据uuid将loginUser缓存
|
// 根据uuid将loginUser缓存
|
||||||
|
|
@ -162,8 +152,7 @@ public class TokenService
|
||||||
redisService.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
redisService.setCacheObject(userKey, loginUser, expireTime, TimeUnit.MINUTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getTokenKey(String token)
|
private String getTokenKey(String token) {
|
||||||
{
|
|
||||||
return ACCESS_TOKEN + token;
|
return ACCESS_TOKEN + token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue