This commit is contained in:
六如
2024-11-29 23:47:58 +08:00
parent 85b33e7c3d
commit 09330a7431
198 changed files with 1504 additions and 982 deletions

View File

@@ -41,7 +41,7 @@ public class IndexController {
* @apiNote 参数描述
<pre>
参数 类型 是否必填 最大长度 描述 示例值
app_id String 是 32 支付宝分配给开发者的应用ID 2014072300007148
app_id String 是 32 平台分配给开发者的应用ID 2014072300007148
method String 是 128 接口名称 alipay.trade.fastpay.refund.query
format String 否 40 仅支持JSON JSON
charset String 是 10 请求使用的编码格式如utf-8,gbk,gb2312等 utf-8

View File

@@ -12,7 +12,7 @@ import java.io.Serializable;
* 请求参数名定义
* <pre>
* 参数 类型 是否必填 最大长度 描述 示例值
* app_id String 是 32 支付宝分配给开发者的应用ID 2014072300007148
* app_id String 是 32 平台分配给开发者的应用ID 2014072300007148
* method String 是 128 接口名称 alipay.trade.fastpay.refund.query
* format String 否 40 仅支持JSON JSON
* charset String 是 10 请求使用的编码格式如utf-8,gbk,gb2312等 utf-8
@@ -20,7 +20,7 @@ import java.io.Serializable;
* sign String 是 344 商户请求参数的签名串,详见签名 详见示例
* timestamp String 是 19 发送请求的时间,格式"yyyy-MM-dd HH:mm:ss" 2014-07-24 03:07:50
* version String 是 3 调用的接口版本固定为1.0 1.0
* notify_url String 否 256 支付宝服务器主动通知商户服务器里指定的页面http/https路径。 http://api.test.alipay.net/atinterface/receive_notify.htm
* notify_url String 否 256 平台服务器主动通知商户服务器里指定的页面http/https路径。 http://api.test.alipay.net/atinterface/receive_notify.htm
* app_auth_token String 否 40 详见应用授权概述
* biz_content String 是 请求参数的集合,最大长度不限,除公共参数外所有请求参数都必须放在这个参数中传递,具体参照各产品快速接入文档
*</pre>
@@ -99,7 +99,7 @@ public class ApiRequest implements Serializable {
private String version;
/**
* 支付宝服务器主动通知商户服务器里指定的页面http/https路径
* 平台服务器主动通知商户服务器里指定的页面http/https路径
*
* @mock http://ww.xx.com/callback
*/

View File

@@ -1,97 +0,0 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2012 All Rights Reserved.
*/
package com.gitee.sop.gateway.service.validate.alipay;
/**
*
* @author runzhi
*/
public class AlipayConstants {
public static final String SIGN_TYPE = "sign_type";
public static final String SIGN_TYPE_RSA = "RSA";
/**
* sha256WithRsa 算法请求类型
*/
public static final String SIGN_TYPE_RSA2 = "RSA2";
public static final String SIGN_ALGORITHMS = "SHA1WithRSA";
public static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA";
public static final String ENCRYPT_TYPE_AES = "AES";
public static final String APP_ID = "app_id";
public static final String FORMAT = "format";
public static final String METHOD = "method";
public static final String TIMESTAMP = "timestamp";
public static final String VERSION = "version";
public static final String SIGN = "sign";
public static final String ALIPAY_SDK = "alipay_sdk";
public static final String ACCESS_TOKEN = "auth_token";
public static final String APP_AUTH_TOKEN = "app_auth_token";
public static final String TERMINAL_TYPE = "terminal_type";
public static final String TERMINAL_INFO = "terminal_info";
public static final String CHARSET = "charset";
public static final String NOTIFY_URL = "notify_url";
public static final String RETURN_URL = "return_url";
public static final String ENCRYPT_TYPE = "encrypt_type";
//-----===-------///
public static final String BIZ_CONTENT_KEY = "biz_content";
/** 默认时间格式 **/
public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
/** Date默认时区 **/
public static final String DATE_TIMEZONE = "GMT+8";
/** UTF-8字符集 **/
public static final String CHARSET_UTF8 = "UTF-8";
/** GBK字符集 **/
public static final String CHARSET_GBK = "GBK";
/** JSON 应格式 */
public static final String FORMAT_JSON = "json";
/** XML 应格式 */
public static final String FORMAT_XML = "xml";
/** SDK版本号 */
public static final String SDK_VERSION = "alipay-sdk-java-3.6.0.ALL";
public static final String PROD_CODE = "prod_code";
/** 老版本失败节点 */
public static final String ERROR_RESPONSE = "error_response";
/** 新版本节点后缀 */
public static final String RESPONSE_SUFFIX = "_response";
/** 加密后XML返回报文的节点名字 */
public static final String RESPONSE_XML_ENCRYPT_NODE_NAME = "response_encrypted";
/** 批量请求id **/
public static final String BATCH_REQUEST_ID = "batch_request_id";
}

View File

@@ -1,141 +0,0 @@
/**
* Alipay.com Inc.
* Copyright (c) 2004-2012 All Rights Reserved.
*/
package com.gitee.sop.gateway.service.validate.alipay;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
/**
* @author runzhi
*/
public class StreamUtil {
private StreamUtil() {
}
private static final int DEFAULT_BUFFER_SIZE = 8192;
public static void io(InputStream in, OutputStream out) throws IOException {
io(in, out, -1);
}
public static void io(InputStream in, OutputStream out, int bufferSize) throws IOException {
if (bufferSize == -1) {
bufferSize = DEFAULT_BUFFER_SIZE;
}
byte[] buffer = new byte[bufferSize];
int amount;
while ((amount = in.read(buffer)) >= 0) {
out.write(buffer, 0, amount);
}
}
public static void io(Reader in, Writer out) throws IOException {
io(in, out, -1);
}
public static void io(Reader in, Writer out, int bufferSize) throws IOException {
if (bufferSize == -1) {
bufferSize = DEFAULT_BUFFER_SIZE >> 1;
}
char[] buffer = new char[bufferSize];
int amount;
while ((amount = in.read(buffer)) >= 0) {
out.write(buffer, 0, amount);
}
}
public static OutputStream synchronizedOutputStream(OutputStream out) {
return new SynchronizedOutputStream(out);
}
public static OutputStream synchronizedOutputStream(OutputStream out, Object lock) {
return new SynchronizedOutputStream(out, lock);
}
public static String readText(InputStream in) throws IOException {
return readText(in, null, -1);
}
public static String readText(InputStream in, String encoding) throws IOException {
return readText(in, encoding, -1);
}
public static String readText(InputStream in, String encoding, int bufferSize)
throws IOException {
Reader reader = (encoding == null) ? new InputStreamReader(in) : new InputStreamReader(in,
encoding);
return readText(reader, bufferSize);
}
public static String readText(Reader reader) throws IOException {
return readText(reader, -1);
}
public static String readText(Reader reader, int bufferSize) throws IOException {
StringWriter writer = new StringWriter();
io(reader, writer, bufferSize);
return writer.toString();
}
private static class SynchronizedOutputStream extends OutputStream {
private OutputStream out;
private Object lock;
SynchronizedOutputStream(OutputStream out) {
this(out, out);
}
SynchronizedOutputStream(OutputStream out, Object lock) {
this.out = out;
this.lock = lock;
}
@Override
public void write(int datum) throws IOException {
synchronized (lock) {
out.write(datum);
}
}
@Override
public void write(byte[] data) throws IOException {
synchronized (lock) {
out.write(data);
}
}
@Override
public void write(byte[] data, int offset, int length) throws IOException {
synchronized (lock) {
out.write(data, offset, length);
}
}
@Override
public void flush() throws IOException {
synchronized (lock) {
out.flush();
}
}
@Override
public void close() throws IOException {
synchronized (lock) {
out.close();
}
}
}
}

View File

@@ -1,171 +0,0 @@
package com.gitee.sop.gateway.service.validate.alipay;
/**
* 字符串工具类。
*
* @author carver.gu
* @since 1.0, Sep 12, 2009
*/
public final class StringUtils {
private StringUtils() {
}
/**
* 检查指定的字符串是否为空。
* <ul>
* <li>SysUtils.isEmpty(null) = true</li>
* <li>SysUtils.isEmpty("") = true</li>
* <li>SysUtils.isEmpty(" ") = true</li>
* <li>SysUtils.isEmpty("abc") = false</li>
* </ul>
*
* @param value 待检查的字符串
* @return true/false
*/
public static boolean isEmpty(String value) {
if (value == null || value.length() == 0) {
return true;
}
for (int i = 0; i < value.length(); i++) {
if (!Character.isWhitespace(value.charAt(i))) {
return false;
}
}
return true;
}
/**
* 检查对象是否为数字型字符串,包含负数开头的。
*/
public static boolean isNumeric(Object obj) {
if (obj == null) {
return false;
}
char[] chars = obj.toString().toCharArray();
int length = chars.length;
if (length < 1) {
return false;
}
int i = 0;
if (length > 1 && chars[0] == '-') {
i = 1;
}
for (; i < length; i++) {
if (!Character.isDigit(chars[i])) {
return false;
}
}
return true;
}
/**
* 检查指定的字符串列表是否不为空。
*/
public static boolean areNotEmpty(String... values) {
boolean result = true;
if (values == null || values.length == 0) {
result = false;
} else {
for (String value : values) {
result &= !isEmpty(value);
}
}
return result;
}
/**
* 把通用字符编码的字符串转化为汉字编码。
*/
public static String unicodeToChinese(String unicode) {
StringBuilder out = new StringBuilder();
if (!isEmpty(unicode)) {
for (int i = 0; i < unicode.length(); i++) {
out.append(unicode.charAt(i));
}
}
return out.toString();
}
/**
* 过滤不可见字符
*/
public static String stripNonValidXMLCharacters(String input) {
if (input == null || ("".equals(input))) {
return "";
}
StringBuilder out = new StringBuilder();
char current;
for (int i = 0; i < input.length(); i++) {
current = input.charAt(i);
if ((current == 0x9) || (current == 0xA) || (current == 0xD)
|| ((current >= 0x20) && (current <= 0xD7FF))
|| ((current >= 0xE000) && (current <= 0xFFFD))
|| ((current >= 0x10000) && (current <= 0x10FFFF))) {
out.append(current);
}
}
return out.toString();
}
public static String leftPad(String str, int size, char padChar) {
if (str == null) {
return null;
} else {
int pads = size - str.length();
if (pads <= 0) {
return str;
} else {
return pads > 8192 ? leftPad(str, size, String.valueOf(padChar)) : padding(pads, padChar).concat(str);
}
}
}
public static String leftPad(String str, int size, String padStr) {
if (str == null) {
return null;
} else {
if (isEmpty(padStr)) {
padStr = " ";
}
int padLen = padStr.length();
int strLen = str.length();
int pads = size - strLen;
if (pads <= 0) {
return str;
} else if (padLen == 1 && pads <= 8192) {
return leftPad(str, size, padStr.charAt(0));
} else if (pads == padLen) {
return padStr.concat(str);
} else if (pads < padLen) {
return padStr.substring(0, pads).concat(str);
} else {
char[] padding = new char[pads];
char[] padChars = padStr.toCharArray();
for (int i = 0; i < pads; ++i) {
padding[i] = padChars[i % padLen];
}
return (new String(padding)).concat(str);
}
}
}
private static String padding(int repeat, char padChar) {
if (repeat < 0) {
throw new IndexOutOfBoundsException("Cannot pad a negative amount: " + repeat);
} else {
char[] buf = new char[repeat];
for (int i = 0; i < buf.length; ++i) {
buf[i] = padChar;
}
return new String(buf);
}
}
}

View File

@@ -1,4 +1,4 @@
package com.gitee.sop.gateway.service.validate.alipay;
package com.gitee.sop.gateway.service.validate.sign;
import com.gitee.sop.gateway.config.ApiConfig;
@@ -16,14 +16,14 @@ import java.util.HashMap;
import java.util.Map;
/**
* 支付宝签名验证实现
* 平台签名验证实现
*
* @author 六如
* @see <a href="https://docs.open.alipay.com/291/106118">支付宝签名</a>
* @see <a href="https://docs.open.alipay.com/291/106118">平台签名</a>
*/
@Slf4j
@Component
public class AlipaySigner implements Signer {
public class PlatformSigner implements Signer {
@Autowired
private ApiConfig apiConfig;
@@ -42,7 +42,7 @@ public class AlipaySigner implements Signer {
}
Map<String, String> params = buildParams(apiRequest);
try {
return AlipaySignature.rsaCheckV2(params, publicKey, charset, signType, apiConfig);
return SignUtil.rsaCheckV2(params, publicKey, charset, signType, apiConfig);
} catch (SignException e) {
ErrorEnum errorEnum = e.getErrorEnum();
log.error("验签错误, code={}, subCode={}, apiRequest={}",

View File

@@ -2,7 +2,7 @@
* Alipay.com Inc.
* Copyright (c) 2004-2012 All Rights Reserved.
*/
package com.gitee.sop.gateway.service.validate.alipay;
package com.gitee.sop.gateway.service.validate.sign;
import com.gitee.sop.gateway.config.ApiConfig;
@@ -10,6 +10,8 @@ import com.gitee.sop.gateway.exception.SignException;
import com.gitee.sop.gateway.message.ErrorEnum;
import com.gitee.sop.gateway.service.validate.SignConfig;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import javax.crypto.Cipher;
import java.io.ByteArrayInputStream;
@@ -17,6 +19,7 @@ import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
@@ -31,7 +34,13 @@ import java.util.Map;
/**
* @author runzhi
*/
public class AlipaySignature {
public class SignUtil {
private static final String SIGN_TYPE_RSA = "RSA";
private static final String SIGN_TYPE_RSA2 = "RSA2";
private static final String SIGN_ALGORITHMS = "SHA1WithRSA";
private static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA";
private static final String CHARSET_GBK = "GBK";
/**
* RSA最大加密明文大小
@@ -45,19 +54,20 @@ public class AlipaySignature {
/**
* @param sortedParams
* @return
* 返回签名内容
*
* @param params 入参
* @return 返回签名内容
*/
public static String getSignContent(Map<String, Object> sortedParams) {
StringBuffer content = new StringBuffer();
List<String> keys = new ArrayList<String>(sortedParams.keySet());
public static String getSignContent(Map<String, Object> params) {
StringBuilder content = new StringBuilder();
List<String> keys = new ArrayList<String>(params.keySet());
Collections.sort(keys);
int index = 0;
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = String.valueOf(sortedParams.get(key));
if (StringUtils.areNotEmpty(key, value)) {
content.append((index == 0 ? "" : "&") + key + "=" + value);
for (String key : keys) {
String value = String.valueOf(params.get(key));
if (!StringUtils.isAnyBlank(key, value)) {
content.append(index == 0 ? "" : "&").append(key).append("=").append(value);
index++;
}
}
@@ -67,17 +77,17 @@ public class AlipaySignature {
/**
* rsa内容签名
*
* @param content
* @param publicKey
* @param charset
* @return
* @param content 内容
* @param publicKey 公钥
* @param charset 字符集
* @return 返回签名串
*/
public static String rsaSign(String content, String publicKey, String charset,
String signType) throws SignException {
if (AlipayConstants.SIGN_TYPE_RSA.equals(signType)) {
if (SIGN_TYPE_RSA.equals(signType)) {
return rsaSign(content, publicKey, charset);
} else if (AlipayConstants.SIGN_TYPE_RSA2.equals(signType)) {
} else if (SIGN_TYPE_RSA2.equals(signType)) {
return rsa256Sign(content, publicKey, charset);
} else {
throw new SignException(ErrorEnum.ISV_INVALID_SIGNATURE_TYPE);
@@ -88,20 +98,20 @@ public class AlipaySignature {
/**
* sha256WithRsa 加签
*
* @param content
* @param privateKey
* @param charset
* @return
* @param content 内容
* @param privateKey 私钥
* @param charset 字符集
* @return 返回签名串
*/
public static String rsa256Sign(String content, String privateKey,
String charset) throws SignException {
try {
PrivateKey priKey = getPrivateKeyFromPKCS8(AlipayConstants.SIGN_TYPE_RSA,
PrivateKey priKey = getPrivateKeyFromPKCS8(SIGN_TYPE_RSA,
new ByteArrayInputStream(privateKey.getBytes()));
java.security.Signature signature = java.security.Signature
.getInstance(AlipayConstants.SIGN_SHA256RSA_ALGORITHMS);
.getInstance(SIGN_SHA256RSA_ALGORITHMS);
signature.initSign(priKey);
@@ -123,19 +133,19 @@ public class AlipaySignature {
/**
* sha1WithRsa 加签
*
* @param content
* @param publicKey
* @param charset
* @return
* @param content 内容
* @param publicKey 公钥
* @param charset 字符集
* @return 返回签名串
*/
public static String rsaSign(String content, String publicKey,
String charset) throws SignException {
try {
PrivateKey priKey = getPrivateKeyFromPKCS8(AlipayConstants.SIGN_TYPE_RSA,
PrivateKey priKey = getPrivateKeyFromPKCS8(SIGN_TYPE_RSA,
new ByteArrayInputStream(publicKey.getBytes()));
java.security.Signature signature = java.security.Signature
.getInstance(AlipayConstants.SIGN_ALGORITHMS);
.getInstance(SIGN_ALGORITHMS);
signature.initSign(priKey);
@@ -171,7 +181,7 @@ public class AlipaySignature {
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
byte[] encodedKey = StreamUtil.readText(ins, "UTF-8").getBytes();
byte[] encodedKey = IOUtils.toString(ins, StandardCharsets.UTF_8).getBytes();
encodedKey = Base64.decodeBase64(encodedKey);
@@ -253,11 +263,11 @@ public class AlipaySignature {
public static boolean rsaCheck(String content, String sign, String publicKey, String charset,
String signType) throws SignException {
if (AlipayConstants.SIGN_TYPE_RSA.equals(signType)) {
if (SIGN_TYPE_RSA.equals(signType)) {
return rsaCheckContent(content, sign, publicKey, charset);
} else if (AlipayConstants.SIGN_TYPE_RSA2.equals(signType)) {
} else if (SIGN_TYPE_RSA2.equals(signType)) {
return rsa256CheckContent(content, sign, publicKey, charset);
@@ -274,7 +284,7 @@ public class AlipaySignature {
new ByteArrayInputStream(publicKey.getBytes()));
java.security.Signature signature = java.security.Signature
.getInstance(AlipayConstants.SIGN_SHA256RSA_ALGORITHMS);
.getInstance(SIGN_SHA256RSA_ALGORITHMS);
signature.initVerify(pubKey);
@@ -297,7 +307,7 @@ public class AlipaySignature {
new ByteArrayInputStream(publicKey.getBytes()));
java.security.Signature signature = java.security.Signature
.getInstance(AlipayConstants.SIGN_ALGORITHMS);
.getInstance(SIGN_ALGORITHMS);
signature.initVerify(pubKey);
@@ -318,7 +328,7 @@ public class AlipaySignature {
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
StringWriter writer = new StringWriter();
StreamUtil.io(new InputStreamReader(ins), writer);
IOUtils.copy(new InputStreamReader(ins), writer);
byte[] encodedKey = writer.toString().getBytes();
@@ -331,7 +341,7 @@ public class AlipaySignature {
* 验签并解密
*
* @param params 参数
* @param alipayPublicKey 支付宝公钥
* @param alipayPublicKey 平台公钥
* @param cusPrivateKey 商户私钥
* @param isCheckSign 是否验签
* @param isDecrypt 是否解密
@@ -359,7 +369,7 @@ public class AlipaySignature {
* 验签并解密
*
* @param params 参数
* @param alipayPublicKey 支付宝公钥
* @param alipayPublicKey 平台公钥
* @param cusPrivateKey 商户私钥
* @param isCheckSign 是否验签
* @param isDecrypt 是否解密
@@ -388,7 +398,7 @@ public class AlipaySignature {
* <b>目前适用于公众号</b>
*
* @param bizContent 待加密签名内容
* @param alipayPublicKey 支付宝公钥
* @param alipayPublicKey 平台公钥
* @param cusPrivateKey 商户私钥
* @param charset 字符集如UTF-8, GBK, GB2312
* @param isEncrypt 是否加密true-加密 false-不加密
@@ -409,7 +419,7 @@ public class AlipaySignature {
boolean isSign) throws SignException {
StringBuilder sb = new StringBuilder();
if (StringUtils.isEmpty(charset)) {
charset = AlipayConstants.CHARSET_GBK;
charset = CHARSET_GBK;
}
sb.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>");
if (isEncrypt) {
@@ -444,7 +454,7 @@ public class AlipaySignature {
* <b>目前适用于公众号</b>
*
* @param bizContent 待加密签名内容
* @param alipayPublicKey 支付宝公钥
* @param alipayPublicKey 平台公钥
* @param cusPrivateKey 商户私钥
* @param charset 字符集如UTF-8, GBK, GB2312
* @param isEncrypt 是否加密true-加密 false-不加密
@@ -465,7 +475,7 @@ public class AlipaySignature {
boolean isSign, String signType) throws SignException {
StringBuilder sb = new StringBuilder();
if (StringUtils.isEmpty(charset)) {
charset = AlipayConstants.CHARSET_GBK;
charset = CHARSET_GBK;
}
sb.append("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>");
if (isEncrypt) {
@@ -510,9 +520,9 @@ public class AlipaySignature {
public static String rsaEncrypt(String content, String publicKey,
String charset) throws SignException {
try {
PublicKey pubKey = getPublicKeyFromX509(AlipayConstants.SIGN_TYPE_RSA,
PublicKey pubKey = getPublicKeyFromX509(SIGN_TYPE_RSA,
new ByteArrayInputStream(publicKey.getBytes()));
Cipher cipher = Cipher.getInstance(AlipayConstants.SIGN_TYPE_RSA);
Cipher cipher = Cipher.getInstance(SIGN_TYPE_RSA);
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] data = StringUtils.isEmpty(charset) ? content.getBytes()
: content.getBytes(charset);
@@ -553,9 +563,9 @@ public class AlipaySignature {
public static String rsaDecrypt(String content, String publicKey,
String charset) throws SignException {
try {
PrivateKey priKey = getPrivateKeyFromPKCS8(AlipayConstants.SIGN_TYPE_RSA,
PrivateKey priKey = getPrivateKeyFromPKCS8(SIGN_TYPE_RSA,
new ByteArrayInputStream(publicKey.getBytes()));
Cipher cipher = Cipher.getInstance(AlipayConstants.SIGN_TYPE_RSA);
Cipher cipher = Cipher.getInstance(SIGN_TYPE_RSA);
cipher.init(Cipher.DECRYPT_MODE, priKey);
byte[] encryptedData = StringUtils.isEmpty(charset)
? Base64.decodeBase64(content.getBytes())