(improvement)(headless) Add API interface to provide data services to other applications (#561)

Co-authored-by: jolunoluo
This commit is contained in:
LXW
2023-12-21 22:10:29 +08:00
committed by GitHub
parent 7b580b7c94
commit fa38e37be3
40 changed files with 1106 additions and 55 deletions

View File

@@ -0,0 +1,24 @@
package com.tencent.supersonic.common.pojo;
public final class Pair<T, U> {
public final T first;
public final U second;
public Pair(T first, U second) {
this.second = second;
this.first = first;
}
// Because 'pair()' is shorter than 'new Pair<>()'.
// Sometimes this difference might be very significant (especially in a
// 80-ish characters boundary). Sorry diamond operator.
public static <T, U> Pair<T, U> pair(T first, U second) {
return new Pair<>(first, second);
}
@Override
public String toString() {
return "(" + first + ", " + second + ")";
}
}

View File

@@ -1,10 +1,11 @@
package com.tencent.supersonic.common.pojo;
import com.google.common.base.Objects;
import java.util.Date;
import lombok.Data;
import lombok.ToString;
import java.util.Date;
@Data
@ToString
public class RecordInfo {
@@ -42,8 +43,7 @@ public class RecordInfo {
}
RecordInfo that = (RecordInfo) o;
return Objects.equal(createdBy, that.createdBy) && Objects.equal(
updatedBy, that.updatedBy) && Objects.equal(createdAt, that.createdAt)
&& Objects.equal(updatedAt, that.updatedAt);
updatedBy, that.updatedBy);
}
@Override

View File

@@ -0,0 +1,9 @@
package com.tencent.supersonic.common.pojo.enums;
public enum ApiItemType {
METRIC,
TAG,
DIMENSION
}

View File

@@ -0,0 +1,71 @@
package com.tencent.supersonic.common.util;
import com.tencent.supersonic.common.pojo.Pair;
import org.apache.commons.codec.binary.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import static java.lang.Thread.sleep;
public class SignatureUtils {
private static final String ALGORITHM_HMAC_SHA256 = "HmacSHA256";
private static final long TIME_OUT = 60 * 1000 * 30;
public static String generateSignature(String appKey, String appSecret, long timestamp) {
try {
Mac sha256HMAC = Mac.getInstance(ALGORITHM_HMAC_SHA256);
SecretKeySpec secretKey = new SecretKeySpec(appSecret.getBytes(), ALGORITHM_HMAC_SHA256);
sha256HMAC.init(secretKey);
String data = appKey + timestamp;
byte[] hash = sha256HMAC.doFinal(data.getBytes());
return Hex.encodeHexString(hash);
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException("Error generating signature", e);
}
}
public static Pair<Boolean, String> isValidSignature(String appKey, String appSecret,
long timestamp, String signatureToCheck) {
long currentTimeMillis = System.currentTimeMillis();
if (currentTimeMillis < timestamp) {
return new Pair<>(false, "Timestamp is in the future");
}
if (currentTimeMillis - timestamp > TIME_OUT) {
return new Pair<>(false, "Timestamp is too old");
}
String generatedSignature = generateSignature(appKey, appSecret, timestamp);
if (generatedSignature.equals(signatureToCheck)) {
return new Pair<>(true, "Signature is valid");
} else {
return new Pair<>(false, "Invalid signature");
}
}
public static void main(String[] args) throws InterruptedException {
// appkey为申请的接口id
String appKey = "1";
//生成的密钥
String appSecret = "8fb44f17-f37d-4510-bb29-59b0e0b266d0";
long timestamp = System.currentTimeMillis();
System.out.println("timeStamp:" + timestamp);
//生成的签名
String serverSignature = generateSignature(appKey, appSecret, timestamp);
System.out.println("Server Signature: " + serverSignature);
sleep(4000);
//用户需要的入参
Pair<Boolean, String> isValid = isValidSignature(appKey, appSecret, timestamp, serverSignature);
System.out.println("Is Signature Valid? " + isValid.first);
}
}