mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-23 10:31:52 +08:00
1.新增支付验签方式
2.订单参数构造类 3.REA证书验签新增Certificate支持
This commit is contained in:
@@ -17,7 +17,10 @@ import org.slf4j.LoggerFactory;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.egzosn.pay.common.bean.BillType;
|
||||
import com.egzosn.pay.common.bean.MethodType;
|
||||
import com.egzosn.pay.common.bean.NoticeParams;
|
||||
import com.egzosn.pay.common.bean.NoticeRequest;
|
||||
import com.egzosn.pay.common.bean.Order;
|
||||
import com.egzosn.pay.common.bean.OrderParaStructure;
|
||||
import com.egzosn.pay.common.bean.PayMessage;
|
||||
import com.egzosn.pay.common.bean.PayOrder;
|
||||
import com.egzosn.pay.common.bean.PayOutMessage;
|
||||
@@ -119,6 +122,7 @@ public abstract class BasePayService<PC extends PayConfigStorage> implements Pay
|
||||
return base64ClientID;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建签名
|
||||
*
|
||||
@@ -395,6 +399,17 @@ public abstract class BasePayService<PC extends PayConfigStorage> implements Pay
|
||||
interceptors.add(interceptor);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将请求参数或者请求流转化为 Map
|
||||
*
|
||||
* @param request 通知请求
|
||||
* @return 获得回调的请求参数
|
||||
*/
|
||||
@Override
|
||||
public NoticeParams getNoticeParams(NoticeRequest request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将请求参数或者请求流转化为 Map
|
||||
*
|
||||
@@ -469,20 +484,26 @@ public abstract class BasePayService<PC extends PayConfigStorage> implements Pay
|
||||
return orderInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 过时
|
||||
* @param parameters 参数map
|
||||
* @param key key
|
||||
* @param value 值
|
||||
* @return 返回订单参数
|
||||
*/
|
||||
@Deprecated
|
||||
protected Map<String, Object> setParameters(Map<String, Object> parameters, String key, String value) {
|
||||
if (StringUtils.isNotEmpty(value)) {
|
||||
parameters.put(key, value);
|
||||
}
|
||||
return parameters;
|
||||
return OrderParaStructure.loadParameters(parameters, key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 过时
|
||||
* @param parameters 参数map
|
||||
* @param key key
|
||||
* @param order 订单对象
|
||||
* @return 返回订单参数
|
||||
*/
|
||||
protected Map<String, Object> setParameters(Map<String, Object> parameters, String key, Order order) {
|
||||
Object attr = order.getAttr(key);
|
||||
if (null != attr && !"".equals(attr)) {
|
||||
order.getAttrs().remove(key);
|
||||
parameters.put(key, attr);
|
||||
}
|
||||
return parameters;
|
||||
return OrderParaStructure.loadParameters(parameters, key, order);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ import java.util.Map;
|
||||
|
||||
import com.egzosn.pay.common.bean.BillType;
|
||||
import com.egzosn.pay.common.bean.MethodType;
|
||||
import com.egzosn.pay.common.bean.NoticeParams;
|
||||
import com.egzosn.pay.common.bean.NoticeRequest;
|
||||
import com.egzosn.pay.common.bean.PayMessage;
|
||||
import com.egzosn.pay.common.bean.PayOrder;
|
||||
import com.egzosn.pay.common.bean.PayOutMessage;
|
||||
@@ -59,14 +61,22 @@ public interface PayService<PC extends PayConfigStorage> {
|
||||
*/
|
||||
PayService setRequestTemplateConfigStorage(HttpConfigStorage configStorage);
|
||||
|
||||
/**
|
||||
* 回调校验
|
||||
* 已过时方法,详情{@link #verify(NoticeParams)}
|
||||
* @param params 回调回来的参数集
|
||||
* @return 签名校验 true通过
|
||||
*/
|
||||
@Deprecated
|
||||
boolean verify(Map<String, Object> params);
|
||||
|
||||
/**
|
||||
* 回调校验
|
||||
*
|
||||
* @param params 回调回来的参数集
|
||||
* @return 签名校验 true通过
|
||||
*/
|
||||
|
||||
boolean verify(Map<String, Object> params);
|
||||
boolean verify(NoticeParams params);
|
||||
|
||||
|
||||
/**
|
||||
@@ -114,8 +124,17 @@ public interface PayService<PC extends PayConfigStorage> {
|
||||
* @param is 请求流
|
||||
* @return 获得回调的请求参数
|
||||
*/
|
||||
@Deprecated
|
||||
Map<String, Object> getParameter2Map(Map<String, String[]> parameterMap, InputStream is);
|
||||
|
||||
/**
|
||||
* 将请求参数或者请求流转化为 Map
|
||||
*
|
||||
* @param request 通知请求
|
||||
* @return 获得回调的请求参数
|
||||
*/
|
||||
NoticeParams getNoticeParams(NoticeRequest request);
|
||||
|
||||
/**
|
||||
* 获取输出消息,用户返回给支付端
|
||||
*
|
||||
|
||||
@@ -42,6 +42,11 @@ public class NoticeParams {
|
||||
*/
|
||||
private Map<String, List<String>> headers;
|
||||
|
||||
/**
|
||||
* 附加属性
|
||||
*/
|
||||
private Map<String, Object> attr;
|
||||
|
||||
|
||||
public NoticeParams(Map<String, Object> body) {
|
||||
this.body = body;
|
||||
@@ -82,4 +87,12 @@ public class NoticeParams {
|
||||
public void setHeaders(Map<String, List<String>> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public Map<String, Object> getAttr() {
|
||||
return attr;
|
||||
}
|
||||
|
||||
public void setAttr(Map<String, Object> attr) {
|
||||
this.attr = attr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.egzosn.pay.common.bean;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.egzosn.pay.common.util.str.StringUtils;
|
||||
|
||||
/**
|
||||
* 订单参数构造器
|
||||
* @author Egan
|
||||
* <pre>
|
||||
* email egzosn@gmail.com
|
||||
* date 2021/8/16
|
||||
* </pre>
|
||||
*/
|
||||
public final class OrderParaStructure {
|
||||
private OrderParaStructure() {
|
||||
}
|
||||
|
||||
public static Map<String, Object> loadParameters(Map<String, Object> parameters, String key, String value) {
|
||||
if (StringUtils.isNotEmpty(value)) {
|
||||
parameters.put(key, value);
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public static Map<String, Object> loadParameters(Map<String, Object> parameters, String key, Order order) {
|
||||
Object attr = order.getAttr(key);
|
||||
if (null != attr && !"".equals(attr)) {
|
||||
order.getAttrs().remove(key);
|
||||
parameters.put(key, attr);
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import java.security.GeneralSecurityException;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
@@ -136,10 +137,7 @@ public class RSA {
|
||||
public static boolean verify(String content, String sign, String publicKey, String signAlgorithms, String characterEncoding) {
|
||||
try {
|
||||
PublicKey pubKey = getPublicKey(publicKey, ALGORITHM);
|
||||
java.security.Signature signature = java.security.Signature.getInstance(signAlgorithms);
|
||||
signature.initVerify(pubKey);
|
||||
signature.update(content.getBytes(characterEncoding));
|
||||
return signature.verify(Base64.decode(sign));
|
||||
return verify(content, sign, pubKey, signAlgorithms, characterEncoding);
|
||||
}
|
||||
catch (GeneralSecurityException e) {
|
||||
LOG.error("", e);
|
||||
@@ -176,6 +174,7 @@ public class RSA {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
*
|
||||
@@ -204,6 +203,20 @@ public class RSA {
|
||||
return verify(content, sign, publicKey, SIGN_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
*
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, Certificate publicKey, String characterEncoding) {
|
||||
final PublicKey pubKey = publicKey.getPublicKey();
|
||||
return verify(content, sign, pubKey, SIGN_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
/**
|
||||
* 解密
|
||||
*
|
||||
|
||||
@@ -5,93 +5,109 @@ import java.io.IOException;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.cert.Certificate;
|
||||
|
||||
public class RSA2 {
|
||||
|
||||
private static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA";
|
||||
private static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA";
|
||||
|
||||
|
||||
public static String sign(String content, String privateKey, String characterEncoding) {
|
||||
|
||||
public static String sign(String content, String privateKey, String characterEncoding) {
|
||||
|
||||
return RSA.sign(content, privateKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
return RSA.sign(content, privateKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA签名
|
||||
*
|
||||
* @param content 待签名数据
|
||||
* @param privateKey 私钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 签名值
|
||||
*/
|
||||
public static String sign(String content, PrivateKey privateKey, String characterEncoding) {
|
||||
return RSA.sign(content, privateKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* RSA签名
|
||||
* @param content 待签名数据
|
||||
* @param privateKey 私钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 签名值
|
||||
*/
|
||||
public static String sign(String content, PrivateKey privateKey ,String characterEncoding){
|
||||
return RSA.sign(content, privateKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
/**
|
||||
* RSA验签名检查
|
||||
*
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, String publicKey, String characterEncoding) {
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, String publicKey, String characterEncoding){
|
||||
|
||||
return RSA.verify(content, sign, publicKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding );
|
||||
}
|
||||
return RSA.verify(content, sign, publicKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
*
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, PublicKey publicKey, String characterEncoding) {
|
||||
return RSA.verify(content, sign, publicKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, PublicKey publicKey, String characterEncoding){
|
||||
return RSA.verify(content, sign, publicKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
/**
|
||||
* RSA验签名检查
|
||||
*
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, Certificate publicKey, String characterEncoding) {
|
||||
PublicKey pubKey = publicKey.getPublicKey();
|
||||
return verify(content, sign, pubKey, characterEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
* @param content 密文
|
||||
* @param privateKey 商户私钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 解密后的字符串
|
||||
* @throws GeneralSecurityException 解密异常
|
||||
* @throws IOException 解密异常
|
||||
*/
|
||||
public static String decrypt(String content, String privateKey, String characterEncoding) throws GeneralSecurityException, IOException {
|
||||
/**
|
||||
* 解密
|
||||
*
|
||||
* @param content 密文
|
||||
* @param privateKey 商户私钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 解密后的字符串
|
||||
* @throws GeneralSecurityException 解密异常
|
||||
* @throws IOException 解密异常
|
||||
*/
|
||||
public static String decrypt(String content, String privateKey, String characterEncoding) throws GeneralSecurityException, IOException {
|
||||
return RSA.decrypt(content, privateKey, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 得到私钥
|
||||
* @param key 密钥字符串(经过base64编码)
|
||||
* @throws GeneralSecurityException 加密异常
|
||||
* @return 私钥
|
||||
*/
|
||||
public static PrivateKey getPrivateKey(String key) throws GeneralSecurityException {
|
||||
return RSA.getPrivateKey(key);
|
||||
}
|
||||
/**
|
||||
* 得到私钥
|
||||
*
|
||||
* @param key 密钥字符串(经过base64编码)
|
||||
* @return 私钥
|
||||
* @throws GeneralSecurityException 加密异常
|
||||
*/
|
||||
public static PrivateKey getPrivateKey(String key) throws GeneralSecurityException {
|
||||
return RSA.getPrivateKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param content 加密文本
|
||||
* @param publicKey 公钥
|
||||
* @param cipherAlgorithm 算法
|
||||
* @param characterEncoding 编码类型
|
||||
* @return 加密后文本
|
||||
* @throws GeneralSecurityException 加密异常
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding ) throws GeneralSecurityException, IOException {
|
||||
return Base64.encode(RSA.encrypt(content.getBytes(characterEncoding), RSA.getPublicKey(publicKey), 2048, 11, cipherAlgorithm));
|
||||
}
|
||||
/**
|
||||
* @param content 加密文本
|
||||
* @param publicKey 公钥
|
||||
* @param cipherAlgorithm 算法
|
||||
* @param characterEncoding 编码类型
|
||||
* @return 加密后文本
|
||||
* @throws GeneralSecurityException 加密异常
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public static String encrypt(String content, String publicKey, String cipherAlgorithm, String characterEncoding) throws GeneralSecurityException, IOException {
|
||||
return Base64.encode(RSA.encrypt(content.getBytes(characterEncoding), RSA.getPublicKey(publicKey), 2048, 11, cipherAlgorithm));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user