返回sign处理

This commit is contained in:
tanghc
2019-06-27 19:20:23 +08:00
parent 800f7e8d82
commit eb179632c8
22 changed files with 472 additions and 238 deletions

View File

@@ -5,11 +5,15 @@ import com.alibaba.fastjson.JSONObject;
import com.gitee.sop.sdk.common.DataNameBuilder;
import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.RequestForm;
import com.gitee.sop.sdk.common.SopSdkConstants;
import com.gitee.sop.sdk.common.SopSdkErrors;
import com.gitee.sop.sdk.exception.SdkException;
import com.gitee.sop.sdk.request.BaseRequest;
import com.gitee.sop.sdk.response.BaseResponse;
import com.gitee.sop.sdk.response.ErrorResponse;
import com.gitee.sop.sdk.sign.SopSignException;
import com.gitee.sop.sdk.sign.SopSignature;
import com.gitee.sop.sdk.sign.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -25,33 +29,46 @@ public class OpenClient {
private static final Log log = LogFactory.getLog(OpenClient.class);
private static final OpenConfig DEFAULT_CONFIG = new OpenConfig();
private static final String ERROR_RESPONSE_KEY = "error_response";
private String url;
private String appId;
private String privateKey;
/**
* 开放平台提供的公钥
*/
private String publicKeyPlatform;
private OpenConfig openConfig;
private OpenRequest openRequest;
private DataNameBuilder dataNameBuilder;
public OpenClient(String url, String appId, String privateKey) {
this(url, appId, privateKey, DEFAULT_CONFIG);
public OpenClient(String url, String appId, String privateKeyIsv) {
this(url, appId, privateKeyIsv, DEFAULT_CONFIG);
}
public OpenClient(String url, String appId, String privateKey, OpenConfig openConfig) {
public OpenClient(String url, String appId, String privateKeyIsv, String publicKeyPlatform) {
this(url, appId, privateKeyIsv);
this.publicKeyPlatform = publicKeyPlatform;
}
public OpenClient(String url, String appId, String privateKeyIsv, OpenConfig openConfig) {
if (openConfig == null) {
throw new IllegalArgumentException("openConfig不能为null");
}
this.url = url;
this.appId = appId;
this.privateKey = privateKey;
this.privateKey = privateKeyIsv;
this.openConfig = openConfig;
this.openRequest = new OpenRequest(openConfig);
this.dataNameBuilder = openConfig.getDataNameBuilder();
}
public OpenClient(String url, String appId, String privateKeyIsv, String publicKeyPlatform, OpenConfig openConfig) {
this(url, appId, privateKeyIsv, openConfig);
this.publicKeyPlatform = publicKeyPlatform;
}
/**
* 请求接口
*
@@ -83,7 +100,7 @@ public class OpenClient {
String content = SopSignature.getSignContent(form);
String sign = null;
try {
sign = SopSignature.rsa256Sign(content, privateKey, "utf-8");
sign = SopSignature.rsaSign(content, privateKey, openConfig.getCharset(), openConfig.getSignType());
} catch (SopSignException e) {
throw new SdkException("构建签名错误", e);
}
@@ -109,17 +126,61 @@ public class OpenClient {
protected <T extends BaseResponse> T parseResponse(String resp, BaseRequest<T> request) {
String method = request.getMethod();
String dataName = dataNameBuilder.build(method);
String rootNodeName = dataNameBuilder.build(method);
JSONObject jsonObject = JSON.parseObject(resp);
boolean errorResponse = jsonObject.containsKey(ERROR_RESPONSE_KEY);
String errorResponseName = this.openConfig.getErrorResponseName();
boolean errorResponse = jsonObject.containsKey(errorResponseName);
if (errorResponse) {
dataName = ERROR_RESPONSE_KEY;
rootNodeName = errorResponseName;
}
JSONObject data = jsonObject.getJSONObject(rootNodeName);
String sign = jsonObject.getString(openConfig.getSignName());
// 是否要验证返回的sign
if (StringUtils.areNotEmpty(sign, publicKeyPlatform)) {
String signContent = buildBizJson(rootNodeName, resp);
if (!this.checkResponseSign(signContent, sign, publicKeyPlatform)) {
ErrorResponse error = SopSdkErrors.CHECK_RESPONSE_SIGN_ERROR.getErrorResponse();
data = JSON.parseObject(JSON.toJSONString(error));
}
}
JSONObject data = jsonObject.getJSONObject(dataName);
T t = data.toJavaObject(request.getResponseClass());
t.setBody(data.toJSONString());
return t;
}
protected String buildBizJson(String rootNodeName, String body) {
int indexOfRootNode = body.indexOf(rootNodeName);
if (indexOfRootNode < 0) {
rootNodeName = SopSdkConstants.ERROR_RESPONSE_KEY;
indexOfRootNode = body.indexOf(rootNodeName);
}
String result = null;
if (indexOfRootNode > 0) {
result = buildJsonNodeData(body, rootNodeName, indexOfRootNode);
}
return result;
}
protected String buildJsonNodeData(String body, String rootNodeName, int indexOfRootNode) {
int signDataStartIndex = indexOfRootNode + rootNodeName.length() + 2;
int indexOfSign = body.indexOf("\"" + openConfig.getSignName() + "\"");
if (indexOfSign < 0) {
return null;
}
int length = indexOfSign - 1;
return body.substring(signDataStartIndex, length);
}
protected <T extends BaseResponse> boolean checkResponseSign(String signContent, String sign, String publicKeyPlatform) {
try {
String charset = this.openConfig.getCharset();
String signType = this.openConfig.getSignType();
return SopSignature.rsaCheck(signContent, sign, publicKeyPlatform, charset, signType);
} catch (SopSignException e) {
log.error("验证服务端sign出错signContent" + signContent, e);
return false;
}
}
}

View File

@@ -4,8 +4,9 @@ import com.alibaba.fastjson.JSON;
import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.RequestForm;
import com.gitee.sop.sdk.common.RequestMethod;
import com.gitee.sop.sdk.common.SopSdkErrors;
import com.gitee.sop.sdk.common.UploadFile;
import com.gitee.sop.sdk.response.BaseResponse;
import com.gitee.sop.sdk.response.ErrorResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@@ -20,8 +21,6 @@ import java.util.Map;
*/
public class OpenRequest {
private static final String HTTP_ERROR_CODE = "-400";
private OpenHttp openHttp;
public OpenRequest(OpenConfig openConfig) {
@@ -69,14 +68,7 @@ public class OpenRequest {
}
protected String causeException(Exception e) {
ErrorResponse result = new ErrorResponse();
result.setCode(HTTP_ERROR_CODE);
result.setSubCode(HTTP_ERROR_CODE);
result.setSubMsg(e.getMessage());
result.setMsg(e.getMessage());
ErrorResponse result = SopSdkErrors.HTTP_ERROR.getErrorResponse();
return JSON.toJSONString(result);
}
static class ErrorResponse extends BaseResponse {
}
}

View File

@@ -19,7 +19,7 @@ package com.gitee.sop.sdk.common;
public class DefaultDataNameBuilder implements DataNameBuilder {
private static final char DOT = '.';
private static final char UNDERLINE = '_';
private static final String DATA_SUFFIX = "_response";
private static final String DATA_SUFFIX = SopSdkConstants.DATA_SUFFIX;
@Override
public String build(String method) {

View File

@@ -7,10 +7,21 @@ import lombok.Data;
*/
@Data
public class OpenConfig {
public static DataNameBuilder DATA_NAME_BUILDER = new DefaultDataNameBuilder();
/** 成功返回码值 */
private String successCode = SdkConfig.SUCCESS_CODE;
private String successCode = "10000";
/** 默认版本号 */
private String defaultVersion = SdkConfig.DEFAULT_VERSION;
private String defaultVersion = "1.0";
/** 字符编码 */
private String charset = "UTF-8";
/** 签名方式 */
private String signType = "RSA2";
/** 格式类型名称 */
private String formatType = "json";
/** 时间戳格式 */
private String timestampPattern = "yyyy-MM-dd HH:mm:ss";
/** 接口属性名 */
private String methodName = "method";
/** 版本号名称 */
@@ -23,22 +34,21 @@ public class OpenConfig {
private String dataName = "biz_content";
/** 时间戳名称 */
private String timestampName = "timestamp";
/** 时间戳格式 */
private String timestampPattern = "yyyy-MM-dd HH:mm:ss";
/** 签名串名称 */
private String signName = "sign";
/** 签名类型名称 */
private String signTypeName = "sign_type";
/** 格式化名称 */
private String formatName = "format";
/** 格式类型名称 */
private String formatType = "json";
/** accessToken名称 */
private String accessTokenName = "app_auth_token";
/** 国际化语言 */
private String locale = "zh-CN";
/** 响应code名称 */
private String responseCodeName = "code";
/** 错误响应节点 */
private String errorResponseName = "error_response";
/** 请求超时时间 */
private int connectTimeoutSeconds = 10;
/** http读取超时时间 */
@@ -49,5 +59,5 @@ public class OpenConfig {
/**
* 构建数据节点名称
*/
private DataNameBuilder dataNameBuilder = SdkConfig.dataNameBuilder;
private DataNameBuilder dataNameBuilder = DATA_NAME_BUILDER;
}

View File

@@ -1,5 +1,9 @@
package com.gitee.sop.sdk.common;
/**
* @deprecated 已废弃使用com.gitee.sop.sdk.common.OpenConfig
*/
@Deprecated
public class SdkConfig {
public static String SUCCESS_CODE = "10000";

View File

@@ -0,0 +1,7 @@
package com.gitee.sop.sdk.common;
public class SopSdkConstants {
public static String DATA_SUFFIX = "_response";
public static String ERROR_RESPONSE_KEY = "error_response";
}

View File

@@ -0,0 +1,56 @@
package com.gitee.sop.sdk.common;
import com.gitee.sop.sdk.response.ErrorResponse;
/**
* @author tanghc
*/
public enum SopSdkErrors {
/**
* 网络错误
*/
HTTP_ERROR("836875001", "网络错误"),
/**
* 验证返回sign错误
*/
CHECK_RESPONSE_SIGN_ERROR("836875002", "验证服务端sign出错")
;
SopSdkErrors(String code, String msg) {
this.code = code;
this.msg = msg;
this.subCode = code;
this.subMsg = msg;
}
public ErrorResponse getErrorResponse() {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setCode(code);
errorResponse.setSubCode(subCode);
errorResponse.setSubMsg(subMsg);
errorResponse.setMsg(msg);
return errorResponse;
}
private String code;
private String msg;
private String subCode;
private String subMsg;
public String getCode() {
return code;
}
public String getMsg() {
return msg;
}
public String getSubCode() {
return subCode;
}
public String getSubMsg() {
return subMsg;
}
}

View File

@@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON;
import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.RequestForm;
import com.gitee.sop.sdk.common.RequestMethod;
import com.gitee.sop.sdk.common.SdkConfig;
import com.gitee.sop.sdk.common.UploadFile;
import com.gitee.sop.sdk.response.BaseResponse;
import com.gitee.sop.sdk.util.ClassUtil;
@@ -36,10 +35,6 @@ import java.util.Map;
public abstract class BaseRequest<T extends BaseResponse> {
private String method;
private String format = SdkConfig.FORMAT_TYPE;
private String charset = SdkConfig.CHARSET;
private String signType = SdkConfig.SIGN_TYPE;
private String timestamp = new SimpleDateFormat(SdkConfig.TIMESTAMP_PATTERN).format(new Date());
private String version;
private String bizContent;
@@ -50,11 +45,10 @@ public abstract class BaseRequest<T extends BaseResponse> {
*/
private List<UploadFile> files;
private Class<T> responseClass;
private Class<T> responseClass = (Class<T>) ClassUtil.getSuperClassGenricType(this.getClass(), 0);;
protected abstract String method();
@SuppressWarnings("unchecked")
public BaseRequest() {
this.setMethodVersion(this.method(), this.version());
}
@@ -65,16 +59,16 @@ public abstract class BaseRequest<T extends BaseResponse> {
private void setMethodVersion(String method, String version) {
this.method = method;
this.version = version == null ? SdkConfig.DEFAULT_VERSION : version;
this.responseClass = (Class<T>) ClassUtil.getSuperClassGenricType(this.getClass(), 0);
this.version = version;
}
protected String version() {
return SdkConfig.DEFAULT_VERSION;
return null;
}
/**
* 添加上传文件
*
* @param file
*/
public void addFile(UploadFile file) {
@@ -88,11 +82,13 @@ public abstract class BaseRequest<T extends BaseResponse> {
// 公共请求参数
Map<String, String> params = new HashMap<String, String>();
params.put(openConfig.getMethodName(), this.method);
params.put(openConfig.getFormatName(), this.format);
params.put(openConfig.getCharsetName(), this.charset);
params.put(openConfig.getSignTypeName(), this.signType);
params.put(openConfig.getTimestampName(), this.timestamp);
params.put(openConfig.getVersionName(), this.version);
params.put(openConfig.getFormatName(), openConfig.getFormatType());
params.put(openConfig.getCharsetName(), openConfig.getCharset());
params.put(openConfig.getSignTypeName(), openConfig.getSignType());
String timestamp = new SimpleDateFormat(openConfig.getTimestampPattern()).format(new Date());
params.put(openConfig.getTimestampName(), timestamp);
String v = this.version == null ? openConfig.getDefaultVersion() : this.version;
params.put(openConfig.getVersionName(), v);
// 业务参数
String biz_content = buildBizContent();
@@ -101,7 +97,7 @@ public abstract class BaseRequest<T extends BaseResponse> {
RequestForm requestForm = new RequestForm(params);
requestForm.setRequestMethod(getRequestMethod());
requestForm.setCharset(this.charset);
requestForm.setCharset(openConfig.getCharset());
requestForm.setFiles(this.files);
return requestForm;
}
@@ -120,6 +116,7 @@ public abstract class BaseRequest<T extends BaseResponse> {
/**
* 指定版本号
*
* @param version
*/
public void setVersion(String version) {
@@ -144,9 +141,11 @@ public abstract class BaseRequest<T extends BaseResponse> {
/**
* 指定HTTP请求method,默认POST
*
* @return
*/
protected RequestMethod getRequestMethod() {
return RequestMethod.POST;
}
}

View File

@@ -0,0 +1,7 @@
package com.gitee.sop.sdk.response;
/**
* @author tanghc
*/
public class ErrorResponse extends BaseResponse {
}

View File

@@ -53,14 +53,14 @@ public class SopSignature {
* @param sortedParams
* @return
*/
public static String getSignContent(Map<String, String> sortedParams) {
public static String getSignContent(Map<String, ?> sortedParams) {
StringBuffer content = new StringBuffer();
List<String> keys = new ArrayList<String>(sortedParams.keySet());
Collections.sort(keys);
int index = 0;
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = sortedParams.get(key);
String value = String.valueOf(sortedParams.get(key));
if (StringUtils.areNotEmpty(key, value)) {
content.append((index == 0 ? "" : "&") + key + "=" + value);
index++;
@@ -210,7 +210,7 @@ public class SopSignature {
return content.toString();
}
public static String getSignCheckContentV2(Map<String, String> params) {
public static String getSignCheckContentV2(Map<String, ?> params) {
if (params == null) {
return null;
}
@@ -223,7 +223,7 @@ public class SopSignature {
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = params.get(key);
String value = String.valueOf(params.get(key));
content.append((i == 0 ? "" : "&") + key + "=" + value);
}
@@ -254,9 +254,9 @@ public class SopSignature {
return rsaCheckContent(content, sign, publicKey, charset);
}
public static boolean rsaCheckV2(Map<String, String> params, String publicKey,
public static boolean rsaCheckV2(Map<String, ?> params, String publicKey,
String charset,String signType) throws SopSignException {
String sign = params.get("sign");
String sign = String.valueOf(params.get("sign"));
String content = getSignCheckContentV2(params);
return rsaCheck(content, sign, publicKey, charset,signType);

View File

@@ -24,12 +24,14 @@ import java.util.Map;
public class SdkTest extends TestCase {
String url = "http://localhost:8081/api"; // zuul
String appId = "2019032617262200001";
// 支付宝私钥
String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCXJv1pQFqWNA/++OYEV7WYXwexZK/J8LY1OWlP9X0T6wHFOvxNKRvMkJ5544SbgsJpVcvRDPrcxmhPbi/sAhdO4x2PiPKIz9Yni2OtYCCeaiE056B+e1O2jXoLeXbfi9fPivJZkxH/tb4xfLkH3bA8ZAQnQsoXA0SguykMRZntF0TndUfvDrLqwhlR8r5iRdZLB6F8o8qXH6UPDfNEnf/K8wX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oo+k5/uEInAo7lyEwpV0EuUMwLA/HCqUgR4K9pyYV+Oyb8d6f0+Hz0BMD92I2pqlXrD7xV2WzDvyXM3s63NvorRooKcyfd9i6ccMjAyTR2qfLkxv0hlbBbsPHz4BbU63xhTJp3Ghi0/ey/1HQKBgQC2VsgqC6ykfSidZUNLmQZe3J0p/Qf9VLkfrQ+xaHapOs6AzDU2H2osuysqXTLJHsGfrwVaTs00ER2z8ljTJPBUtNtOLrwNRlvgdnzyVAKHfOgDBGwJgiwpeE9voB1oAV/mXqSaUWNnuwlOIhvQEBwekqNyWvhLqC7nCAIhj3yvNQKBgQCqYbeec56LAhWP903Zwcj9VvG7sESqXUhIkUqoOkuIBTWFFIm54QLTA1tJxDQGb98heoCIWf5x/A3xNI98RsqNBX5JON6qNWjb7/dobitti3t99v/ptDp9u8JTMC7penoryLKK0Ty3bkan95Kn9SC42YxaSghzqkt+uvfVQgiNGQKBgGxU6P2aDAt6VNwWosHSe+d2WWXt8IZBhO9d6dn0f7ORvcjmCqNKTNGgrkewMZEuVcliueJquR47IROdY8qmwqcBAN7Vg2K7r7CPlTKAWTRYMJxCT1Hi5gwJb+CZF3+IeYqsJk2NF2s0w5WJTE70k1BSvQsfIzAIDz2yE1oPHvwVAoGAA6e+xQkVH4fMEph55RJIZ5goI4Y76BSvt2N5OKZKd4HtaV+eIhM3SDsVYRLIm9ZquJHMiZQGyUGnsvrKL6AAVNK7eQZCRDk9KQz+0GKOGqku0nOZjUbAu6A2/vtXAaAuFSFx1rUQVVjFulLexkXR3KcztL1Qu2k5pB6Si0K/uwQ=";
/** 开放平台提供的公钥
* 前往SOP-ADMINISV管理--秘钥管理,生成平台提供的公私钥,然后把【平台公钥】放到这里 */
String publicKeyPlatform = "";
// 声明一个就行
OpenClient client = new OpenClient(url, appId, privateKey);
OpenClient client = new OpenClient(url, appId, privateKeyIsv, publicKeyPlatform);
// 标准用法
@Test