X723595506 1 месяц назад
Сommit
9dfef17a44
31 измененных файлов с 553 добавлено и 0 удалено
  1. 89 0
      day0329/pom.xml
  2. 17 0
      day0329/src/main/java/cn/zhentao/Day0329Application.java
  3. 23 0
      day0329/src/main/java/cn/zhentao/controller/GlobalExceptionHandler.java
  4. 84 0
      day0329/src/main/java/cn/zhentao/controller/Test.java
  5. 45 0
      day0329/src/main/java/cn/zhentao/pojo/Parameter.java
  6. 9 0
      day0329/src/main/java/cn/zhentao/pojo/RequestOne.java
  7. 9 0
      day0329/src/main/java/cn/zhentao/pojo/RequestTwo.java
  8. 7 0
      day0329/src/main/resources/application.yml
  9. 38 0
      day0329/src/test/java/cn/zhentao/AppTest.java
  10. 7 0
      day0329/target/classes/application.yml
  11. BIN
      day0329/target/classes/cn/zhentao/Day0329Application.class
  12. BIN
      day0329/target/classes/cn/zhentao/controller/GlobalExceptionHandler.class
  13. BIN
      day0329/target/classes/cn/zhentao/controller/Test.class
  14. BIN
      day0329/target/classes/cn/zhentao/pojo/Parameter.class
  15. BIN
      day0329/target/classes/cn/zhentao/pojo/RequestOne.class
  16. BIN
      day0329/target/classes/cn/zhentao/pojo/RequestTwo.class
  17. BIN
      day0329/target/day0329-1.0-SNAPSHOT.jar
  18. BIN
      day0329/target/day0329-1.0-SNAPSHOT.jar.original
  19. 3 0
      day0329/target/maven-archiver/pom.properties
  20. 6 0
      day0329/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst
  21. 6 0
      day0329/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
  22. 1 0
      day0329/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst
  23. 1 0
      day0329/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst
  24. 5 0
      day0329/target/surefire-reports/2025-03-30T20-32-32_393.dumpstream
  25. 9 0
      day0329/target/surefire-reports/TEST-cn.zhentao.AppTest.xml
  26. 4 0
      day0329/target/surefire-reports/cn.zhentao.AppTest.txt
  27. BIN
      day0329/target/test-classes/cn/zhentao/AppTest.class
  28. 30 0
      day0331/pom.xml
  29. 122 0
      day0331/src/main/java/cn/zhentao/Test.java
  30. 38 0
      day0331/src/test/java/cn/zhentao/AppTest.java
  31. BIN
      day0331/target/classes/cn/zhentao/Test.class

+ 89 - 0
day0329/pom.xml

@@ -0,0 +1,89 @@
+<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">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>cn.zhentao</groupId>
+  <artifactId>day0329</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>day0329</name>
+  <url>http://maven.apache.org</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.projectlombok</groupId>
+      <artifactId>lombok</artifactId>
+      <version>1.18.30</version>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-web</artifactId>
+      <version>2.3.2.RELEASE</version>
+    </dependency>
+    <dependency>
+      <groupId>jakarta.validation</groupId>
+      <artifactId>jakarta.validation-api</artifactId>
+      <version>2.0.2</version>
+    </dependency>
+
+    <dependency>
+      <groupId>cn.hutool</groupId>
+      <artifactId>hutool-all</artifactId>
+      <version>5.8.16</version>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-validation</artifactId>
+      <version>2.7.18</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <!-- Maven Compiler Plugin -->
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>3.8.1</version>
+        <configuration>
+          <source>1.8</source>
+          <target>1.8</target>
+          <encoding>UTF-8</encoding>
+        </configuration>
+      </plugin>
+
+      <!-- Spring Boot Maven Plugin -->
+      <plugin>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-maven-plugin</artifactId>
+        <!-- 明确指定Spring Boot版本,假设这里是2.7.10,可按需修改 -->
+        <version>2.3.12.RELEASE</version>
+        <configuration>
+          <!-- 请替换为实际包含main方法的主类全限定名 -->
+          <mainClass>cn.zhentao.Day0329Application</mainClass>
+          <skip>false</skip>
+        </configuration>
+        <executions>
+          <execution>
+            <id>repackage</id>
+            <goals>
+              <goal>repackage</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+</project>

+ 17 - 0
day0329/src/main/java/cn/zhentao/Day0329Application.java

@@ -0,0 +1,17 @@
+package cn.zhentao;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * Hello world!
+ *
+ */
+@SpringBootApplication
+public class Day0329Application
+{
+    public static void main( String[] args )
+    {
+        SpringApplication.run(Day0329Application.class,args);
+    }
+}

+ 23 - 0
day0329/src/main/java/cn/zhentao/controller/GlobalExceptionHandler.java

@@ -0,0 +1,23 @@
+package cn.zhentao.controller;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@RestControllerAdvice
+public class GlobalExceptionHandler {
+
+    @ExceptionHandler(MethodArgumentNotValidException.class)
+    public ResponseEntity<Map<String, String>> handleValidationExceptions(
+            MethodArgumentNotValidException ex) {
+        Map<String, String> errors = new HashMap<>();
+        ex.getBindingResult().getFieldErrors().forEach(error ->
+                errors.put(error.getField(), error.getDefaultMessage()));
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors);
+    }
+}

+ 84 - 0
day0329/src/main/java/cn/zhentao/controller/Test.java

@@ -0,0 +1,84 @@
+package cn.zhentao.controller;
+
+import cn.hutool.crypto.digest.DigestUtil;
+import cn.zhentao.pojo.RequestOne;
+import cn.zhentao.pojo.RequestTwo;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+
+@RestController
+@RequestMapping("/api")
+public class Test {
+    @PostMapping("/interfaceOne")
+    public ResponseEntity<String> interfaceOne(
+            @RequestHeader("reqId") String reqId,
+            @Valid @RequestBody RequestOne request) {
+
+
+        String appKey = getAppKeyByAppId(request.getAppId());
+
+// 2. 生成服务端签名
+        String rawSign = request.getAppId() +
+                request.getGoodsId() +
+                reqId +
+                request.getReqTime() +
+                appKey;
+
+        String serverSign = DigestUtil.md5Hex(rawSign);
+
+// 生成签名:appId + goodsId + reqId + reqTime + appKey
+        String a = request.getAppId() + request.getGoodsId() + reqId + request.getReqTime() +  "1234567890";
+        String sign = DigestUtil.md5Hex(a);
+        request.setSign(sign);
+
+
+        if (!serverSign.equals(request.getSign())) {
+            return ResponseEntity.ok("无效的 appId"+reqId);
+        }
+
+
+        return ResponseEntity.ok("接口一调用成功, reqId: " + reqId);
+    }
+
+    @GetMapping("/interfaceTwo")
+    public ResponseEntity<String> interfaceTwo(
+            @RequestHeader("reqId") String reqId,
+            RequestTwo request) {
+
+
+        String appKey = getAppKeyByAppId(request.getAppId());
+
+// 2. 生成服务端签名
+        String rawSign = request.getAppId() +
+                request.getGoodsId() +
+                reqId +
+                request.getReqTime() +
+                appKey;
+
+        String serverSign = DigestUtil.md5Hex(rawSign);
+
+// 生成签名:appId + goodsId + reqId + reqTime + appKey
+        String a = request.getAppId() + request.getGoodsId() + reqId + request.getReqTime() +  "1234567890";
+        String sign = DigestUtil.md5Hex(a);
+        request.setSign(sign);
+        System.err.println(serverSign);
+
+
+        if (!serverSign.equals(request.getSign())) {
+            return ResponseEntity.ok("无效的 appId"+reqId);
+        }
+
+
+        return ResponseEntity.ok("接口二调用成功, reqId: " + reqId);
+    }
+
+    // 新增方法:根据 appId 获取 appKey
+    private String getAppKeyByAppId(String appId) {
+        if ("test_app".equals(appId)) {
+            return "1234567890";
+        }
+        throw new IllegalArgumentException("无效的 appId");
+    }
+}

+ 45 - 0
day0329/src/main/java/cn/zhentao/pojo/Parameter.java

@@ -0,0 +1,45 @@
+package cn.zhentao.pojo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import lombok.Data;
+
+import javax.validation.constraints.*;
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+@Data
+public class Parameter implements Serializable {
+    //应用id
+    @NotBlank(message = "appId不能为空")
+    private String appId;
+    //签名
+    @NotNull(message = "签名不能为空")
+    private String sign;
+    //时间戳
+    @NotNull(message = "reqTime不能为空")
+    @JsonSerialize(using = ToStringSerializer.class)
+    private Long reqTime;
+    //商品id
+    @NotNull(message = "goodsId不能为空")
+    @JsonFormat(shape = JsonFormat.Shape.STRING)
+    private Long goodsId;
+    //数量
+    @NotNull(message = "amount不能为空")
+    @DecimalMin(value = "1",message = "最小值1")
+    private Integer amount;
+    //价格
+    @NotNull(message = "price不能为空")
+    @DecimalMin(value = "0",message = "最小值0")
+    @DecimalMax(value = "9999",message = "最大值9999")
+    private BigDecimal price;
+    //手机号
+    @NotBlank(message = "mobile不能为空")
+    @Pattern(regexp = "^((13[0-9])|(14[0-9])|(15[0-9])|(166)|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$",message = "手机号码格式错误")
+    private String mobile;
+    //日期
+    @NotBlank(message = "nowDate不能为空")
+//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8",shape = JsonFormat.Shape.STRING)
+    private String nowDate;
+}

+ 9 - 0
day0329/src/main/java/cn/zhentao/pojo/RequestOne.java

@@ -0,0 +1,9 @@
+package cn.zhentao.pojo;
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+@Data
+@EqualsAndHashCode(callSuper = true) //自动生成equals()和hashcode()方法
+public class RequestOne extends Parameter{
+}

+ 9 - 0
day0329/src/main/java/cn/zhentao/pojo/RequestTwo.java

@@ -0,0 +1,9 @@
+package cn.zhentao.pojo;
+
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+@Data
+@EqualsAndHashCode(callSuper = true) //自动生成equals()和hashcode()方法
+public class RequestTwo extends Parameter{
+}

+ 7 - 0
day0329/src/main/resources/application.yml

@@ -0,0 +1,7 @@
+server:
+  port: 8080
+
+logging:
+  level:
+    cn:
+      zhentao: debug

+ 38 - 0
day0329/src/test/java/cn/zhentao/AppTest.java

@@ -0,0 +1,38 @@
+package cn.zhentao;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest 
+    extends TestCase
+{
+    /**
+     * Create the test case
+     *
+     * @param testName name of the test case
+     */
+    public AppTest( String testName )
+    {
+        super( testName );
+    }
+
+    /**
+     * @return the suite of tests being tested
+     */
+    public static Test suite()
+    {
+        return new TestSuite( AppTest.class );
+    }
+
+    /**
+     * Rigourous Test :-)
+     */
+    public void testApp()
+    {
+        assertTrue( true );
+    }
+}

+ 7 - 0
day0329/target/classes/application.yml

@@ -0,0 +1,7 @@
+server:
+  port: 8080
+
+logging:
+  level:
+    cn:
+      zhentao: debug

BIN
day0329/target/classes/cn/zhentao/Day0329Application.class


BIN
day0329/target/classes/cn/zhentao/controller/GlobalExceptionHandler.class


BIN
day0329/target/classes/cn/zhentao/controller/Test.class


BIN
day0329/target/classes/cn/zhentao/pojo/Parameter.class


BIN
day0329/target/classes/cn/zhentao/pojo/RequestOne.class


BIN
day0329/target/classes/cn/zhentao/pojo/RequestTwo.class


BIN
day0329/target/day0329-1.0-SNAPSHOT.jar


BIN
day0329/target/day0329-1.0-SNAPSHOT.jar.original


+ 3 - 0
day0329/target/maven-archiver/pom.properties

@@ -0,0 +1,3 @@
+artifactId=day0329
+groupId=cn.zhentao
+version=1.0-SNAPSHOT

+ 6 - 0
day0329/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst

@@ -0,0 +1,6 @@
+cn\zhentao\controller\GlobalExceptionHandler.class
+cn\zhentao\controller\Test.class
+cn\zhentao\pojo\RequestTwo.class
+cn\zhentao\pojo\Parameter.class
+cn\zhentao\Day0329Application.class
+cn\zhentao\pojo\RequestOne.class

+ 6 - 0
day0329/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst

@@ -0,0 +1,6 @@
+D:\2407\code\2407-a_cloud\day0329\src\main\java\cn\zhentao\Day0329Application.java
+D:\2407\code\2407-a_cloud\day0329\src\main\java\cn\zhentao\controller\GlobalExceptionHandler.java
+D:\2407\code\2407-a_cloud\day0329\src\main\java\cn\zhentao\pojo\Parameter.java
+D:\2407\code\2407-a_cloud\day0329\src\main\java\cn\zhentao\pojo\RequestTwo.java
+D:\2407\code\2407-a_cloud\day0329\src\main\java\cn\zhentao\controller\Test.java
+D:\2407\code\2407-a_cloud\day0329\src\main\java\cn\zhentao\pojo\RequestOne.java

+ 1 - 0
day0329/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst

@@ -0,0 +1 @@
+cn\zhentao\AppTest.class

+ 1 - 0
day0329/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst

@@ -0,0 +1 @@
+D:\2407\code\2407-a_cloud\day0329\src\test\java\cn\zhentao\AppTest.java

+ 5 - 0
day0329/target/surefire-reports/2025-03-30T20-32-32_393.dumpstream

@@ -0,0 +1,5 @@
+# Created at 2025-03-30T20:32:32.716
+Boot Manifest-JAR contains absolute paths in classpath 'D:\repository\org\apache\maven\surefire\surefire-booter\3.0.0\surefire-booter-3.0.0.jar'
+Hint: <argLine>-Djdk.net.URLClassPath.disableClassPathURLCheck=true</argLine>
+'other' has different root
+

Разница между файлами не показана из-за своего большого размера
+ 9 - 0
day0329/target/surefire-reports/TEST-cn.zhentao.AppTest.xml


+ 4 - 0
day0329/target/surefire-reports/cn.zhentao.AppTest.txt

@@ -0,0 +1,4 @@
+-------------------------------------------------------------------------------
+Test set: cn.zhentao.AppTest
+-------------------------------------------------------------------------------
+Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.008 s - in cn.zhentao.AppTest

BIN
day0329/target/test-classes/cn/zhentao/AppTest.class


+ 30 - 0
day0331/pom.xml

@@ -0,0 +1,30 @@
+<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">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>cn.zhentao</groupId>
+  <artifactId>day0331</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>day0331</name>
+  <url>http://maven.apache.org</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>cn.hutool</groupId>
+      <artifactId>hutool-all</artifactId>
+      <version>5.8.16</version>
+    </dependency>
+  </dependencies>
+</project>

+ 122 - 0
day0331/src/main/java/cn/zhentao/Test.java

@@ -0,0 +1,122 @@
+package cn.zhentao;
+
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.json.JSONUtil;
+
+import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+public class Test {
+
+    // 配置参数(需替换为实际值)
+    private static final String BASE_URL = "http://localhost:8080/api";
+    private static final String APP_KEY = "1234567890";
+    private static final String APP_ID = "test_app";
+
+    public static void main(String[] args) {
+        // 调用接口一(POST请求)
+        callInterfaceOne();
+
+        // 调用接口二(GET请求)
+        callInterfaceTwo();
+    }
+
+    /**
+     * 调用接口一(POST /interfaceOne)
+     */
+    private static void callInterfaceOne() {
+        // 请求参数
+        String reqId = "req_" + System.currentTimeMillis();
+        Long goodsId = 10001L;
+        Long reqTime = System.currentTimeMillis();
+
+        // 生成签名
+        String sign = generateMd5Sign(APP_ID, goodsId, reqId, reqTime, APP_KEY);
+        System.err.println(sign);
+        // 构造请求体
+        Map<String, Object> requestBody = new HashMap<String, Object>();
+        requestBody.put("appId", APP_ID);
+        requestBody.put("sign", sign);
+        requestBody.put("goodsId", goodsId);
+        requestBody.put("reqTime", reqTime);
+        requestBody.put("amount", 2);
+        requestBody.put("price", new BigDecimal("199.99"));
+        requestBody.put("mobile", "13800138000");
+        requestBody.put("nowDate", new Date());
+
+        // 请求头
+        Map<String, String> headers = new HashMap<String, String>();
+        headers.put("reqId", reqId);
+        headers.put("Content-Type", "application/json");
+
+        // 发送请求
+        HttpResponse response = HttpRequest.post(BASE_URL + "/interfaceOne")
+                .addHeaders(headers)
+                .body(JSONUtil.toJsonStr(requestBody))
+                .execute();
+
+        System.out.println("=== 接口一调用结果 ===");
+        System.out.println("状态码: " + response.getStatus());
+        System.out.println("响应体: " + response.body());
+    }
+
+    /**
+     * 调用接口二(GET /interfaceTwo)
+     */
+    private static void callInterfaceTwo() {
+        // 请求参数
+        String reqId = "req_" + (System.currentTimeMillis() + 1);
+        Long goodsId = 10002L;
+        Long reqTime = System.currentTimeMillis();
+
+        // 生成签名
+        String sign = generateMd5Sign(APP_ID, goodsId, reqId, reqTime, APP_KEY);
+
+        // GET请求参数
+        Map<String, Object> params = new HashMap<String, Object>();
+        params.put("appId", APP_ID);
+        params.put("sign", sign);
+        params.put("goodsId", goodsId);
+        params.put("reqTime", reqTime);
+        params.put("amount", 1);
+        params.put("price", new BigDecimal("99.50"));
+        params.put("mobile", "13900139000");
+        params.put("nowDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
+
+        // 请求头
+        Map<String, String> headers = new HashMap<String, String>();
+        headers.put("reqId", reqId);
+
+        // 发送请求
+        HttpResponse response = HttpRequest.get(BASE_URL + "/interfaceTwo")
+                .addHeaders(headers)
+                .form( params)
+                .execute();
+
+        System.out.println("\n=== 接口二调用结果 ===");
+        System.out.println("状态码: " + response.getStatus());
+        System.out.println("响应体: " + response.body());
+    }
+
+    /**
+     * 生成MD5签名
+     */
+    private static String generateMd5Sign(String appId, Long goodsId, String reqId, Long reqTime, String appKey) {
+        String rawString = appId + goodsId + reqId + reqTime + appKey;
+        return cn.hutool.crypto.digest.DigestUtil.md5Hex(rawString);
+    }
+
+    /**
+     * 验证签名(模拟服务端返回的签名验证)
+     */
+    private static boolean verifySign(String responseBody, String appId, Long goodsId, String reqId, Long reqTime, String appKey) {
+        // 实际项目中需要解析响应体获取服务端返回的签名
+        // 这里模拟验证过程
+        String expectedSign = generateMd5Sign(appId, goodsId, reqId, reqTime, appKey);
+        return responseBody.contains(expectedSign.substring(0, 8)); // 简单验证前8位
+    }
+}

+ 38 - 0
day0331/src/test/java/cn/zhentao/AppTest.java

@@ -0,0 +1,38 @@
+package cn.zhentao;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest 
+    extends TestCase
+{
+    /**
+     * Create the test case
+     *
+     * @param testName name of the test case
+     */
+    public AppTest( String testName )
+    {
+        super( testName );
+    }
+
+    /**
+     * @return the suite of tests being tested
+     */
+    public static Test suite()
+    {
+        return new TestSuite( AppTest.class );
+    }
+
+    /**
+     * Rigourous Test :-)
+     */
+    public void testApp()
+    {
+        assertTrue( true );
+    }
+}

BIN
day0331/target/classes/cn/zhentao/Test.class


Некоторые файлы не были показаны из-за большого количества измененных файлов