From 63e8bef5dbf6ac9fc4edf871e86098f1e19ce53b Mon Sep 17 00:00:00 2001 From: xezzon Date: Sun, 8 Dec 2024 15:21:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geom-open-sdk/pom.xml | 33 +++++----- .../github/xezzon/geom/GeomOpenConstant.java | 15 +++++ .../xezzon/geom/GeomOpenRequestBuilder.java | 15 ++--- .../io/github/xezzon/geom/TestController.java | 8 +-- .../geom/call/SubscriptionCallController.java | 8 +-- .../subscription/SubscriptionService.java | 10 +-- .../ISubscriptionService4ThirdPartyApp.java | 16 ++--- .../third_party_app/ThirdPartyAppService.java | 6 +- .../geom/call/SubscriptionCallHttpTest.java | 62 +++++++++---------- 9 files changed, 92 insertions(+), 81 deletions(-) create mode 100644 geom-open-sdk/src/main/java/io/github/xezzon/geom/GeomOpenConstant.java diff --git a/geom-open-sdk/pom.xml b/geom-open-sdk/pom.xml index 5832420d..4860acae 100644 --- a/geom-open-sdk/pom.xml +++ b/geom-open-sdk/pom.xml @@ -2,7 +2,24 @@ + 4.0.0 geom-open-sdk + 构造与geom开放平台交互的HTTP接口的SDK + ${project.groupId}:${project.artifactId} + + jar + + io.github.xezzon + geom + 1.0-SNAPSHOT + + + + 17 + 17 + UTF-8 + + feign-core @@ -29,20 +46,4 @@ test - 构造与geom开放平台交互的HTTP接口的SDK - - 4.0.0 - ${project.groupId}:${project.artifactId} - - - geom - io.github.xezzon - 1.0-SNAPSHOT - - - - 17 - 17 - UTF-8 - diff --git a/geom-open-sdk/src/main/java/io/github/xezzon/geom/GeomOpenConstant.java b/geom-open-sdk/src/main/java/io/github/xezzon/geom/GeomOpenConstant.java new file mode 100644 index 00000000..ebbd414b --- /dev/null +++ b/geom-open-sdk/src/main/java/io/github/xezzon/geom/GeomOpenConstant.java @@ -0,0 +1,15 @@ +package io.github.xezzon.geom; + +/** + * @author xezzon + */ +public class GeomOpenConstant { + + public static final String DIGEST_ALGORITHM = "HmacSHA256"; + public static final String ACCESS_KEY_HEADER = "X-Access-Key"; + public static final String TIMESTAMP_HEADER = "X-Timestamp"; + public static final String SIGNATURE_HEADER = "X-Signature"; + + private GeomOpenConstant() { + } +} diff --git a/geom-open-sdk/src/main/java/io/github/xezzon/geom/GeomOpenRequestBuilder.java b/geom-open-sdk/src/main/java/io/github/xezzon/geom/GeomOpenRequestBuilder.java index d6d2f1eb..96daedc8 100644 --- a/geom-open-sdk/src/main/java/io/github/xezzon/geom/GeomOpenRequestBuilder.java +++ b/geom-open-sdk/src/main/java/io/github/xezzon/geom/GeomOpenRequestBuilder.java @@ -15,11 +15,6 @@ */ public class GeomOpenRequestBuilder { - public static final String DIGEST_ALGORITHM = "HmacSHA256"; - public static final String ACCESS_KEY_HEADER = "X-Access-Key"; - public static final String TIMESTAMP_HEADER = "X-Timestamp"; - public static final String SIGNATURE_HEADER = "X-Signature"; - static { Security.addProvider(new BouncyCastleProvider()); } @@ -48,18 +43,18 @@ class GeomOpenRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { // 应用标识 - requestTemplate.header(ACCESS_KEY_HEADER, accessKey); + requestTemplate.header(GeomOpenConstant.ACCESS_KEY_HEADER, accessKey); // 时间戳 long timestamp = Instant.now().toEpochMilli(); - requestTemplate.header(TIMESTAMP_HEADER, String.valueOf(timestamp)); + requestTemplate.header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp)); // 摘要 byte[] body = requestTemplate.body(); try { - Mac mac = Mac.getInstance(DIGEST_ALGORITHM); - mac.init(new SecretKeySpec(secretKey, DIGEST_ALGORITHM)); + Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM); + mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM)); mac.update(body); String signature = Base64.getEncoder().encodeToString(mac.doFinal()); - requestTemplate.header(SIGNATURE_HEADER, signature); + requestTemplate.header(GeomOpenConstant.SIGNATURE_HEADER, signature); } catch (Exception e) { throw new GeomOpenException(e); } diff --git a/geom-open-sdk/src/test/java/io/github/xezzon/geom/TestController.java b/geom-open-sdk/src/test/java/io/github/xezzon/geom/TestController.java index 89d9b0aa..95a4ad8a 100644 --- a/geom-open-sdk/src/test/java/io/github/xezzon/geom/TestController.java +++ b/geom-open-sdk/src/test/java/io/github/xezzon/geom/TestController.java @@ -1,6 +1,6 @@ package io.github.xezzon.geom; -import static io.github.xezzon.geom.GeomOpenRequestBuilder.DIGEST_ALGORITHM; +import static io.github.xezzon.geom.GeomOpenConstant.DIGEST_ALGORITHM; import static io.github.xezzon.geom.TestApplication.SECRET_KEY; import com.fasterxml.jackson.databind.ObjectMapper; @@ -31,9 +31,9 @@ public class TestController { @PostMapping("/test") public String test( @RequestBody byte[] body, - @RequestHeader(GeomOpenRequestBuilder.ACCESS_KEY_HEADER) String accessKey, - @RequestHeader(GeomOpenRequestBuilder.TIMESTAMP_HEADER) Instant timestamp, - @RequestHeader(GeomOpenRequestBuilder.SIGNATURE_HEADER) String signature + @RequestHeader(GeomOpenConstant.ACCESS_KEY_HEADER) String accessKey, + @RequestHeader(GeomOpenConstant.TIMESTAMP_HEADER) Instant timestamp, + @RequestHeader(GeomOpenConstant.SIGNATURE_HEADER) String signature ) throws NoSuchAlgorithmException, InvalidKeyException, IOException { Assertions.assertEquals("hello", accessKey); Assertions.assertTrue(timestamp.isBefore(Instant.now())); diff --git a/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/call/SubscriptionCallController.java b/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/call/SubscriptionCallController.java index 93fca5fb..6dfedade 100644 --- a/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/call/SubscriptionCallController.java +++ b/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/call/SubscriptionCallController.java @@ -2,7 +2,7 @@ import static com.google.auth.http.AuthHttpConstants.BEARER; -import io.github.xezzon.geom.GeomOpenRequestBuilder; +import io.github.xezzon.geom.GeomOpenConstant; import jakarta.servlet.http.HttpServletResponse; import java.time.Instant; import org.springframework.http.HttpHeaders; @@ -31,9 +31,9 @@ public SubscriptionCallController(SubscriptionCallService subscriptionCallServic public void validateCall( @RequestBody byte[] body, @RequestParam("path") String path, - @RequestHeader(GeomOpenRequestBuilder.ACCESS_KEY_HEADER) String accessKey, - @RequestHeader(GeomOpenRequestBuilder.TIMESTAMP_HEADER) Instant timestamp, - @RequestHeader(GeomOpenRequestBuilder.SIGNATURE_HEADER) String signature, + @RequestHeader(GeomOpenConstant.ACCESS_KEY_HEADER) String accessKey, + @RequestHeader(GeomOpenConstant.TIMESTAMP_HEADER) Instant timestamp, + @RequestHeader(GeomOpenConstant.SIGNATURE_HEADER) String signature, HttpServletResponse response ) { /* 所有校验通过后发放令牌 */ diff --git a/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/subscription/SubscriptionService.java b/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/subscription/SubscriptionService.java index 90be2412..579cbb75 100644 --- a/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/subscription/SubscriptionService.java +++ b/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/subscription/SubscriptionService.java @@ -68,6 +68,11 @@ protected void auditSubscription(String id) { subscriptionDAO.get().save(entity); } + @Override + public List listSubscriptionsOfApp(String appId) { + return subscriptionDAO.get().findByAppId(appId); + } + @Override public Page listSubscription(ODataQueryOption odata, String appId) { thirdPartyAppService.checkPermission(appId); @@ -87,9 +92,4 @@ public Page listSubscription(ODataQueryOption odata, String appId) .toList(); return new PageImpl<>(subscriptions, openapiPage.getPageable(), openapiPage.getTotalElements()); } - - @Override - public List listSubscriptionsOfApp(String appId) { - return subscriptionDAO.get().findByAppId(appId); - } } diff --git a/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/subscription/service/ISubscriptionService4ThirdPartyApp.java b/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/subscription/service/ISubscriptionService4ThirdPartyApp.java index cfa2535a..a40221d0 100644 --- a/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/subscription/service/ISubscriptionService4ThirdPartyApp.java +++ b/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/subscription/service/ISubscriptionService4ThirdPartyApp.java @@ -10,18 +10,18 @@ */ public interface ISubscriptionService4ThirdPartyApp { - /** - * 获取订阅列表 - * @param into OData查询选项,用于指定查询条件、排序方式等。 - * @param appId 应用程序ID。 - * @return 包含订阅信息的分页对象。 - */ - Page listSubscription(ODataQueryOption into, String appId); - /** * 获取指定应用ID下订阅的所有OpenAPI列表 * @param appId 应用ID * @return 返回订阅的所有OpenAPI列表 */ List listSubscriptionsOfApp(String appId); + + /** + * 获取订阅列表 + * @param into OData查询选项,用于指定查询条件、排序方式等。 + * @param appId 应用程序ID。 + * @return 包含订阅信息的分页对象。 + */ + Page listSubscription(ODataQueryOption odata, String appId); } diff --git a/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/third_party_app/ThirdPartyAppService.java b/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/third_party_app/ThirdPartyAppService.java index 6b6a73a7..4438b382 100644 --- a/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/third_party_app/ThirdPartyAppService.java +++ b/geom-service/geom-service-open/src/main/java/io/github/xezzon/geom/third_party_app/ThirdPartyAppService.java @@ -1,7 +1,7 @@ package io.github.xezzon.geom.third_party_app; import cn.dev33.satoken.stp.StpUtil; -import io.github.xezzon.geom.GeomOpenRequestBuilder; +import io.github.xezzon.geom.GeomOpenConstant; import io.github.xezzon.geom.common.exception.DataPermissionForbiddenException; import io.github.xezzon.geom.common.exception.InvalidAccessKeyException; import io.github.xezzon.geom.core.odata.ODataQueryOption; @@ -95,9 +95,9 @@ public void validateSignature(String appId, byte[] body, String signature) { AccessSecret accessSecret = accessSecretRepository.findById(appId) .orElseThrow(InvalidAccessKeyException::new); try { - Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM); + Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM); byte[] secretKey = Base64.getDecoder().decode(accessSecret.getSecretKey()); - mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM)); + mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM)); mac.update(body); if (!Objects.equals( signature, diff --git a/geom-service/geom-service-open/src/test/java/io/github/xezzon/geom/call/SubscriptionCallHttpTest.java b/geom-service/geom-service-open/src/test/java/io/github/xezzon/geom/call/SubscriptionCallHttpTest.java index e0fdf419..d7911c4e 100644 --- a/geom-service/geom-service-open/src/test/java/io/github/xezzon/geom/call/SubscriptionCallHttpTest.java +++ b/geom-service/geom-service-open/src/test/java/io/github/xezzon/geom/call/SubscriptionCallHttpTest.java @@ -5,7 +5,7 @@ import static io.github.xezzon.geom.common.exception.GlobalExceptionHandler.ERROR_CODE_HEADER; import cn.hutool.core.util.RandomUtil; -import io.github.xezzon.geom.GeomOpenRequestBuilder; +import io.github.xezzon.geom.GeomOpenConstant; import io.github.xezzon.geom.auth.JwtAuth; import io.github.xezzon.geom.auth.JwtClaim; import io.github.xezzon.geom.common.exception.OpenErrorCode; @@ -75,9 +75,9 @@ void validate() throws NoSuchAlgorithmException, InvalidKeyException { final String rawBody = "{\"id\":\"1234567890\"}"; long timestamp = Instant.now().toEpochMilli(); Tuple3 dataset = this.initData(); - Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM); + Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM); byte[] secretKey = Base64.getDecoder().decode(dataset.getT3().getSecretKey()); - mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM)); + mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM)); mac.update(rawBody.getBytes()); String signature = Base64.getEncoder().encodeToString(mac.doFinal()); @@ -87,9 +87,9 @@ void validate() throws NoSuchAlgorithmException, InvalidKeyException { .queryParam("path", dataset.getT2().getOpenapiCode()) .build() ) - .header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey()) - .header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp)) - .header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature) + .header(GeomOpenConstant.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey()) + .header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp)) + .header(GeomOpenConstant.SIGNATURE_HEADER, signature) .bodyValue(rawBody) .exchange() .expectStatus().isOk() @@ -112,9 +112,9 @@ void validate_notSubscribed() throws NoSuchAlgorithmException, InvalidKeyExcepti final String rawBody = "{\"id\":\"1234567890\"}"; long timestamp = Instant.now().toEpochMilli(); Tuple3 dataset = this.initData(); - Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM); + Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM); byte[] secretKey = Base64.getDecoder().decode(dataset.getT3().getSecretKey()); - mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM)); + mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM)); mac.update(rawBody.getBytes()); String signature = Base64.getEncoder().encodeToString(mac.doFinal()); @@ -124,9 +124,9 @@ void validate_notSubscribed() throws NoSuchAlgorithmException, InvalidKeyExcepti .queryParam("path", RandomUtil.randomString(8)) .build() ) - .header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey()) - .header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp)) - .header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature) + .header(GeomOpenConstant.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey()) + .header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp)) + .header(GeomOpenConstant.SIGNATURE_HEADER, signature) .bodyValue(rawBody) .exchange() .expectStatus().isForbidden() @@ -138,9 +138,9 @@ void validate_incorrectAccessKey() throws NoSuchAlgorithmException, InvalidKeyEx final String rawBody = "{\"id\":\"1234567890\"}"; long timestamp = Instant.now().toEpochMilli(); Tuple3 dataset = this.initData(); - Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM); + Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM); byte[] secretKey = Base64.getDecoder().decode(dataset.getT3().getSecretKey()); - mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM)); + mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM)); mac.update(rawBody.getBytes()); String signature = Base64.getEncoder().encodeToString(mac.doFinal()); @@ -150,9 +150,9 @@ void validate_incorrectAccessKey() throws NoSuchAlgorithmException, InvalidKeyEx .queryParam("path", dataset.getT2().getOpenapiCode()) .build() ) - .header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, RandomUtil.randomString(8)) - .header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp)) - .header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature) + .header(GeomOpenConstant.ACCESS_KEY_HEADER, RandomUtil.randomString(8)) + .header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp)) + .header(GeomOpenConstant.SIGNATURE_HEADER, signature) .bodyValue(rawBody) .exchange() .expectStatus().isForbidden() @@ -164,9 +164,9 @@ void validate_incorrectSecretKey() throws NoSuchAlgorithmException, InvalidKeyEx final String rawBody = "{\"id\":\"1234567890\"}"; long timestamp = Instant.now().toEpochMilli(); Tuple3 dataset = this.initData(); - Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM); + Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM); byte[] secretKey = Base64.getDecoder().decode(RandomUtil.randomString(8)); - mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM)); + mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM)); mac.update(rawBody.getBytes()); String signature = Base64.getEncoder().encodeToString(mac.doFinal()); @@ -176,9 +176,9 @@ void validate_incorrectSecretKey() throws NoSuchAlgorithmException, InvalidKeyEx .queryParam("path", dataset.getT2().getOpenapiCode()) .build() ) - .header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey()) - .header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp)) - .header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature) + .header(GeomOpenConstant.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey()) + .header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp)) + .header(GeomOpenConstant.SIGNATURE_HEADER, signature) .bodyValue(rawBody) .exchange() .expectStatus().isForbidden() @@ -190,9 +190,9 @@ void validate_mismatchSignature() throws NoSuchAlgorithmException, InvalidKeyExc final String rawBody = "{\"id\":\"1234567890\"}"; long timestamp = Instant.now().toEpochMilli(); Tuple3 dataset = this.initData(); - Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM); + Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM); byte[] secretKey = Base64.getDecoder().decode(dataset.getT3().getSecretKey()); - mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM)); + mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM)); mac.update("tampered message".getBytes()); String signature = Base64.getEncoder().encodeToString(mac.doFinal()); @@ -202,9 +202,9 @@ void validate_mismatchSignature() throws NoSuchAlgorithmException, InvalidKeyExc .queryParam("path", dataset.getT2().getOpenapiCode()) .build() ) - .header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey()) - .header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp)) - .header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature) + .header(GeomOpenConstant.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey()) + .header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp)) + .header(GeomOpenConstant.SIGNATURE_HEADER, signature) .bodyValue(rawBody) .exchange() .expectStatus().isForbidden() @@ -216,9 +216,9 @@ void validate_timeout() throws NoSuchAlgorithmException, InvalidKeyException { final String rawBody = "{\"id\":\"1234567890\"}"; long timestamp = Instant.now().getEpochSecond(); Tuple3 dataset = this.initData(); - Mac mac = Mac.getInstance(GeomOpenRequestBuilder.DIGEST_ALGORITHM); + Mac mac = Mac.getInstance(GeomOpenConstant.DIGEST_ALGORITHM); byte[] secretKey = Base64.getDecoder().decode(dataset.getT3().getSecretKey()); - mac.init(new SecretKeySpec(secretKey, GeomOpenRequestBuilder.DIGEST_ALGORITHM)); + mac.init(new SecretKeySpec(secretKey, GeomOpenConstant.DIGEST_ALGORITHM)); mac.update(rawBody.getBytes()); String signature = Base64.getEncoder().encodeToString(mac.doFinal()); @@ -228,9 +228,9 @@ void validate_timeout() throws NoSuchAlgorithmException, InvalidKeyException { .queryParam("path", dataset.getT2().getOpenapiCode()) .build() ) - .header(GeomOpenRequestBuilder.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey()) - .header(GeomOpenRequestBuilder.TIMESTAMP_HEADER, String.valueOf(timestamp)) - .header(GeomOpenRequestBuilder.SIGNATURE_HEADER, signature) + .header(GeomOpenConstant.ACCESS_KEY_HEADER, dataset.getT3().getAccessKey()) + .header(GeomOpenConstant.TIMESTAMP_HEADER, String.valueOf(timestamp)) + .header(GeomOpenConstant.SIGNATURE_HEADER, signature) .bodyValue(rawBody) .exchange() .expectStatus().isOk()