跳转验签
This commit is contained in:
1 parent
bd1231c2dd
commit
9daba4a98e
5 files changed
+191
-2
No files matched your search
@@ -31,7 +31,7 @@ public class UsdtConfigBalanceRefreshTask {
|
||||
private TronServiceProvider tronServiceProvider;
|
||||
|
||||
/** 默认每 30 秒刷新一次,完成上一轮后再计算间隔,防止慢查询堆积。 */
|
||||
@Scheduled(fixedDelayString = "${payment.usdt-config-balance.refresh-delay-ms:30000}")
|
||||
@Scheduled(fixedDelayString = "${payment.usdt-config-balance.refresh-delay-ms:15000}")
|
||||
public void refresh() {
|
||||
UsdtConfigPlus config = usdtConfigMapper.selectOne(new LambdaQueryWrapper<UsdtConfigPlus>()
|
||||
.select(UsdtConfigPlus::getId, UsdtConfigPlus::getTokenAddress)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<!-- 日志存放路径 -->
|
||||
<property name="log.path" value="/home/ruoyi/logs" />
|
||||
<property name="log.path" value="/opt/ruoyi-admin/logs" />
|
||||
<!-- 日志输出格式 -->
|
||||
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] [traceId:%X{traceId}] %-5level %logger{20} - [%method,%line] - %msg%n" />
|
||||
|
||||
@@ -57,6 +57,29 @@
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!-- 系统日志输出 -->
|
||||
<appender name="file_warn" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/sys-warn.log</file>
|
||||
<!-- 循环政策:基于时间创建日志文件 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 日志文件名格式 -->
|
||||
<fileNamePattern>${log.path}/sys-warn.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<!-- 日志最大的历史 60天 -->
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<!-- 过滤的级别 -->
|
||||
<level>WARN</level>
|
||||
<!-- 匹配时的操作:接收(记录) -->
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!-- 用户访问日志输出 -->
|
||||
<appender name="sys-user" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/sys-user.log</file>
|
||||
@@ -84,6 +107,7 @@
|
||||
<root level="info">
|
||||
<appender-ref ref="file_info" />
|
||||
<appender-ref ref="file_error" />
|
||||
<appender-ref ref="file_warn" />
|
||||
</root>
|
||||
|
||||
<!--系统用户操作日志-->
|
||||
|
||||
@@ -29,6 +29,8 @@ public class PaymentApiController {
|
||||
private ApiSecurityService apiSecurityService;
|
||||
@Autowired
|
||||
private PaymentOrderOpenApiService paymentOrderOpenApiService;
|
||||
@Autowired
|
||||
private PaymentRedirectService paymentRedirectService;
|
||||
|
||||
@Log(title = "开放 API 创建支付订单", businessType = BusinessType.INSERT,
|
||||
isSaveRequestData = false, isSaveResponseData = false)
|
||||
@@ -51,6 +53,10 @@ public class PaymentApiController {
|
||||
result.put("trade_id", order.getTradeId());
|
||||
result.put("status", order.getStatus());
|
||||
result.put("transaction_id", order.getBlockTransactionId());
|
||||
if (Long.valueOf(2L).equals(order.getStatus())) {
|
||||
result.put("signed_redirect_url",
|
||||
paymentRedirectService.buildSignedRedirectUrl(order, request.getApiKey()));
|
||||
}
|
||||
return AjaxResult.success(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.ruoyi.system.service;
|
||||
|
||||
import com.ruoyi.system.domain.AppsPlus;
|
||||
import com.ruoyi.system.domain.UsdtOrdersPlus;
|
||||
import com.ruoyi.system.security.RsaKeyUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.security.PrivateKey;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/** 为支付成功后的同步跳转生成平台 RSA 签名,私钥只在服务端内存中使用。 */
|
||||
@Service
|
||||
public class PaymentRedirectService {
|
||||
@Autowired
|
||||
private IAppsService appsService;
|
||||
@Autowired
|
||||
private AppPrivateKeyCipher privateKeyCipher;
|
||||
|
||||
/** 未支付或未配置跳转地址时不生成地址,避免提前签发成功凭据。 */
|
||||
public String buildSignedRedirectUrl(UsdtOrdersPlus order, String authenticatedApiKey) {
|
||||
if (order == null || !Long.valueOf(2L).equals(order.getStatus())
|
||||
|| !StringUtils.hasText(order.getRedirectUrl())) {
|
||||
return null;
|
||||
}
|
||||
if (!StringUtils.hasText(authenticatedApiKey)) {
|
||||
throw new IllegalStateException("支付查询缺少已认证API Key");
|
||||
}
|
||||
AppsPlus app = appsService.getById(order.getAppId());
|
||||
if (app == null || !StringUtils.hasText(app.getPrivateKey())) {
|
||||
throw new IllegalStateException("订单所属应用没有有效RSA私钥");
|
||||
}
|
||||
try {
|
||||
PrivateKey privateKey = RsaKeyUtil.loadPrivateKey(privateKeyCipher.decrypt(app.getPrivateKey()));
|
||||
Map<String, Object> values = callbackValues(order);
|
||||
String signature = PaymentSignatureService.signRsa(values, privateKey);
|
||||
return appendCallbackParameters(order.getRedirectUrl(), values, authenticatedApiKey, signature);
|
||||
} catch (Exception exception) {
|
||||
throw new IllegalStateException("支付同步跳转签名生成失败", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> callbackValues(UsdtOrdersPlus order) {
|
||||
Map<String, Object> values = new HashMap<String, Object>();
|
||||
values.put("order_id", order.getOrderId());
|
||||
values.put("trade_id", order.getTradeId());
|
||||
values.put("status", order.getStatus());
|
||||
values.put("transaction_id", order.getBlockTransactionId());
|
||||
return values;
|
||||
}
|
||||
|
||||
static String appendCallbackParameters(String redirectUrl, Map<String, Object> values,
|
||||
String apiKey, String signature) {
|
||||
return UriComponentsBuilder.fromHttpUrl(redirectUrl)
|
||||
.queryParam("order_id", values.get("order_id"))
|
||||
.queryParam("trade_id", values.get("trade_id"))
|
||||
.queryParam("status", values.get("status"))
|
||||
.queryParam("transaction_id", values.get("transaction_id"))
|
||||
.queryParam("api_key", apiKey)
|
||||
.queryParam("signature", signature)
|
||||
.build().encode().toUriString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.ruoyi.system.service;
|
||||
|
||||
import com.ruoyi.system.domain.AppsPlus;
|
||||
import com.ruoyi.system.domain.UsdtOrdersPlus;
|
||||
import org.junit.Test;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
import org.springframework.web.util.UriUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.Signature;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PaymentRedirectServiceTest {
|
||||
@Test
|
||||
public void shouldPreserveMerchantQueryAndEncodeSignedCallbackParameters() {
|
||||
Map<String, Object> values = new HashMap<String, Object>();
|
||||
values.put("order_id", "merchant-1");
|
||||
values.put("trade_id", "trade-1");
|
||||
values.put("status", 2);
|
||||
values.put("transaction_id", "tx-1");
|
||||
|
||||
String url = PaymentRedirectService.appendCallbackParameters(
|
||||
"http://192.168.3.103:2020/system/demoUsdt/redirect?source=checkout",
|
||||
values, "ak_test", "base64+/=signature");
|
||||
org.springframework.util.MultiValueMap<String, String> query =
|
||||
UriComponentsBuilder.fromUriString(url).build().getQueryParams();
|
||||
|
||||
assertEquals("checkout", query.getFirst("source"));
|
||||
assertEquals("merchant-1", query.getFirst("order_id"));
|
||||
assertEquals("trade-1", query.getFirst("trade_id"));
|
||||
assertEquals("2", query.getFirst("status"));
|
||||
assertEquals("tx-1", query.getFirst("transaction_id"));
|
||||
assertEquals("ak_test", query.getFirst("api_key"));
|
||||
assertEquals("base64+/=signature", UriUtils.decode(query.getFirst("signature"), "UTF-8"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldBuildRedirectWhoseSignatureCanBeVerifiedByMerchantPublicKey() throws Exception {
|
||||
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||
generator.initialize(2048);
|
||||
KeyPair keyPair = generator.generateKeyPair();
|
||||
|
||||
AppPrivateKeyCipher cipher = new AppPrivateKeyCipher();
|
||||
setField(cipher, "masterKey", "test-master-key-with-at-least-32-characters");
|
||||
AppsPlus app = new AppsPlus();
|
||||
app.setPrivateKey(cipher.encrypt(Base64.getEncoder().encodeToString(keyPair.getPrivate().getEncoded())));
|
||||
IAppsService appsService = (IAppsService) Proxy.newProxyInstance(
|
||||
IAppsService.class.getClassLoader(), new Class<?>[]{IAppsService.class},
|
||||
(proxy, method, arguments) -> "getById".equals(method.getName()) ? app : null);
|
||||
|
||||
PaymentRedirectService service = new PaymentRedirectService();
|
||||
setField(service, "appsService", appsService);
|
||||
setField(service, "privateKeyCipher", cipher);
|
||||
UsdtOrdersPlus order = new UsdtOrdersPlus();
|
||||
order.setAppId(7L);
|
||||
order.setOrderId("merchant-7");
|
||||
order.setTradeId("trade-7");
|
||||
order.setStatus(2L);
|
||||
order.setBlockTransactionId("tx-7");
|
||||
order.setRedirectUrl("http://192.168.3.103:2020/system/demoUsdt/redirect");
|
||||
|
||||
String url = service.buildSignedRedirectUrl(order, "ak_7");
|
||||
org.springframework.util.MultiValueMap<String, String> query =
|
||||
UriComponentsBuilder.fromUriString(url).build().getQueryParams();
|
||||
Map<String, Object> signedValues = new HashMap<String, Object>();
|
||||
signedValues.put("order_id", query.getFirst("order_id"));
|
||||
signedValues.put("trade_id", query.getFirst("trade_id"));
|
||||
signedValues.put("status", Integer.valueOf(query.getFirst("status")));
|
||||
signedValues.put("transaction_id", query.getFirst("transaction_id"));
|
||||
|
||||
Signature verifier = Signature.getInstance("SHA256withRSA");
|
||||
verifier.initVerify(keyPair.getPublic());
|
||||
verifier.update(PaymentSignatureService.canonicalText(signedValues).getBytes(StandardCharsets.UTF_8));
|
||||
String encodedSignature = UriUtils.decode(query.getFirst("signature"), "UTF-8");
|
||||
assertTrue(verifier.verify(Base64.getDecoder().decode(encodedSignature)));
|
||||
assertEquals("ak_7", query.getFirst("api_key"));
|
||||
}
|
||||
|
||||
private static void setField(Object target, String name, Object value) throws Exception {
|
||||
Field field = target.getClass().getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
field.set(target, value);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user