|
|
@@ -0,0 +1,148 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.KeyFactory;
|
|
|
+import java.security.PrivateKey;
|
|
|
+import java.security.PublicKey;
|
|
|
+import java.security.Signature;
|
|
|
+import java.security.spec.PKCS8EncodedKeySpec;
|
|
|
+import java.security.spec.X509EncodedKeySpec;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import javax.crypto.Cipher;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 爱云医(AI 检测)鉴权签名宿主,能力键 {@code ai.aiyunyi.sign}(S-cfc-469)。
|
|
|
+ *
|
|
|
+ * <p>按设计 §3.2 统一在服务端构造鉴权头,私钥/公钥仅存在于服务端配置,绝不下发前端。
|
|
|
+ * 签名串固定为 {@code appKey={AppKey}×tamp={毫秒}},使用 SHA256withRSA 私钥签名后 Base64;
|
|
|
+ * 业务头额外携带 {@code Authorization: Bearer {accessToken}} 与
|
|
|
+ * {@code X-User-Id-Encrypt}(RSA 公钥 PKCS#1 加密 userId)。</p>
|
|
|
+ *
|
|
|
+ * <p>本类为无状态内部能力:每次调用实时取毫秒时间戳,不做任何缓存;
|
|
|
+ * 可被 Spring 托管,亦可被反射直接构造(保持无参构造器)。</p>
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class AicdsGatewayService {
|
|
|
+
|
|
|
+ private static final String H_APP_KEY = "X-App-Key";
|
|
|
+ private static final String H_TIMESTAMP = "X-Timestamp";
|
|
|
+ private static final String H_API_SIGN = "X-Api-Sign";
|
|
|
+ private static final String H_AUTHORIZATION = "Authorization";
|
|
|
+ private static final String H_USER_ID_ENCRYPT = "X-User-Id-Encrypt";
|
|
|
+
|
|
|
+ private static final String SIGN_ALGORITHM = "SHA256withRSA";
|
|
|
+ private static final String KEY_ALGORITHM = "RSA";
|
|
|
+ private static final String CIPHER_ALGORITHM = "RSA/ECB/PKCS1Padding";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AC2:构造固定格式签名串(字段名/顺序/& 分隔符固定,无多余空格)。
|
|
|
+ */
|
|
|
+ public String buildSignString(String appKey, long timestampMillis) {
|
|
|
+ return "appKey=" + appKey + "×tamp=" + timestampMillis;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AC3/AC8:Base64(SHA256withRSA(私钥, signString));空/空白私钥快速失败。
|
|
|
+ */
|
|
|
+ public String sign(String signString, String privateKeyPem) {
|
|
|
+ if (privateKeyPem == null || privateKeyPem.trim().isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("爱云医签名私钥为空,拒绝生成空签名(鉴权不得静默失效)");
|
|
|
+ }
|
|
|
+ if (signString == null) {
|
|
|
+ throw new IllegalArgumentException("爱云医签名串为空");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ PrivateKey privateKey = loadPrivateKey(privateKeyPem);
|
|
|
+ Signature signer = Signature.getInstance(SIGN_ALGORITHM);
|
|
|
+ signer.initSign(privateKey);
|
|
|
+ signer.update(signString.getBytes(StandardCharsets.UTF_8));
|
|
|
+ return Base64.getEncoder().encodeToString(signer.sign());
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new IllegalStateException("爱云医 SHA256withRSA 签名失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AC4/AC6:登录三头(X-App-Key / X-Timestamp / X-Api-Sign),签名绑定头内时间戳。
|
|
|
+ */
|
|
|
+ public Map<String, String> buildAuthHeaders(String appKey, long timestampMillis, String privateKeyPem) {
|
|
|
+ Map<String, String> headers = new LinkedHashMap<String, String>();
|
|
|
+ headers.put(H_APP_KEY, appKey);
|
|
|
+ headers.put(H_TIMESTAMP, String.valueOf(timestampMillis));
|
|
|
+ headers.put(H_API_SIGN, sign(buildSignString(appKey, timestampMillis), privateKeyPem));
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AC5:每次请求实时取当前毫秒,时间戳不可缓存。
|
|
|
+ */
|
|
|
+ public Map<String, String> buildAuthHeaders(String appKey, String privateKeyPem) {
|
|
|
+ return buildAuthHeaders(appKey, System.currentTimeMillis(), privateKeyPem);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AC7:Base64(RSA 公钥 PKCS#1 加密 userId),可用配对私钥解密还原。
|
|
|
+ */
|
|
|
+ public String encryptUserId(String userId, String publicKeyPem) {
|
|
|
+ if (userId == null) {
|
|
|
+ throw new IllegalArgumentException("爱云医待加密 userId 为空");
|
|
|
+ }
|
|
|
+ if (publicKeyPem == null || publicKeyPem.trim().isEmpty()) {
|
|
|
+ throw new IllegalArgumentException("爱云医加密公钥为空");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ PublicKey publicKey = loadPublicKey(publicKeyPem);
|
|
|
+ Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
|
|
+ return Base64.getEncoder().encodeToString(
|
|
|
+ cipher.doFinal(userId.getBytes(StandardCharsets.UTF_8)));
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new IllegalStateException("爱云医 RSA 加密 userId 失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AC7:业务头 = 登录三头 + Authorization(Bearer) + X-User-Id-Encrypt。
|
|
|
+ */
|
|
|
+ public Map<String, String> buildBusinessHeaders(String appKey, long timestampMillis,
|
|
|
+ String privateKeyPem, String publicKeyPem,
|
|
|
+ String accessToken, String userId) {
|
|
|
+ Map<String, String> headers = buildAuthHeaders(appKey, timestampMillis, privateKeyPem);
|
|
|
+ headers.put(H_AUTHORIZATION, "Bearer " + accessToken);
|
|
|
+ headers.put(H_USER_ID_ENCRYPT, encryptUserId(userId, publicKeyPem));
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== PEM 解析(纯 JDK,支持标准 PKCS#8 私钥 / X.509 公钥) ====================
|
|
|
+
|
|
|
+ private static PrivateKey loadPrivateKey(String pem) throws Exception {
|
|
|
+ byte[] der = decodePem(pem);
|
|
|
+ return KeyFactory.getInstance(KEY_ALGORITHM).generatePrivate(new PKCS8EncodedKeySpec(der));
|
|
|
+ }
|
|
|
+
|
|
|
+ private static PublicKey loadPublicKey(String pem) throws Exception {
|
|
|
+ byte[] der = decodePem(pem);
|
|
|
+ return KeyFactory.getInstance(KEY_ALGORITHM).generatePublic(new X509EncodedKeySpec(der));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 去掉 PEM 头尾标记与换行,解出 DER 字节。 */
|
|
|
+ private static byte[] decodePem(String pem) {
|
|
|
+ String base64 = pem.replaceAll("-----BEGIN [A-Z ]+-----", "")
|
|
|
+ .replaceAll("-----END [A-Z ]+-----", "")
|
|
|
+ .replaceAll("\\s", "");
|
|
|
+ try {
|
|
|
+ return Base64.getDecoder().decode(base64);
|
|
|
+ } catch (IllegalArgumentException e) {
|
|
|
+ throw new IllegalArgumentException("爱云医密钥 PEM 格式非法(Base64 解码失败)", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|