case-collection/sa-admin/src/test/java/net/lab1024/sa/admin/SmartAdminApplicationTest.java
2025-06-21 11:06:35 +08:00

78 lines
3.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package net.lab1024.sa.admin;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Map;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class SmartAdminApplicationTest {
@Value("${token.key}")
private String tokenKey;
@BeforeEach
public void before() {
System.out.println("----------------------- 测试开始 -----------------------");
}
@AfterEach
public void after() {
System.out.println("----------------------- 测试结束 -----------------------");
}
public static String getResultSign(String key, String body) throws Exception {
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
hmacSha256.init(secret_key);
return Base64.getEncoder().encodeToString(hmacSha256.doFinal(body.getBytes()));
}
@Test
public void test12() throws Exception {
String key = "123654";
// 请确保 body 为您收到回调请求的原始包体,不要做任何转化,需要完整保留\n\t转移字符示例如下
String body = "{\n" + "\t\"EventGroupId\":\t2,\n" + "\t\"EventType\":\t204,\n" + "\t\"CallbackTs\":\t1664209748188,\n" + "\t\"EventInfo\":\t{\n" + "\t\t\"RoomId\":\t8489,\n" + "\t\t\"EventTs\":\t1664209748,\n" + "\t\t\"EventMsTs\":\t1664209748180,\n" + "\t\t\"UserId\":\t\"user_85034614\",\n" + "\t\t\"Reason\":\t0\n" + "\t}\n" + "}";
String Sign = "kkoFeO3Oh2ZHnjtg8tEAQhtXK16/KI05W3BQff8IvGA=";
String resultSign = getResultSign(key, body);
if (resultSign.equals(Sign)) {
System.out.println("{'Status': 'OK', 'Info': '校验通过'}");
} else {
System.out.println("{'Status': 'FAIL', 'Info': '校验失败'}");
}
System.out.println("https://medical-case.oss-cn-beijing.aliyuncs.com//sign/10/076436c3-9096-4b14-bef4-5cd3623fc943.png".length());
}
@Test
public void test1(){
String token = "eyJhbGciOiJIUzUxMiJ9.eyJpZCI6NSwibmFtZSI6IkFBVyIsInR5cGUiOjIsImRldmljZSI6NSwic3VwZXJQYXNzd29yZEZsYWciOmZhbHNlLCJpYXQiOjE3MDYyNTQ5NTQsImV4cCI6MTcwNjg1OTc1NH0.eQScNydlCvv8iGwg_j2tSVvrF_vyu4CevFaTyGOfWfTUMr4dxrqRujv0cyxPboLNA_1VBEWMgXacVJkXC_D-Pw";
String token1 = "eyJhbGciOiJIUzUxMiJ9.eyJpZCI6NSwibmFtZSI6IkFBVyIsInR5cGUiOjIsImRldmljZSI6NSwic3VwZXJQYXNzd29yZEZsYWciOmZhbHNlLCJpYXQiOjE3MDYyNTQ5NTQsImV4cCI6MTcwNjg1OTc1NH0.eQScNydlCvv8iGwg_j2tSVvrF_vyu4CevFaTyGOfWfTUMr4dxrqRujv0cyxPboLNA_1VBEWMgXacVJkXC_D-Pw";
System.out.println(token1.equals(token));
try {
Claims body = Jwts.parser()
.setSigningKey("sa-jwt-key")
.parseClaimsJws(token)
.getBody();
System.out.println(body);
} catch (Exception e) {
e.printStackTrace();
}
}
}