reghao 5 月之前
父节点
当前提交
feadfa5a5c

+ 30 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/clazz/ClassUtil.java

@@ -1,5 +1,6 @@
 package cn.reghao.jutil.jdk.clazz;
 
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 
 /**
@@ -7,6 +8,35 @@ import java.lang.reflect.Method;
  * @date 2023-02-21 10:06:26
  */
 public class ClassUtil {
+    public static Object getObject(Class<?> clazz, String[] strs) throws Exception {
+        Field[] fields = clazz.getDeclaredFields();
+        if (strs.length != fields.length) {
+            return null;
+        }
+
+        Object object = clazz.getDeclaredConstructor().newInstance();
+        for (int i = 0; i < fields.length; i++) {
+            Class<?> clazzType = fields[i].getType();
+            if (clazzType.equals(String.class)) {
+                fields[i].setAccessible(true);
+                fields[i].set(object, strs[i]);
+            } else {
+                Object result;
+                if (clazzType.equals(Long.class)) {
+                    result = Long.parseLong(strs[i]);
+                } else if (clazzType.equals(Integer.class)) {
+                    result = Integer.parseInt(strs[i]);
+                } else {
+                    result = Double.parseDouble(strs[i]);
+                }
+
+                fields[i].setAccessible(true);
+                fields[i].set(object, result);
+            }
+        }
+        return object;
+    }
+
     public static Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes) {
         try {
             return clazz.getMethod(methodName, paramTypes);

+ 157 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/file/CaptchaUtil.java

@@ -0,0 +1,157 @@
+package cn.reghao.jutil.jdk.file;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.geom.CubicCurve2D;
+import java.awt.geom.QuadCurve2D;
+import java.awt.image.BufferedImage;
+import java.awt.image.RenderedImage;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.security.SecureRandom;
+import java.util.Random;
+
+/**
+ * @author reghao
+ * @date 2021-12-05 03:35:30
+ */
+public class CaptchaUtil {
+    // 常用颜色
+    private static final int[][] COLOR = {{0, 135, 255}, {51, 153, 51}, {255, 102, 102}, {255, 153, 0}, {153, 102, 0}, 
+            {153, 102, 153}, {51, 153, 153}, {102, 102, 255}, {0, 102, 204}, {204, 51, 51}, {0, 153, 204}, {0, 51, 102}};
+    private static final Random random = new SecureRandom();
+
+    public static ByteArrayOutputStream captchaWithDisturb(String randomStr, int width, int height) throws IOException {
+        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+        Graphics2D g2d = (Graphics2D) bi.getGraphics();
+        // 填充背景
+        g2d.setColor(Color.WHITE);
+        g2d.fillRect(0, 0, width, height);
+        // 抗锯齿
+        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+        // 画干扰圆
+        drawOval(2, null, g2d, width, height);
+        // 设置线条特征
+        g2d.setStroke(new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
+        // 画干扰线
+        drawLine(1, null, g2d, width, height);
+        // 画贝塞尔曲线
+        drawBesselLine(1, null, g2d, width, height);
+
+        FontMetrics fontMetrics = g2d.getFontMetrics();
+        // 每一个字符所占的宽度
+        int fW = width / randomStr.length();
+        // 字符的左右边距
+        int fSp = (fW - (int) fontMetrics.getStringBounds("W", g2d).getWidth()) / 2;
+        for (int i = 0; i < randomStr.length(); i++) {
+            g2d.setColor(color());
+            // 文字的纵坐标
+            int fY = height - ((height - (int) fontMetrics.getStringBounds(String.valueOf(randomStr.charAt(i)), g2d).getHeight()) >> 1);
+            g2d.drawString(String.valueOf(randomStr.charAt(i)), i * fW + fSp + 3, fY - 3);
+        }
+        g2d.dispose();
+
+        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+        ImageIO.write((RenderedImage) bi, "jpg", byteArrayOutputStream);
+        return byteArrayOutputStream;
+    }
+
+    /**
+     * 随机画干扰圆
+     *
+     * @param num   数量
+     * @param color 颜色
+     * @param g     Graphics2D
+     */
+    private static void drawOval(int num, Color color, Graphics2D g, int width, int height) {
+        for (int i = 0; i < num; i++) {
+            g.setColor(color == null ? color() : color);
+            int w = 5 + num(10);
+            g.drawOval(num(width - 25), num(height - 15), w, w);
+        }
+    }
+
+    /**
+     * 产生两个数之间的随机数
+     *
+     * @param min 最小值
+     * @param max 最大值
+     * @return 随机数
+     */
+    private static int num(int min, int max) {
+        return min + random.nextInt(max - min);
+    }
+
+    /**
+     * 产生0-num的随机数,不包括num
+     *
+     * @param num 最大值
+     * @return 随机数
+     */
+    private static int num(int num) {
+        return random.nextInt(num);
+    }
+
+    /**
+     * 随机获取常用颜色
+     *
+     * @return 随机颜色
+     */
+    private static Color color() {
+        int[] color = COLOR[num(COLOR.length)];
+        int r = color[0];
+        int g = color[1];
+        int b = color[2];
+        return new Color(r, g, b);
+    }
+
+    /**
+     * 随机画干扰线
+     *
+     * @param num   数量
+     * @param color 颜色
+     * @param g     Graphics2D
+     */
+    private static void drawLine(int num, Color color, Graphics2D g, int width, int height) {
+        for (int i = 0; i < num; i++) {
+            g.setColor(color == null ? color() : color);
+            int x1 = num(-10, width - 10);
+            int y1 = num(5, height - 5);
+            int x2 = num(10, width + 10);
+            int y2 = num(2, height - 2);
+            g.drawLine(x1, y1, x2, y2);
+        }
+    }
+
+    /**
+     * 随机画贝塞尔曲线
+     *
+     * @param num   数量
+     * @param color 颜色
+     * @param g     Graphics2D
+     */
+    private static void drawBesselLine(int num, Color color, Graphics2D g, int width, int height) {
+        for (int i = 0; i < num; i++) {
+            g.setColor(color == null ? color() : color);
+            int x1 = 5, y1 = num(5, height / 2);
+            int x2 = width - 5, y2 = num(height / 2, height - 5);
+            int ctrlx = num(width / 4, width / 4 * 3), ctrly = num(5, height - 5);
+            if (num(2) == 0) {
+                int ty = y1;
+                y1 = y2;
+                y2 = ty;
+            }
+            // 二阶贝塞尔曲线
+            if (num(2) == 0) {
+                QuadCurve2D shape = new QuadCurve2D.Double();
+                shape.setCurve(x1, y1, ctrlx, ctrly, x2, y2);
+                g.draw(shape);
+            } else {
+                // 三阶贝塞尔曲线
+                int ctrlx1 = num(width / 4, width / 4 * 3), ctrly1 = num(5, height - 5);
+                CubicCurve2D shape = new CubicCurve2D.Double(x1, y1, ctrlx, ctrly, ctrlx1, ctrly1, x2, y2);
+                g.draw(shape);
+            }
+        }
+    }
+}

+ 31 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/math/Calculator.java

@@ -0,0 +1,31 @@
+package cn.reghao.jutil.jdk.math;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
+/**
+ * @author reghao
+ * @date 2024-10-22 17:09:44
+ */
+public class Calculator {
+    public static double getPercentage(long total, long avail) {
+        long used = total - avail;
+        BigDecimal bigDecimal1 = new BigDecimal(total);
+        BigDecimal bigDecimal2 = new BigDecimal(used);
+        BigDecimal result = bigDecimal2.divide(bigDecimal1, 4, RoundingMode.DOWN);
+        return result.multiply(new BigDecimal(100)).setScale(2, RoundingMode.DOWN).doubleValue();
+    }
+
+    public static double divide(long d1, long d2) {
+        int scale = 2;
+        BigDecimal b1 = new BigDecimal(d1);
+        BigDecimal b2 = new BigDecimal(d2);
+        BigDecimal result = b1.divide(b2, scale, RoundingMode.HALF_UP);
+        double doubleValue = result.doubleValue();
+        /*DecimalFormat df = new DecimalFormat("#.##");
+        df.setRoundingMode(RoundingMode.HALF_UP);
+        String result2 = df.format(d1/d2);
+        System.out.println(result2);*/
+        return doubleValue;
+    }
+}

+ 65 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/result/AppVersion.java

@@ -0,0 +1,65 @@
+package cn.reghao.jutil.jdk.result;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Serializable;
+import java.util.Properties;
+
+/**
+ * @author reghao
+ * @date 2022-05-11 22:29:49
+ */
+public class AppVersion implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    private final String repo;
+    private final String branch;
+    private final String commitId;
+    private final String commitTime;
+    private final String buildTime;
+
+    private AppVersion(String repo, String branch, String commitId, String commitTime, String builtTime) {
+        this.repo = repo;
+        this.branch = branch;
+        this.commitId = commitId;
+        this.commitTime = commitTime;
+        this.buildTime = builtTime;
+    }
+
+    public String getRepo() {
+        return repo;
+    }
+
+    public String getBranch() {
+        return branch;
+    }
+
+    public String getCommitId() {
+        return commitId;
+    }
+
+    public String getCommitTime() {
+        return commitTime;
+    }
+
+    public String getBuildTime() {
+        return buildTime;
+    }
+
+    public static AppVersion getVersion() {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+        try {
+            // 读取 src/main/resources 目录下的文件
+            InputStream inputStream = classLoader.getResourceAsStream("git.properties");
+            Properties props = new Properties();
+            props.load(inputStream);String repo = props.get("repo").toString();
+            String branch = props.get("branch").toString();
+            String commitId = props.get("commitId").toString();
+            String commitTime = props.get("commitTime").toString();
+            String buildTime = props.get("buildTime").toString();
+            return new AppVersion(repo, branch, commitId, commitTime, buildTime);
+        } catch (IOException ignore) {
+        }
+        return null;
+    }
+}

+ 19 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/result/NotAvailable.java

@@ -0,0 +1,19 @@
+package cn.reghao.jutil.jdk.result;
+
+/**
+ * @author reghao
+ * @date 2021-11-08 16:53:59
+ */
+public enum NotAvailable {
+    na("N/A");
+
+    private String desc;
+
+    NotAvailable(String desc) {
+        this.desc = desc;
+    }
+
+    public String getDesc() {
+        return desc;
+    }
+}

+ 0 - 1
pom.xml

@@ -12,7 +12,6 @@
         <module>jdk</module>
         <module>tool</module>
         <module>web</module>
-        <module>validator</module>
         <module>media</module>
     </modules>
 

+ 0 - 39
validator/pom.xml

@@ -1,39 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-    <parent>
-        <artifactId>jutil</artifactId>
-        <groupId>cn.reghao.jutil</groupId>
-        <version>1.0.0-SNAPSHOT</version>
-    </parent>
-    <modelVersion>4.0.0</modelVersion>
-
-    <artifactId>validator</artifactId>
-
-    <properties>
-        <maven.compiler.source>11</maven.compiler.source>
-        <maven.compiler.target>11</maven.compiler.target>
-    </properties>
-
-    <dependencies>
-        <dependency>
-            <groupId>cn.reghao.jutil</groupId>
-            <artifactId>jdk</artifactId>
-            <version>1.0.0-SNAPSHOT</version>
-        </dependency>
-
-        <dependency>
-            <groupId>javax.validation</groupId>
-            <artifactId>validation-api</artifactId>
-            <version>2.0.1.Final</version>
-        </dependency>
-    </dependencies>
-
-    <distributionManagement>
-        <repository>
-            <id>maven-local-hosted</id>
-            <url>http://nexus.reghao.cn/repository/maven-local-hosted/</url>
-        </repository>
-    </distributionManagement>
-</project>

+ 13 - 0
web/pom.xml

@@ -29,12 +29,25 @@
     </dependencyManagement>
 
     <dependencies>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+            <version>1.18.16</version>
+        </dependency>
+
         <dependency>
             <groupId>cn.reghao.jutil</groupId>
             <artifactId>jdk</artifactId>
             <version>1.0.0-SNAPSHOT</version>
         </dependency>
 
+        <dependency>
+            <groupId>javax.validation</groupId>
+            <artifactId>validation-api</artifactId>
+            <version>2.0.1.Final</version>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>

+ 48 - 0
web/src/main/java/cn/reghao/jutil/web/ServletUtil.java

@@ -1,6 +1,7 @@
 package cn.reghao.jutil.web;
 
 import cn.reghao.jutil.jdk.serializer.JsonConverter;
+import cn.reghao.jutil.web.log.GatewayLog;
 import org.springframework.util.StringUtils;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
@@ -12,6 +13,7 @@ import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -153,4 +155,50 @@ public class ServletUtil {
     private static ServletRequestAttributes getServletRequest(){
         return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
     }
+
+    public static GatewayLog getGatewayLog() {
+        HttpServletRequest request = ServletUtil.getRequest();
+        return getGatewayLog(request);
+    }
+
+    public static GatewayLog getGatewayLog(HttpServletRequest request) {
+        String uri = request.getRequestURI();
+        String method = request.getMethod();
+        String host = request.getHeader("host");
+        String referer = request.getHeader("referer");
+        String userAgent = request.getHeader("user-agent");
+        String remoteAddr = request.getRemoteAddr();
+        int remotePort = request.getRemotePort();
+
+        Map<String, String> requestHeaders = new HashMap<>();
+        Enumeration<String> headerNames = request.getHeaderNames();
+        while (headerNames.hasMoreElements()) {
+            String headerName = headerNames.nextElement();
+            String headerValue = request.getHeader(headerName);
+            requestHeaders.put(headerName, headerValue);
+        }
+
+        String requestId = (String) request.getAttribute("x-request-id");
+        long requestTime = (Long) request.getAttribute("x-request-time");
+        String targetRoute = "";
+        String targetService = "";
+        String requestUrl = request.getRequestURI();
+        String requestMethod = request.getMethod();
+        String requestBody = "";
+
+        int statusCode = 200;
+        Map<String, String> responseHeaders = new HashMap<>();
+        String responseBody = "";
+        long responseTime = 0L;
+        long executeTime = 0L;
+
+        String realRemoteAddr = requestHeaders.get("X-Real-Remote");
+        if (realRemoteAddr != null && !realRemoteAddr.isBlank()) {
+            remoteAddr = realRemoteAddr;
+        }
+
+        GatewayLog gatewayLog = new GatewayLog(requestId, requestTime, requestUrl, requestMethod, requestHeaders,
+                remoteAddr, remotePort, statusCode, responseHeaders, responseTime);
+        return gatewayLog;
+    }
 }

+ 27 - 0
web/src/main/java/cn/reghao/jutil/web/log/AppLog.java

@@ -0,0 +1,27 @@
+package cn.reghao.jutil.web.log;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+
+import java.io.Serializable;
+
+/**
+ * @author reghao
+ * @date 2025-07-18 11:33:46
+ */
+@AllArgsConstructor
+@NoArgsConstructor
+@Getter
+public class AppLog implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    private String requestId;
+    private String app;
+    private String host;
+    private long timestamp;
+    private String level;
+    private String thread;
+    private String logger;
+    private String message;
+}

+ 70 - 0
web/src/main/java/cn/reghao/jutil/web/log/GatewayLog.java

@@ -0,0 +1,70 @@
+package cn.reghao.jutil.web.log;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.io.Serializable;
+import java.util.Map;
+
+/**
+ * @author reghao
+ * @date 2024-11-20 16:40:24
+ */
+@Setter
+@Getter
+public class GatewayLog implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    private String requestId;
+    // ms
+    private long requestTime;
+    private String targetRoute;
+    private String targetService;
+    private String requestUrl;
+    private String requestMethod;
+    private Map<String, String> requestHeaders;
+    private String requestBody;
+    private long requestBytes;
+    private String remoteAddr;
+    private int remotePort;
+    private int statusCode;
+    private Map<String, String> responseHeaders;
+    private String responseBody;
+    private long responseBytes;
+    // ms
+    private long responseTime;
+    // ms
+    private long executeTime;
+    private String fingerprint;
+
+    public GatewayLog() {
+        this.targetRoute = "";
+        this.targetService = "";
+        this.requestBody = "";
+        this.requestBytes = 0;
+        this.remoteAddr = "";
+        this.remotePort = 0;
+        this.responseBody = "";
+        this.responseBytes = 0;
+        this.fingerprint = "";
+    }
+
+    public GatewayLog(String requestId, long requestTime, String requestUrl, String requestMethod, Map<String, String> requestHeaders,
+                      String remoteAddr, int remotePort, int statusCode, Map<String, String> responseHeaders, long responseTime) {
+        this.requestId = requestId;
+        this.requestTime = requestTime;
+        this.targetRoute = "";
+        this.targetService = "";
+        this.requestUrl = requestUrl;
+        this.requestMethod = requestMethod;
+        this.requestHeaders = requestHeaders;
+        this.requestBody = "";
+        this.remoteAddr = remoteAddr;
+        this.remotePort = remotePort;
+        this.statusCode = statusCode;
+        this.responseHeaders = responseHeaders;
+        this.responseBody = "";
+        this.responseTime = responseTime;
+        this.executeTime = responseTime-requestTime;
+    }
+}

+ 30 - 0
web/src/main/java/cn/reghao/jutil/web/log/NginxLog.java

@@ -0,0 +1,30 @@
+package cn.reghao.jutil.web.log;
+
+import com.google.gson.annotations.SerializedName;
+import lombok.Getter;
+
+import java.io.Serializable;
+
+/**
+ * @author reghao
+ * @date 2023-11-07 14:58:07
+ */
+@Getter
+public class NginxLog implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    @SerializedName("time_iso8601") private String timeIso8601;
+    @SerializedName("remote_addr") private String remoteAddr;
+    private String request;
+    private Integer status;
+    @SerializedName("request_method") private String requestMethod;
+    @SerializedName("body_bytes_sent") private Integer bodyBytesSent;
+    @SerializedName("request_time") private Double requestTime;
+    @SerializedName("upstream_response_time") private String upstreamResponseTime;
+    @SerializedName("upstream_addr") private String upstreamAddr;
+    private String host;
+    private String url;
+    @SerializedName("http_x_forwarded_for") private String httpXForwardedFor;
+    @SerializedName("http_referer") private String httpReferer;
+    @SerializedName("http_user_agent") private String httpUserAgent;
+}

+ 1 - 1
validator/src/main/java/cn/reghao/jutil/validator/EnumValidator.java → web/src/main/java/cn/reghao/jutil/web/validator/EnumValidator.java

@@ -1,4 +1,4 @@
-package cn.reghao.jutil.validator;
+package cn.reghao.jutil.web.validator;
 
 import cn.reghao.jutil.jdk.clazz.ClassUtil;
 

+ 1 - 1
validator/src/main/java/cn/reghao/jutil/validator/ValidEnum.java → web/src/main/java/cn/reghao/jutil/web/validator/ValidEnum.java

@@ -1,4 +1,4 @@
-package cn.reghao.jutil.validator;
+package cn.reghao.jutil.web.validator;
 
 import javax.validation.Constraint;
 import javax.validation.Payload;