zhentao 2 月之前
父節點
當前提交
31c27fc185

+ 15 - 0
demo/pom.xml

@@ -33,6 +33,21 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-validation</artifactId>
         </dependency>
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.16</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.5.13</version>
+        </dependency>
+        <dependency>
+            <groupId>org.jsoup</groupId>
+            <artifactId>jsoup</artifactId>
+            <version>1.15.3</version>
+        </dependency>
     </dependencies>
     <dependencyManagement>
         <dependencies>

+ 3 - 0
demo/src/main/java/com/zhentao/Path2.java

@@ -1,5 +1,7 @@
 package com.zhentao;
 
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -37,4 +39,5 @@ public class Path2 {
             }
         System.err.println(list);
     }
+
 }

+ 48 - 0
demo/src/main/java/com/zhentao/Python.java

@@ -0,0 +1,48 @@
+package com.zhentao;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.util.ArrayList;
+import java.util.List;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+
+public class Python {
+    public static void main(String[] args) {
+        List<String> info = getBaiduHomepageInfo("https://www.jingdong.com");
+        for (String item : info) {
+            System.out.println(item);
+        }
+    }
+    public static List<String> getBaiduHomepageInfo(String url) {
+        List<String> info = new ArrayList<>();
+        try {
+// 创建 HTTP 客户端
+            HttpClient client = HttpClient.newHttpClient ();
+// 创建 HTTP 请求并设置请求头
+            HttpRequest request = HttpRequest.newBuilder ()
+                    .uri (URI.create (url))
+                    .header ("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3")
+                    .build ();
+// 发送请求并获取响应
+            HttpResponse<String> response = client.send (request, HttpResponse.BodyHandlers.ofString ());
+            if (response.statusCode () == 200) {
+// 使用 Jsoup 解析 HTML
+                Document doc = Jsoup.parse (response.body ());
+// 这里可根据具体需求选择要提取的信息,例如提取所有链接文本
+                Elements links = doc.select ("a");
+                for (Element link : links) {
+                    info.add (link.text ());
+                }
+            }
+        } catch (IOException | InterruptedException e) {
+            e.printStackTrace ();
+        }
+        return info;
+    }
+}

+ 34 - 1
demo/src/main/java/com/zhentao/controller/ApiController.java

@@ -1,5 +1,7 @@
 package com.zhentao.controller;
 
+import cn.hutool.crypto.digest.DigestUtil;
+import com.zhentao.pojo.CeShi;
 import com.zhentao.pojo.Object1;
 import jakarta.validation.ConstraintViolation;
 import jakarta.validation.Validation;
@@ -16,6 +18,36 @@ import java.util.Set;
 @RestController
 @RequestMapping("api")
 public class ApiController {
+    public static String generateMD5Signature(String appId, String goodsId, String reqId, String reqTime, String appKey) {
+        String input = appId + goodsId + reqId + reqTime + appKey;
+//        try {
+//            // 获取 MD5 算法的 MessageDigest 实例
+//            MessageDigest md = MessageDigest.getInstance("MD5");
+//            // 将输入字符串转换为字节数组并更新到 MessageDigest 中
+//            byte[] messageDigest = md.digest(input.getBytes());
+//            StringBuilder hexString = new StringBuilder();
+//            for (byte b : messageDigest) {
+//                String hex = Integer.toHexString(0xFF & b);
+//                System.err.println(hex);
+//                if (hex.length() == 1) {
+//                    hexString.append('0');
+//                }
+//                hexString.append(hex);
+//            }
+//            // 返回生成的 MD5 签名
+//            return hexString.toString();
+//        } catch (NoSuchAlgorithmException e) {
+//            // 若 MD5 算法不可用,抛出运行时异常
+//            throw new RuntimeException(e);
+//        }
+        return DigestUtil.md5Hex(input);
+    }
+
+    // 验证签名的方法
+    public static boolean verifySignature(String appId, String goodsId, String reqId, String reqTime, String appKey, String receivedSignature) {
+        String generatedSignature = generateMD5Signature(appId, goodsId, reqId, reqTime, appKey);
+        return generatedSignature.equals(receivedSignature);
+    }
     @RequestMapping("/find1")
     public Object1 find1(@RequestBody Object1 object1,@RequestHeader("reqId") String reqId){
 
@@ -25,7 +57,8 @@ public class ApiController {
 
         if (violations.isEmpty()) {
             System.out.println("邮箱地址验证通过");
-
+            String yourAppKey = ApiController.generateMD5Signature(object1.getAppId(), object1.getGoodsId().toString(), reqId, "20250331120000", "yourAppKey");
+            System.err.println(yourAppKey);
             return object1;
         } else {
             for (ConstraintViolation<Object1> violation : violations) {

+ 88 - 0
demo/src/main/java/com/zhentao/pojo/CeShi.java

@@ -0,0 +1,88 @@
+package com.zhentao.pojo;
+
+import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.ChineseDate;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.lang.Console;
+import cn.hutool.core.util.ReUtil;
+import cn.hutool.crypto.digest.DigestUtil;
+import cn.hutool.extra.tokenizer.Result;
+import cn.hutool.extra.tokenizer.TokenizerEngine;
+import cn.hutool.extra.tokenizer.TokenizerUtil;
+import cn.hutool.extra.tokenizer.Word;
+import cn.hutool.http.HttpUtil;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Iterator;
+import java.util.List;
+
+public class CeShi {
+    // 生成 MD5 签名的方法
+    public static String generateMD5Signature(String appId, String goodsId, String reqId, String reqTime, String appKey) {
+        String input = appId + goodsId + reqId + reqTime + appKey;
+//        try {
+//            // 获取 MD5 算法的 MessageDigest 实例
+//            MessageDigest md = MessageDigest.getInstance("MD5");
+//            // 将输入字符串转换为字节数组并更新到 MessageDigest 中
+//            byte[] messageDigest = md.digest(input.getBytes());
+//            StringBuilder hexString = new StringBuilder();
+//            for (byte b : messageDigest) {
+//                String hex = Integer.toHexString(0xFF & b);
+//                System.err.println(hex);
+//                if (hex.length() == 1) {
+//                    hexString.append('0');
+//                }
+//                hexString.append(hex);
+//            }
+//            // 返回生成的 MD5 签名
+//            return hexString.toString();
+//        } catch (NoSuchAlgorithmException e) {
+//            // 若 MD5 算法不可用,抛出运行时异常
+//            throw new RuntimeException(e);
+//        }
+        return DigestUtil.md5Hex(input);
+    }
+
+    // 验证签名的方法
+    public static boolean verifySignature(String appId, String goodsId, String reqId, String reqTime, String appKey, String receivedSignature) {
+        String generatedSignature = generateMD5Signature(appId, goodsId, reqId, reqTime, appKey);
+        return generatedSignature.equals(receivedSignature);
+    }
+
+    public static void main(String[] args) {
+//        // 示例数据
+//        String appId = "123";
+//        String goodsId = "456";
+//        String reqId = "789";
+//        String reqTime = "20250331120000";
+//        String appKey = "yourAppKey";
+//
+//        // 生成签名
+//        String signature = generateMD5Signature(appId, goodsId, reqId, reqTime, appKey);
+//        System.out.println("生成的签名: " + signature);
+//        String s = "291b0ea36b97301bd20ed80ff624f24";
+//
+//        // 验证签名
+//        boolean isVerified = verifySignature(appId, goodsId, reqId, reqTime, appKey, s);
+//        System.out.println("签名验证结果: " + (isVerified ? "通过" : "不通过"));
+//        ChineseDate chineseDate = new ChineseDate(DateUtil.parseDate("2020-03-31"));
+//        String cyclicalYMD = chineseDate.getCyclicalYMD();
+//        System.err.println(cyclicalYMD);
+//        TokenizerEngine engine = TokenizerUtil.createEngine();
+///jar包
+////解析文本
+//        String text = "这两个方法的区别在于返回值";
+//        Result result = engine.parse("我吃苹果");
+////输出:这 两个 方法 的 区别 在于 返回 值
+//        String resultStr = CollUtil.join((Iterator<Word>)result, " ");
+        String listContent = HttpUtil.get("https://www.oschina.net/action/ajax/get_more_news_list?newsType=&p=2");
+//使用正则获取所有标题
+        List<String> titles = ReUtil.findAll("<span class=\"text-ellipsis\">(.*?)</span>", listContent, 1);
+//        List<String> titles = ReUtil.findAll("<span class=\"bdzm\">(.*?)</span>", listContent, 1);
+        for (String title : titles) {
+            //打印标题
+            Console.log(title);
+        }
+    }
+}

+ 1 - 1
demo/src/main/java/com/zhentao/pojo/Object1.java

@@ -14,7 +14,7 @@ public class Object1 {
     private long createTime=System.currentTimeMillis();
     @NotNull
     private Integer goodsId;
-    @NotNull
+    @NotNull(message = "price not null")
     @DecimalMin(value = "0",message = "最小是0")
     @DecimalMax(value = "9999",message = "最大是9999")
     private BigDecimal price;

+ 9 - 0
demo/src/main/java/com/zhentao/vo/ResultVo.java

@@ -0,0 +1,9 @@
+package com.zhentao.vo;
+
+public class ResultVo <T>{
+    private  T data;
+    private String message;
+    private int code;
+
+
+}