Merge remote-tracking branch 'origin/V1.00' into V1.00

This commit is contained in:
waner 2026-05-28 10:11:52 +08:00
commit 6ad2f46ad9
3 changed files with 40 additions and 31 deletions

View File

@ -49,6 +49,13 @@
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.17</version>
</dependency>
</dependencies>

View File

@ -8,8 +8,9 @@ public class Main
public static void main(String[] args) {
PBCAgent2G signClient = new PBCAgent2G();
String dn = "C=CN,ST=Beijing,L=Beijing,O=Initial Org,OU=Initial OU,CN=Initial Entity";
try {
signClient.rawSign("test".getBytes(StandardCharsets.UTF_8), "dn");
signClient.dettachedSign("test".getBytes(StandardCharsets.UTF_8), dn);
} catch (Exception e) {
System.out.println("签名失败:" + e.getMessage());

View File

@ -7,6 +7,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.security.cert.X509Certificate;
@ -19,8 +21,8 @@ public class PBCAgent2G{
private static final MediaType JSON_MEDIA_TYPE =
MediaType.parse("application/json; charset=utf-8");
private static final Logger logger = LoggerFactory.getLogger(PBCAgent2G.class);
private static final String baseUrl = "http://127.0.0.1:8080/api/v1/openapi";
private static final String apiKey = "key";
private static final String rawSignPath = "/rawSign";
@ -91,8 +93,7 @@ public class PBCAgent2G{
};
public String rawSign(byte[] origBytes, String dn) {
System.out.println("调用rawSign, dn:" + dn);
logger.info("调用rawSign, dn:" + dn);
String sessionId = UUID.randomUUID().toString().replace("-", "");
if (origBytes == null) {
@ -138,9 +139,9 @@ public class PBCAgent2G{
.build();
System.out.println("准备连接签名服务器");
logger.info("准备连接签名服务器");
try (Response response = okHttpClient.newCall(request).execute()) {
System.out.println("连接成功,并收到响应");
logger.info("连接成功,并收到响应");
String responseBody = response.body() == null ? "" : response.body().string();
JsonNode root = objectMapper.readTree(responseBody);
int code = root.get("code").asInt();
@ -159,16 +160,16 @@ public class PBCAgent2G{
}
} catch (IOException e) {
System.err.println("连接签名服务器异常" + e.getMessage());
logger.info("连接签名服务器异常" + e.getMessage());
throw new RuntimeException("调用rawSign异常:" + e.getMessage(), e);
} finally {
System.out.println("请求结束, 连接资源已释放");
logger.info("请求结束, 连接资源已释放");
}
}
public boolean rawVerify(byte[] origBytes, String certStr, String dn) {
System.out.println("调用rawVerify, dn:" + dn);
logger.info("调用rawVerify, dn:" + dn + " , certStr:" + certStr);
String sessionId = UUID.randomUUID().toString().replace("-", "");
if (origBytes == null) {
@ -219,9 +220,9 @@ public class PBCAgent2G{
.addHeader("X-API-Key", apiKey)
.build();
System.out.println("准备连接签名服务器");
logger.info("准备连接签名服务器");
try (Response response = okHttpClient.newCall(request).execute()) {
System.out.println("连接成功,并收到响应");
logger.info("连接成功,并收到响应");
String responseBody = response.body() == null ? "" : response.body().string();
JsonNode root = objectMapper.readTree(responseBody);
int code = root.get("code").asInt();
@ -239,16 +240,16 @@ public class PBCAgent2G{
}
} catch (IOException e) {
System.err.println("连接签名服务器异常" + e.getMessage());
logger.info("连接签名服务器异常" + e.getMessage());
throw new RuntimeException("调用rawVerify异常", e);
} finally {
System.out.println("请求结束, 连接资源已释放");
logger.info("请求结束, 连接资源已释放");
}
}
public String dettachedSign(byte[] origBytes, String dn) {
System.out.println("调用dettachedSign, dn:" + dn);
logger.info("调用dettachedSign, dn:" + dn);
String sessionId = UUID.randomUUID().toString().replace("-", "");
if (origBytes == null) {
@ -290,9 +291,9 @@ public class PBCAgent2G{
.build();
System.out.println("准备连接签名服务器");
logger.info("准备连接签名服务器");
try (Response response = okHttpClient.newCall(request).execute()) {
System.out.println("连接成功,并收到响应");
logger.info("连接成功,并收到响应");
String responseBody = response.body() == null ? "" : response.body().string();
JsonNode root = objectMapper.readTree(responseBody);
int code = root.get("code").asInt();
@ -310,10 +311,10 @@ public class PBCAgent2G{
}
} catch (IOException e) {
System.err.println("连接签名服务器异常" + e.getMessage());
logger.info("连接签名服务器异常" + e.getMessage());
throw new RuntimeException("调用dettachedSign异常", e);
} finally {
System.out.println("请求结束, 连接资源已释放");
logger.info("请求结束, 连接资源已释放");
}
}
@ -361,13 +362,13 @@ public class PBCAgent2G{
.addHeader("X-API-Key", apiKey)
.build();
System.out.println("准备连接签名服务器");
logger.info("准备连接签名服务器");
try (Response response = okHttpClient.newCall(request).execute()) {
System.out.println("连接成功,并收到响应");
logger.info("连接成功,并收到响应");
String responseBody = response.body() == null ? "" : response.body().string();
JsonNode root = objectMapper.readTree(responseBody);
int code = root.get("code").asInt();
System.out.println(root.toPrettyString());
logger.info(root.toPrettyString());
if (code == 200){
DettachedVerifyResponse data = objectMapper.treeToValue(root.get("data"), DettachedVerifyResponse.class);
return data.getGenericCertificate();
@ -383,10 +384,10 @@ public class PBCAgent2G{
}
} catch (IOException e) {
System.err.println("连接签名服务器异常" + e.getMessage());
logger.info("连接签名服务器异常" + e.getMessage());
throw new RuntimeException("调用dettachedVerify异常", e);
} finally {
System.out.println("请求结束, 连接资源已释放");
logger.info("请求结束, 连接资源已释放");
}
}
@ -435,9 +436,9 @@ public class PBCAgent2G{
.addHeader("X-API-Key", apiKey)
.build();
System.out.println("准备连接签名服务器");
logger.info("准备连接签名服务器");
try (Response response = okHttpClient.newCall(request).execute()) {
System.out.println("连接成功,并收到响应");
logger.info("连接成功,并收到响应");
String responseBody = response.body() == null ? "" : response.body().string();
JsonNode root = objectMapper.readTree(responseBody);
int code = root.get("code").asInt();
@ -455,10 +456,10 @@ public class PBCAgent2G{
}
} catch (IOException e) {
System.err.println("连接签名服务器异常" + e.getMessage());
logger.info("连接签名服务器异常" + e.getMessage());
throw new RuntimeException("调用dettachedVerifySimple异常", e);
} finally {
System.out.println("请求结束, 连接资源已释放");
logger.info("请求结束, 连接资源已释放");
}
}
@ -501,9 +502,9 @@ public class PBCAgent2G{
.addHeader("X-API-Key", apiKey)
.build();
System.out.println("准备连接签名服务器");
logger.info("准备连接签名服务器");
try (Response response = okHttpClient.newCall(request).execute()) {
System.out.println("连接成功,并收到响应");
logger.info("连接成功,并收到响应");
String responseBody = response.body() == null ? "" : response.body().string();
JsonNode root = objectMapper.readTree(responseBody);
int code = root.get("code").asInt();
@ -521,10 +522,10 @@ public class PBCAgent2G{
}
} catch (IOException e) {
System.err.println("连接签名服务器异常" + e.getMessage());
logger.info("连接签名服务器异常" + e.getMessage());
throw new RuntimeException("调用uploadCert异常", e);
} finally {
System.out.println("请求结束, 连接资源已释放");
logger.info("请求结束, 连接资源已释放");
}
}