tianyiman 1 сар өмнө
commit
d289ce03ae

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 13 - 0
.idea/compiler.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="CompilerConfiguration">
+    <annotationProcessing>
+      <profile name="Maven default annotation processors profile" enabled="true">
+        <sourceOutputDir name="target/generated-sources/annotations" />
+        <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
+        <outputRelativeToContentRoot value="true" />
+        <module name="xiang-mu" />
+      </profile>
+    </annotationProcessing>
+  </component>
+</project>

+ 7 - 0
.idea/encodings.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding">
+    <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
+    <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
+  </component>
+</project>

+ 20 - 0
.idea/jarRepositories.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="RemoteRepositoriesConfiguration">
+    <remote-repository>
+      <option name="id" value="central" />
+      <option name="name" value="Central Repository" />
+      <option name="url" value="http://maven.aliyun.com/nexus/content/groups/public/" />
+    </remote-repository>
+    <remote-repository>
+      <option name="id" value="central" />
+      <option name="name" value="Maven Central repository" />
+      <option name="url" value="https://repo1.maven.org/maven2" />
+    </remote-repository>
+    <remote-repository>
+      <option name="id" value="jboss.community" />
+      <option name="name" value="JBoss Community repository" />
+      <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
+    </remote-repository>
+  </component>
+</project>

+ 14 - 0
.idea/misc.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ExternalStorageConfigurationManager" enabled="true" />
+  <component name="MavenProjectsManager">
+    <option name="originalFiles">
+      <list>
+        <option value="$PROJECT_DIR$/pom.xml" />
+      </list>
+    </option>
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 30 - 0
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>xiang-mu</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>xiang-mu</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>

+ 112 - 0
src/main/java/cn/zhentao/Api.java

@@ -0,0 +1,112 @@
+package cn.zhentao;
+
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.crypto.digest.DigestUtil;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Hello world!
+ *
+ */
+public class Api
+{
+    // 假设的appKey,实际应用中应从安全配置中获取
+    private static final String APP_KEY = "your_app_key_here";
+    private static final String APP_ID = "your_app_id";
+
+    public static void main(String[] args) {
+        // 模拟请求参数
+        String goodsId = "12345";
+        String reqId = "req_67890";
+        String reqTime = String.valueOf(System.currentTimeMillis());
+
+        // 生成签名
+        String signature = generateSignature(APP_ID, goodsId, reqId, reqTime, APP_KEY);
+        System.out.println("生成的签名: " + signature);
+
+        // 调用接口一
+        callApi1(APP_ID, goodsId, reqId, reqTime, signature);
+
+        // 调用接口二
+        callApi2(APP_ID, goodsId, reqId, reqTime, signature);
+    }
+
+    /**
+     * 生成MD5签名
+     * @param appId 应用ID
+     * @param goodsId 商品ID
+     * @param reqId 请求ID
+     * @param reqTime 请求时间戳
+     * @param appKey 应用密钥
+     * @return MD5签名
+     */
+    public static String generateSignature(String appId, String goodsId, String reqId, String reqTime, String appKey) {
+        // 拼接签名字符串:appId+goodsId+reqId+reqTime+appKey
+        String content = StrUtil.builder()
+                .append(appId)
+                .append(goodsId)
+                .append(reqId)
+                .append(reqTime)
+                .append(appKey)
+                .toString();
+
+        // 使用Hutool的DigestUtil进行MD5加密
+        return DigestUtil.md5Hex(content);
+    }
+
+    /**
+     * 模拟调用接口一
+     */
+    public static void callApi1(String appId, String goodsId, String reqId, String reqTime, String signature) {
+        System.out.println("\n调用接口一...");
+
+        // 模拟服务端验证签名
+        boolean isValid = verifySignature(appId, goodsId, reqId, reqTime, signature, APP_KEY);
+
+        if (isValid) {
+            System.out.println("接口一: 签名验证成功,处理请求...");
+            // 这里可以继续处理业务逻辑
+        } else {
+            System.out.println("接口一: 签名验证失败,拒绝请求!");
+        }
+    }
+
+    /**
+     * 模拟调用接口二
+     */
+    public static void callApi2(String appId, String goodsId, String reqId, String reqTime, String signature) {
+        System.out.println("\n调用接口二...");
+
+        // 模拟服务端验证签名
+        boolean isValid = verifySignature(appId, goodsId, reqId, reqTime, signature, APP_KEY);
+
+        if (isValid) {
+            System.out.println("接口二: 签名验证成功,处理请求...");
+            // 这里可以继续处理业务逻辑
+        } else {
+            System.out.println("接口二: 签名验证失败,拒绝请求!");
+        }
+
+    }
+
+    /**
+     * 验证签名
+     * @param appId 应用ID
+     * @param goodsId 商品ID
+     * @param reqId 请求ID
+     * @param reqTime 请求时间戳
+     * @param signature 待验证的签名
+     * @param appKey 应用密钥
+     * @return 是否验证通过
+     */
+    public static boolean verifySignature(String appId, String goodsId, String reqId,
+                                          String reqTime, String signature, String appKey) {
+        // 重新生成签名
+        String calculatedSignature = generateSignature(appId, goodsId, reqId, reqTime, appKey);
+
+        // 比较签名是否一致
+        return StrUtil.equalsIgnoreCase(signature, calculatedSignature);
+    }
+}

+ 38 - 0
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 );
+    }
+}