perf: 移除hutool

This commit is contained in:
fit2cloud-chenyw
2024-01-11 14:43:23 +08:00
parent 60e9d88ba9
commit c9685da158
33 changed files with 360 additions and 127 deletions

View File

@@ -1,15 +1,17 @@
package io.dataease.auth.interceptor;
import cn.hutool.core.util.ReflectUtil;
import io.dataease.utils.CommonBeanFactory;
import io.dataease.utils.DeReflectUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
@@ -51,7 +53,8 @@ public class CorsInterceptor implements HandlerInterceptor {
bean = CommonBeanFactory.getBean(aClass);
}
if (ObjectUtils.isNotEmpty(bean)) {
Object result = ReflectUtil.invoke(bean, methodName);
Method method = DeReflectUtil.findMethod(aClass, methodName);
Object result = ReflectionUtils.invokeMethod(method, bean);
if (ObjectUtils.isNotEmpty(result)) {
List<String> list = (List<String>) result;
if (CollectionUtils.isNotEmpty(list)) {

View File

@@ -1,9 +1,9 @@
package io.dataease.doc;
import cn.hutool.core.util.RandomUtil;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import org.apache.commons.lang3.RandomUtils;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
@@ -22,10 +22,10 @@ public class SwaggerConfig {
@Bean
public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
return openApi -> {
if (openApi.getTags()!=null){
if (openApi.getTags() != null) {
openApi.getTags().forEach(tag -> {
Map<String,Object> map=new HashMap<>();
map.put("x-order", RandomUtil.randomInt(0,100));
Map<String, Object> map = new HashMap<>();
map.put("x-order", RandomUtils.nextInt(0, 100));
tag.setExtensions(map);
});
}
@@ -48,7 +48,6 @@ public class SwaggerConfig {
}
@Bean
public GroupedOpenApi visualizationApi() {
return GroupedOpenApi.builder().group("1-visualization").displayName("可视化管理").packagesToScan("io.dataease.visualization").build();
@@ -89,6 +88,4 @@ public class SwaggerConfig {
}
}

View File

@@ -0,0 +1,19 @@
package io.dataease.utils;
import org.apache.commons.lang3.ArrayUtils;
import java.lang.reflect.Method;
public class DeReflectUtil {
public static Method findMethod(Class<?> cla, String methodName) {
Method[] methods = cla.getMethods();
if (ArrayUtils.isEmpty(methods)) return null;
for (Method method : methods) {
if (method.getName().equals(methodName)){
return method;
}
}
return null;
}
}

View File

@@ -1,18 +1,20 @@
package io.dataease.utils;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
public class IDUtils {
private static SnowFlake snowFlake = new SnowFlake(1, 1);
public static String randomID(Integer num) {
num = ObjectUtils.isEmpty(num) ? 16 : num;
return RandomUtil.randomString(16);
return RandomStringUtils.randomAlphanumeric(num);
}
// 主键请不要使用字符串 推荐雪花算法
public static Long snowID() {
return IdUtil.getSnowflakeNextId();
return snowFlake.nextId();
}
}

View File

@@ -1,23 +1,42 @@
package io.dataease.utils;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.AES;
import io.dataease.exception.DEException;
import io.dataease.model.RSAModel;
import io.dataease.rsa.dao.entity.CoreRsa;
import io.dataease.rsa.manage.RsaManage;
import jakarta.annotation.Resource;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.Security;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
@Component
public class RsaUtils {
static {
if (ObjectUtils.isNotEmpty(Security.getProvider("BC"))) {
Security.removeProvider("BC");
}
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
private static final int MAX_ENCRYPT_BLOCK = 117;
private static final int MAX_DECRYPT_BLOCK = 128;
private static final String PK_SEPARATOR = "-pk_separator-";
private static RsaManage rsaManage;
@@ -27,20 +46,108 @@ public class RsaUtils {
RsaUtils.rsaManage = rsaManage;
}
private static KeyPair getKeyPair() {
KeyPairGenerator generator = null;
try {
generator = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
LogUtil.error(e.getMessage(), e);
DEException.throwException(e);
}
generator.initialize(1024);
return generator.generateKeyPair();
}
private static PrivateKey getPrivateKey(String privateKey) {
KeyFactory keyFactory = null;
try {
keyFactory = KeyFactory.getInstance("RSA");
byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
return keyFactory.generatePrivate(keySpec);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
private static PublicKey getPublicKey(String publicKey) {
KeyFactory keyFactory = null;
try {
keyFactory = KeyFactory.getInstance("RSA");
byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
return keyFactory.generatePublic(keySpec);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
private static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
int inputLen = data.getBytes().length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_ENCRYPT_BLOCK;
}
byte[] encryptedData = out.toByteArray();
out.close();
return Base64.encodeBase64String(encryptedData);
}
private static String decrypt(String data, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] dataBytes = Base64.decodeBase64(data);
int inputLen = dataBytes.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
}
out.write(cache, 0, cache.length);
i++;
offset = i * MAX_DECRYPT_BLOCK;
}
out.close();
return out.toString(StandardCharsets.UTF_8);
}
public static RSAModel generate() {
RSA rsa = new RSA();
String privateKeyBase64 = rsa.getPrivateKeyBase64();
String publicKeyBase64 = rsa.getPublicKeyBase64();
KeyPair keyPair = getKeyPair();
String privateKey = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
String publicKey = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
RSAModel rsaModel = new RSAModel();
rsaModel.setPrivateKey(privateKeyBase64);
rsaModel.setPublicKey(publicKeyBase64);
rsaModel.setPrivateKey(privateKey);
rsaModel.setPublicKey(publicKey);
rsaModel.setAesKey(generateAesKey());
return rsaModel;
}
public static String decryptStr(String data, String privateKey) {
RSA rsa = new RSA(privateKey, null);
return rsa.decryptStr(data, KeyType.PrivateKey);
try {
return decrypt(data, getPrivateKey(privateKey));
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
public static String decryptStr(String data) {
@@ -48,8 +155,12 @@ public class RsaUtils {
}
public static String encryptStr(String data) {
RSA rsa = new RSA(privateKey(), publicKey());
return rsa.encryptBase64(data, KeyType.PublicKey);
try {
return encrypt(data, getPublicKey(publicKey()));
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
public static String privateKey() {
@@ -61,23 +172,40 @@ public class RsaUtils {
CoreRsa coreRsa = rsaManage.query();
String publicKey = coreRsa.getPublicKey();
String aesKey = coreRsa.getAesKey();
// Security.addProvider(new BouncyCastleProvider());
String pk = ascEncrypt(publicKey, aesKey);
String pk = ascEncrypt(publicKey, aesKey).replaceAll("[\\s*\t\n\r]", "");
String separator = Base64Utils.encodeToUrlSafeString(PK_SEPARATOR.getBytes(StandardCharsets.UTF_8));
return pk + separator + aesKey;
}
private static final String IV_KEY = "0000000000000000";
private static String generateAesKey() {
return RandomUtil.randomString(16);
return RandomStringUtils.randomAlphanumeric(16);
}
private static String ascEncrypt(String message, String key) {
byte[] baseKey = key.getBytes(StandardCharsets.UTF_8);
/*byte[] baseKey = key.getBytes(StandardCharsets.UTF_8);
byte[] ivBytes = IV_KEY.getBytes(StandardCharsets.UTF_8);
AES aes = new AES("CBC", "PKCS7Padding", baseKey, ivBytes);
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
return Base64Utils.encodeToString(aes.encrypt(messageBytes));
return Base64Utils.encodeToString(aes.encrypt(messageBytes));*/
Cipher cipher = null;
try {
byte[] baseKey = key.getBytes(StandardCharsets.UTF_8);
byte[] ivBytes = IV_KEY.getBytes(StandardCharsets.UTF_8);
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
// 根据secretKey(密钥)的字节内容,"恢复"秘钥对象
SecretKey keySpec = new SecretKeySpec(baseKey, "AES");
IvParameterSpec ivps = new IvParameterSpec(ivBytes);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivps);
byte[] data = cipher.doFinal(messageBytes);
return Base64.encodeBase64String(data);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,89 @@
package io.dataease.utils;
public class SnowFlake {
/**
* 起始的时间戳
*/
private final static long START_STMP = 1480166465631L;
/**
* 每一部分占用的位数
*/
private final static long SEQUENCE_BIT = 12; //序列号占用的位数
private final static long MACHINE_BIT = 5; //机器标识占用的位数
private final static long DATACENTER_BIT = 5;//数据中心占用的位数
/**
* 每一部分的最大值
*/
private final static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT);
private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);
/**
* 每一部分向左的位移
*/
private final static long MACHINE_LEFT = SEQUENCE_BIT;
private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;
private long datacenterId; //数据中心
private long machineId; //机器标识
private long sequence = 0L; //序列号
private long lastStmp = -1L;//上一次时间戳
public SnowFlake(long datacenterId, long machineId) {
if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) {
throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
}
if (machineId > MAX_MACHINE_NUM || machineId < 0) {
throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0");
}
this.datacenterId = datacenterId;
this.machineId = machineId;
}
/**
* 产生下一个ID
*
* @return
*/
public synchronized long nextId() {
long currStmp = getNewstmp();
if (currStmp < lastStmp) {
throw new RuntimeException("Clock moved backwards. Refusing to generate id");
}
if (currStmp == lastStmp) {
//相同毫秒内,序列号自增
sequence = (sequence + 1) & MAX_SEQUENCE;
//同一毫秒的序列数已经达到最大
if (sequence == 0L) {
currStmp = getNextMill();
}
} else {
//不同毫秒内序列号置为0
sequence = 0L;
}
lastStmp = currStmp;
return (currStmp - START_STMP) << TIMESTMP_LEFT //时间戳部分
| datacenterId << DATACENTER_LEFT //数据中心部分
| machineId << MACHINE_LEFT //机器标识部分
| sequence; //序列号部分
}
private long getNextMill() {
long mill = getNewstmp();
while (mill <= lastStmp) {
mill = getNewstmp();
}
return mill;
}
private long getNewstmp() {
return System.currentTimeMillis();
}
}

View File

@@ -1,9 +1,10 @@
package io.dataease.utils;
import cn.hutool.core.codec.Base64Encoder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -12,7 +13,7 @@ import static io.dataease.constant.StaticResourceConstants.*;
public class StaticResourceUtils {
private final static String FILE_BASE_PATH = USER_HOME+ FILE_SEPARATOR+UPLOAD_URL_PREFIX;
private final static String FILE_BASE_PATH = USER_HOME + FILE_SEPARATOR + UPLOAD_URL_PREFIX;
public static String ensureBoth(@NonNull String string, @NonNull String bothfix) {
return ensureBoth(string, bothfix, bothfix);
@@ -53,8 +54,7 @@ public class StaticResourceUtils {
}
/**
*
* @param imgFile local storage path
* @param imgFile local storage path
* @return
*/
public static String getImgFileToBase64(String imgFile) {
@@ -63,7 +63,7 @@ public class StaticResourceUtils {
byte[] buffer = null;
//Read picture byte array
try {
inputStream = new FileInputStream(FILE_BASE_PATH+FILE_SEPARATOR+imgFile);
inputStream = new FileInputStream(FILE_BASE_PATH + FILE_SEPARATOR + imgFile);
int count = 0;
while (count == 0) {
count = inputStream.available();
@@ -72,9 +72,9 @@ public class StaticResourceUtils {
inputStream.read(buffer);
} catch (IOException e) {
LogUtil.error(e);
}catch (Exception e){
} catch (Exception e) {
LogUtil.error(e);
}finally {
} finally {
if (inputStream != null) {
try {
// Close InputStream
@@ -85,10 +85,9 @@ public class StaticResourceUtils {
}
}
// Encode byte array as Base64
if(buffer!=null){
return Base64Encoder.encode(buffer);
}else{
if (buffer != null) {
return Base64Utils.encodeToString(buffer);
} else {
return null;
}
}

View File

@@ -1,6 +1,5 @@
package io.dataease.utils;
import cn.hutool.core.collection.ListUtil;
import io.dataease.constant.XpackSettingConstants;
import java.util.List;
@@ -8,7 +7,8 @@ import java.util.List;
public class SystemSettingUtils {
public static boolean xpackSetting(String pkey) {
List<String> xpackSettingList = ListUtil.toList(XpackSettingConstants.AUTO_CREATE_USER);
List<String> xpackSettingList = List.of(XpackSettingConstants.AUTO_CREATE_USER);
return xpackSettingList.contains(pkey);
}
}

View File

@@ -1,14 +1,12 @@
package io.dataease.utils;
import cn.hutool.core.collection.CollectionUtil;
import io.dataease.model.ITreeBase;
import io.dataease.model.TreeBaseModel;
import io.dataease.model.TreeModel;
import io.dataease.model.TreeResultModel;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Arrays;
@@ -38,21 +36,21 @@ public class TreeUtils {
List<Long> existedList = new ArrayList<>();
modelList.forEach(po -> {
List<TreeModel> children = null;
if (CollectionUtil.isNotEmpty(children = childMap.get(po.getId()))) {
if (CollectionUtils.isNotEmpty(children = childMap.get(po.getId()))) {
po.setChildren(children);
existedList.addAll(children.stream().map(TreeModel::getId).toList());
}
});
if (CollectionUtil.isEmpty(modelList)) {
if (CollectionUtils.isEmpty(modelList)) {
return null;
}
List<TreeModel> floatingList = modelList.stream().filter(node -> !isRoot(node) && !existedList.contains(node.getId())).toList();
if (CollectionUtil.isNotEmpty(existedList)) {
if (CollectionUtils.isNotEmpty(existedList)) {
modelResult = modelList.stream().filter(node -> !existedList.contains(node.getId())).toList();
} else {
modelResult = modelList;
}
if (rootExist.get() && CollectionUtil.isNotEmpty(floatingList)) {
if (rootExist.get() && CollectionUtils.isNotEmpty(floatingList)) {
modelResult = modelResult.stream().filter(TreeUtils::isRoot).collect(Collectors.toList());
TreeModel root = modelResult.get(0);
if (root.getChildren() == null) {

View File

@@ -1,6 +1,5 @@
package io.dataease.utils;
import cn.hutool.core.collection.ListUtil;
import io.dataease.constant.AuthConstant;
import org.apache.commons.lang3.StringUtils;
@@ -8,7 +7,7 @@ import java.util.List;
public class WhitelistUtils {
public static List<String> WHITE_PATH = ListUtil.of(
public static List<String> WHITE_PATH = List.of(
"/login/localLogin",
"/apisix/check",
"/dekey",