From bac08028c52a82520f53a8080a1011b85b894075 Mon Sep 17 00:00:00 2001 From: Carina Date: Fri, 18 Jun 2021 18:52:18 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20Mybatis=20?= =?UTF-8?q?=E8=8E=B7=E5=BE=97=20Token=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../opsli/common/constants/SignConstants.java | 7 ++- .../MybatisAutoFillInterceptor.java | 11 ++-- .../java/org/opsli/core/utils/JwtUtil.java | 7 ++- .../org/opsli/core/utils/UserTokenUtil.java | 56 +++++++++++++++---- 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/constants/SignConstants.java b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/constants/SignConstants.java index 244c973..31a2873 100644 --- a/opsli-base-support/opsli-common/src/main/java/org/opsli/common/constants/SignConstants.java +++ b/opsli-base-support/opsli-common/src/main/java/org/opsli/common/constants/SignConstants.java @@ -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"; diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/filters/interceptor/MybatisAutoFillInterceptor.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/filters/interceptor/MybatisAutoFillInterceptor.java index 010724d..eb08e3d 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/filters/interceptor/MybatisAutoFillInterceptor.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/filters/interceptor/MybatisAutoFillInterceptor.java @@ -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; // 更新日期 diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/JwtUtil.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/JwtUtil.java index bc30e92..7b22b56 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/JwtUtil.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/JwtUtil.java @@ -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); diff --git a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/UserTokenUtil.java b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/UserTokenUtil.java index c69fdd8..77ad6d8 100644 --- a/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/UserTokenUtil.java +++ b/opsli-base-support/opsli-core/src/main/java/org/opsli/core/utils/UserTokenUtil.java @@ -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; + } /** * 退出登陆