同步跳转验签
This commit is contained in:
1 parent
454da449c4
commit
361bfd6421
2 files changed
+19
-6
No files matched your search
@@ -8,6 +8,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.PrivateKey;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -54,13 +56,23 @@ public class PaymentRedirectService {
|
||||
|
||||
static String appendCallbackParameters(String redirectUrl, Map<String, Object> values,
|
||||
String apiKey, String signature) {
|
||||
return UriComponentsBuilder.fromHttpUrl(redirectUrl)
|
||||
String unsignedUrl = 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();
|
||||
try {
|
||||
// 标准Base64包含“+”。它在查询参数中会按空格解码,必须使用表单规则编码为%2B。
|
||||
String encodedSignature = URLEncoder.encode(signature, "UTF-8");
|
||||
int fragmentIndex = unsignedUrl.indexOf('#');
|
||||
String fragment = fragmentIndex < 0 ? "" : unsignedUrl.substring(fragmentIndex);
|
||||
String urlWithoutFragment = fragmentIndex < 0 ? unsignedUrl : unsignedUrl.substring(0, fragmentIndex);
|
||||
return urlWithoutFragment + (urlWithoutFragment.contains("?") ? "&" : "?")
|
||||
+ "signature=" + encodedSignature + fragment;
|
||||
} catch (UnsupportedEncodingException exception) {
|
||||
throw new IllegalStateException("UTF-8 URL编码不可用", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,10 @@ 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.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
@@ -21,7 +21,7 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PaymentRedirectServiceTest {
|
||||
@Test
|
||||
public void shouldPreserveMerchantQueryAndEncodeSignedCallbackParameters() {
|
||||
public void shouldPreserveMerchantQueryAndEncodeSignedCallbackParameters() throws Exception {
|
||||
Map<String, Object> values = new HashMap<String, Object>();
|
||||
values.put("order_id", "merchant-1");
|
||||
values.put("trade_id", "trade-1");
|
||||
@@ -40,7 +40,8 @@ public class PaymentRedirectServiceTest {
|
||||
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"));
|
||||
assertTrue(url.contains("signature=base64%2B%2F%3Dsignature"));
|
||||
assertEquals("base64+/=signature", URLDecoder.decode(query.getFirst("signature"), "UTF-8"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,7 +81,7 @@ public class PaymentRedirectServiceTest {
|
||||
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");
|
||||
String encodedSignature = URLDecoder.decode(query.getFirst("signature"), "UTF-8");
|
||||
assertTrue(verifier.verify(Base64.getDecoder().decode(encodedSignature)));
|
||||
assertEquals("ak_7", query.getFirst("api_key"));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user