restful介面返回JSONObject和父類抽象實現類設計,請求頭獲取sign和支付寶RSA簽名驗籤工具類方法

oktokeep發表於2024-06-08

restful介面返回JSONObject和父類抽象實現類設計,請求頭獲取sign和支付寶RSA簽名驗籤工具類方法

1.
JSONObject可以通用資料的靈活性,類似Map資料,資料欄位不清晰。具體返回類,如:AutoPayResultVo
// http響應結果載體
JSONObject result = new JSONObject();
// 業務處理結果載體
JSONObject response = new JSONObject();

response.put("code", "40004");
response.put("msg", "Business Failed");
response.put("sub_code", "ISV-VERIFICATION-FAILED");
response.put("sub_msg", "驗籤失敗");

result.put("response",response);


2.父類實現通用介面設計,以下虛擬碼。
//2.1使用父類Object
private void saveSpiAndOpenApiService(Object request, Object result) {
//instanceof判斷
if(request instanceof CreateRequest) {

}

}

//2.2使用父類BaseOutResponseVo,獲取簽名方法
private BaseSignResponseVo getSign(BaseOutResponseVo response) throws AlipayApiException {

}

//子類實現類
CreateResponse extends BaseOutResponseVo
ReleaseResponse extends BaseOutResponseVo
SyncResponse extends BaseOutResponseVo


//呼叫,呼叫獲取簽名
getSign(createResponse);
getSign(releaseResponse);
getSign(syncResponse);


3.HttpServletRequest獲取header引數 sign
//介面增加 HttpServletRequest request, 引數,也可以不寫,如果需要從請求頭中獲取引數,則需要該request引數。
@RequestMapping(value = "/public/pay/payBatch",method=RequestMethod.POST)
public Response<AutoPayResultVo> pay(@Valid HttpServletRequest request, @RequestBody PayVo payVo,BindingResult result){

//HttpServletRequest獲取header引數 sign
Map<String, String> headers = getHeaders(request);
String signStr = headers.get("byte-signature");

}

//從請求頭中獲取引數
private static Map<String, String> getHeaders(HttpServletRequest request) {
Map<String, String> headerMap = new HashMap<>();
Enumeration<String> enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String name = enumeration.nextElement();
String value = request.getHeader(name);
headerMap.put(name, value);
}
return headerMap;
}


4.
支付寶sdk簽名和驗籤,可以按工具類來使用。

compile("com.alipay.sdk:alipay-sdk-java:4.39.70.ALL")


商戶 》》支付寶
//商戶私鑰簽名
String sign = AlipaySignature.rsaSign(contentToSignJson, isvPrivateKey, "UTF-8", "RSA2");
//商戶公鑰驗籤
boolean isPass = AlipaySignature.rsaCheck(GsonUtils.toJson(request), sign, alipayPublicKey, "UTF-8","RSA2");

支付寶 》》 商戶
//支付寶私鑰簽名
//支付寶公鑰解密

相關文章