This commit is contained in:
2025-05-30 10:31:21 +08:00
parent 115edc19fa
commit 80f4491836
73 changed files with 310 additions and 485 deletions

View File

@@ -12,7 +12,7 @@ public class ApiConst {
*
* @version 1.0.0
*/
public static enum Code {
public enum Code {
/**
* 成功返回码
@@ -94,11 +94,11 @@ public class ApiConst {
*/
CODE_MISS_TOKEN_ERROR(5006);
private Code(int intCode) {
Code(int intCode) {
this.intCode = intCode;
}
private int intCode;
private final int intCode;
public int code() {
return intCode;

View File

@@ -6,6 +6,7 @@ import com.ycwl.basic.enums.BizCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serial;
import java.io.Serializable;
/**
@@ -17,6 +18,7 @@ import java.io.Serializable;
@ApiModel(value = "通用返回数据对象")
public class ApiResponse<T> implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
@@ -48,7 +50,7 @@ public class ApiResponse<T> implements Serializable {
* @return
*/
public static <T> ApiResponse<T> success(T data) {
ApiResponse<T> response = new ApiResponse<T>();
ApiResponse<T> response = new ApiResponse<>();
response.setCode(ApiConst.Code.CODE_SUCCESS.code());
response.setData(data);
return response;
@@ -60,7 +62,7 @@ public class ApiResponse<T> implements Serializable {
* @return
*/
public static <T> ApiResponse<T> buildEmptyResponse() {
ApiResponse<T> response = new ApiResponse<T>();
ApiResponse<T> response = new ApiResponse<>();
response.setCode(ApiConst.Code.CODE_CONTENT_EMPTY.code());
return response;
}
@@ -84,7 +86,7 @@ public class ApiResponse<T> implements Serializable {
* @return
*/
public static <T> ApiResponse<T> buildResponse(int code, T data, String msg) {
ApiResponse<T> response = new ApiResponse<T>();
ApiResponse<T> response = new ApiResponse<>();
response.setCode(code);
response.setData(data);
response.setMsg(msg);
@@ -99,7 +101,7 @@ public class ApiResponse<T> implements Serializable {
* @return
*/
public static <T> ApiResponse<T> buildFlagResponse(boolean flag, T data) {
ApiResponse<T> response = new ApiResponse<T>();
ApiResponse<T> response = new ApiResponse<>();
if (flag) {
response.setCode(ApiConst.Code.CODE_SUCCESS.code());
response.setData(data);
@@ -120,7 +122,7 @@ public class ApiResponse<T> implements Serializable {
* @return
*/
public static <T> ApiResponse<T> buildResponse(ApiConst.Code code, String msg) {
ApiResponse<T> response = new ApiResponse<T>();
ApiResponse<T> response = new ApiResponse<>();
response.setCode(code.code());
response.setMsg(msg);
return response;
@@ -134,7 +136,7 @@ public class ApiResponse<T> implements Serializable {
* @return
*/
public static <T> ApiResponse<T> buildResponse(int code, String msg) {
ApiResponse<T> response = new ApiResponse<T>();
ApiResponse<T> response = new ApiResponse<>();
response.setCode(code);
response.setMsg(msg);
return response;

View File

@@ -1,5 +1,6 @@
package com.ycwl.basic.utils;
import cn.hutool.core.codec.Base64;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
@@ -7,16 +8,15 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
public class ImageUtils {
public static MultipartFile base64ToMultipartFile(String base64) {
String[] baseStrs = base64.split(",");
byte[] b;
b = Base64.getDecoder().decode(baseStrs[0]);
b = Base64.decode(baseStrs[0]);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
b[i] += (byte) 256;
}
}
return new Base64DecodedMultipartFile(b, baseStrs[0]);
@@ -33,14 +33,12 @@ public class ImageUtils {
@Override
public String getName() {
// TODO - implementation depends on your requirements
return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
}
@Override
public String getOriginalFilename() {
// TODO - implementation depends on your requirements
return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];
return System.currentTimeMillis() + "." + header.split("/")[1];
}
@Override

View File

@@ -3,8 +3,6 @@ package com.ycwl.basic.utils;
import org.apache.commons.lang3.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 获取IP方法
@@ -43,106 +41,6 @@ public class IpUtils {
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
}
/**
* 将IPv4地址转换成字节
*
* @param text IPv4地址
* @return byte 字节
*/
public static byte[] textToNumericFormatV4(String text) {
if (text.length() == 0) {
return null;
}
byte[] bytes = new byte[4];
String[] elements = text.split("\\." , -1);
try {
long l;
int i;
switch (elements.length) {
case 1:
l = Long.parseLong(elements[0]);
if ((l < 0L) || (l > 4294967295L)) {
return null;
}
bytes[0] = (byte) (int) (l >> 24 & 0xFF);
bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 2:
l = Integer.parseInt(elements[0]);
if ((l < 0L) || (l > 255L)) {
return null;
}
bytes[0] = (byte) (int) (l & 0xFF);
l = Integer.parseInt(elements[1]);
if ((l < 0L) || (l > 16777215L)) {
return null;
}
bytes[1] = (byte) (int) (l >> 16 & 0xFF);
bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 3:
for (i = 0; i < 2; ++i) {
l = Integer.parseInt(elements[i]);
if ((l < 0L) || (l > 255L)) {
return null;
}
bytes[i] = (byte) (int) (l & 0xFF);
}
l = Integer.parseInt(elements[2]);
if ((l < 0L) || (l > 65535L)) {
return null;
}
bytes[2] = (byte) (int) (l >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 4:
for (i = 0; i < 4; ++i) {
l = Integer.parseInt(elements[i]);
if ((l < 0L) || (l > 255L)) {
return null;
}
bytes[i] = (byte) (int) (l & 0xFF);
}
break;
default:
return null;
}
} catch (NumberFormatException e) {
return null;
}
return bytes;
}
/**
* 获取IP地址
*
* @return 本地IP地址
*/
public static String getHostIp() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
}
return "127.0.0.1";
}
/**
* 获取主机名
*
* @return 本地主机名
*/
public static String getHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
}
return "未知";
}
/**
* 从多级反向代理中获得第一个非unknown IP地址
*
@@ -154,7 +52,7 @@ public class IpUtils {
if (ip != null && ip.indexOf(",") > 0) {
final String[] ips = ip.trim().split(",");
for (String subIp : ips) {
if (false == isUnknown(subIp)) {
if (!isUnknown(subIp)) {
ip = subIp;
break;
}

View File

@@ -10,7 +10,6 @@ import io.jsonwebtoken.SignatureAlgorithm;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Iterator;
import java.util.Map;
/**
@@ -48,9 +47,8 @@ public class JwtAnalysisUtil {
*/
public static JwtInfo getInfoFromToken(String token, byte[] pubKey) throws Exception {
Claims body = (Claims) RsaKeyUtil.parserToken(token, pubKey).getBody();
Iterator var3 = body.entrySet().iterator();
while (var3.hasNext()) {
Map.Entry entry = (Map.Entry) var3.next();
for (Map.Entry<String, Object> stringObjectEntry : body.entrySet()) {
Map.Entry<String, Object> entry = stringObjectEntry;
if (!"sub".equals(entry.getKey())
&& !"userId".equals(entry.getKey())
&& !"phone".equals(entry.getKey())
@@ -59,7 +57,7 @@ public class JwtAnalysisUtil {
&& !"name".equals(entry.getKey())
&& !"roleName".equals(entry.getKey())
&& !"scenicId".equals(entry.getKey())
&& !"expire".equals(entry.getKey()));
&& !"expire".equals(entry.getKey())) ;
}
// convert

View File

@@ -1,5 +1,6 @@
package com.ycwl.basic.utils;
import cn.hutool.core.codec.Base64;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
@@ -8,14 +9,13 @@ import java.io.IOException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author yangchen
*/
@SuppressWarnings("ALL")
public class RsaKeyUtil {
public RsaKeyUtil() {
@@ -38,18 +38,18 @@ public class RsaKeyUtil {
KeyPair keyPair;
byte[] publicKeyBytes = (keyPair = keyPairGenerator.genKeyPair()).getPublic().getEncoded();
byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
HashMap map;
(map = new HashMap()).put("pub", publicKeyBytes);
HashMap<String, byte[]> map = new HashMap<>();
map.put("pub", publicKeyBytes);
map.put("pri", privateKeyBytes);
return map;
}
public static String toHexString(byte[] b) {
return Base64.getEncoder().encodeToString(b);
return Base64.encode(b);
}
public static final byte[] toBytes(String s) throws IOException {
return Base64.getDecoder().decode(s);
public static byte[] toBytes(String s) throws IOException {
return Base64.decode(s);
}
public static Jws<Claims> parserToken(String token, byte[] pubKey) throws Exception {
@@ -67,7 +67,7 @@ public class RsaKeyUtil {
SecureRandom secureRandom = new SecureRandom("123".getBytes());
keyPairGenerator.initialize(1024, secureRandom);
KeyPair keyPair = keyPairGenerator.genKeyPair();
System.out.println(keyPair.getPublic().getEncoded());
System.out.println(Arrays.toString(keyPair.getPublic().getEncoded()));
System.out.println("====");

View File

@@ -99,7 +99,6 @@ public class SnowFlakeUtil {
public static long convert(UniqueId uniqueId) {
long result = 0;
try {
result = 0L;
result |= uniqueId.getSequence();