perf: 优化 Mybatis 获得 Token问题

pull/9/head
Carina 3 years ago
parent 493a669aba
commit bac08028c5

@ -8,12 +8,15 @@ package org.opsli.common.constants;
*/
public interface SignConstants {
/** username */
/** 用户账号 */
String ACCOUNT = "account";
/** user Id */
/** 用户ID */
String USER_ID = "userId";
/** 租户ID */
String TENANT_ID = "tenantId";
/** 时间戳 */
String TIMESTAMP = "timestamp";

@ -29,8 +29,7 @@ import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.opsli.common.constants.MyBatisConstants;
import org.opsli.core.utils.UserUtil;
import org.opsli.core.utils.excel.factory.AbstractModelHelper;
import org.opsli.core.utils.UserTokenUtil;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
@ -145,7 +144,7 @@ public class MybatisAutoFillInterceptor implements Interceptor {
// 如果创建人 为空则进行默认赋值
Object createValue = ReflectUtil.getFieldValue(arg, f.getName());
if(StringUtils.isBlank(Convert.toStr(createValue))){
BeanUtil.setProperty(arg, MyBatisConstants.FIELD_CREATE_BY, UserUtil.getUser().getId());
BeanUtil.setProperty(arg, MyBatisConstants.FIELD_CREATE_BY, UserTokenUtil.getUserIdByToken());
}
break;
// 更新人
@ -153,7 +152,7 @@ public class MybatisAutoFillInterceptor implements Interceptor {
// 如果更新人 为空则进行默认赋值
Object updateValue = ReflectUtil.getFieldValue(arg, f.getName());
if(StringUtils.isBlank(Convert.toStr(updateValue))){
BeanUtil.setProperty(arg, MyBatisConstants.FIELD_UPDATE_BY, UserUtil.getUser().getId());
BeanUtil.setProperty(arg, MyBatisConstants.FIELD_UPDATE_BY, UserTokenUtil.getUserIdByToken());
}
break;
// 创建日期
@ -178,7 +177,7 @@ public class MybatisAutoFillInterceptor implements Interceptor {
// 如果租户ID 为空则进行默认赋值
Object tenantValue = ReflectUtil.getFieldValue(arg, f.getName());
if(StringUtils.isBlank(Convert.toStr(tenantValue))){
BeanUtil.setProperty(arg, MyBatisConstants.FIELD_TENANT, UserUtil.getTenantId());
BeanUtil.setProperty(arg, MyBatisConstants.FIELD_TENANT, UserTokenUtil.getTenantIdByToken());
}
break;
default:
@ -242,7 +241,7 @@ public class MybatisAutoFillInterceptor implements Interceptor {
// 如果更新人 为空则进行默认赋值
Object updateValue = ReflectUtil.getFieldValue(arg, f.getName());
if(StringUtils.isBlank(Convert.toStr(updateValue))){
BeanUtil.setProperty(arg, MyBatisConstants.FIELD_UPDATE_BY, UserUtil.getUser().getId());
BeanUtil.setProperty(arg, MyBatisConstants.FIELD_UPDATE_BY, UserTokenUtil.getUserIdByToken());
}
break;
// 更新日期

@ -96,7 +96,8 @@ public class JwtUtil {
* @param isExpire
* @return java.lang.String Token
*/
public static String sign(String tokenType, String account, String userId, boolean isExpire) {
public static String sign(String tokenType, String account,
String userId, String tenantId, boolean isExpire) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
@ -110,6 +111,7 @@ public class JwtUtil {
.withClaim(SignConstants.TYPE, tokenType)
.withClaim(SignConstants.ACCOUNT, account)
.withClaim(SignConstants.USER_ID, userId)
.withClaim(SignConstants.TENANT_ID, tenantId)
.withClaim(SignConstants.TIMESTAMP, String.valueOf(currentTimeMillis));
// 如果为是 则开启失效时间
@ -150,7 +152,8 @@ public class JwtUtil {
// ==================
public static void main(String[] args) {
String token = JwtUtil.sign(TokenTypeConstants.TYPE_SYSTEM, "test","123123", true);
String token = JwtUtil.sign(TokenTypeConstants.TYPE_SYSTEM,
"test","123123", "",true);
boolean verify = JwtUtil.verify(token);
System.out.println(token);

@ -33,6 +33,7 @@ import org.opsli.common.constants.TokenConstants;
import org.opsli.common.constants.TokenTypeConstants;
import org.opsli.common.enums.LoginLimitRefuse;
import org.opsli.common.exception.TokenException;
import org.opsli.core.api.TokenThreadLocal;
import org.opsli.core.autoconfigure.properties.GlobalProperties;
import org.opsli.core.cache.local.CacheUtil;
import org.opsli.core.msg.CoreMsg;
@ -118,7 +119,11 @@ public class UserTokenUtil {
// 生成 Token 包含 username userId timestamp
boolean reviveMode = LOGIN_PROPERTIES.getReviveMode() != null && LOGIN_PROPERTIES.getReviveMode();
String signToken = JwtUtil.sign(
TokenTypeConstants.TYPE_SYSTEM, user.getUsername(), user.getId(), !reviveMode);
TokenTypeConstants.TYPE_SYSTEM,
user.getUsername(),
user.getId(),
user.getTenantId(),
!reviveMode);
// 获得当前Token时间戳
long timestamp = Convert.toLong(
@ -153,17 +158,19 @@ public class UserTokenUtil {
throw new TokenException(TokenMsg.EXCEPTION_TOKEN_CREATE_ERROR);
}
/**
* Token ID
* @return String
*/
public static String getUserIdByToken() {
return getUserIdByToken(TokenThreadLocal.get());
}
/**
* Token ID
* @param token token
* @return String
*/
public static String getUserIdByToken(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(token)){
return null;
}
@ -174,17 +181,20 @@ public class UserTokenUtil {
return userId;
}
/**
* Token username
* Token
* @return String
*/
public static String getUserNameByToken() {
return getUserNameByToken(TokenThreadLocal.get());
}
/**
* Token
* @param token token
* @return String
*/
public static String getUserNameByToken(String token) {
// 判断 工具类是否初始化完成
ThrowExceptionUtil.isThrowException(!IS_INIT,
CoreMsg.OTHER_EXCEPTION_UTILS_INIT);
if(StringUtils.isEmpty(token)){
return null;
}
@ -195,7 +205,29 @@ public class UserTokenUtil {
return username;
}
/**
* Token ID
* @return String
*/
public static String getTenantIdByToken() {
return getTenantIdByToken(TokenThreadLocal.get());
}
/**
* Token ID
* @param token token
* @return String
*/
public static String getTenantIdByToken(String token) {
if(StringUtils.isEmpty(token)){
return null;
}
String username = "";
try {
username = JwtUtil.getClaim(token, SignConstants.TENANT_ID);
}catch (Exception ignored){}
return username;
}
/**
* 退

Loading…
Cancel
Save