|
@ -0,0 +1,182 @@
|
|
|
package gateway.processor;
|
|
|
|
|
|
import com.yihu.hos.core.datatype.StringUtil;
|
|
|
import com.yihu.hos.core.encrypt.MD5;
|
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.Map;
|
|
|
import java.util.TreeMap;
|
|
|
|
|
|
/**
|
|
|
* @author HZY
|
|
|
* @vsrsion 1.0
|
|
|
* Created at 2017/3/15.
|
|
|
*/
|
|
|
public class ParamVerifyBean {
|
|
|
|
|
|
private final String version = "1.0";
|
|
|
|
|
|
private TreeMap<String, String> paramMap = new TreeMap<>();
|
|
|
private String timestamp;
|
|
|
//TODO 配置文件中配置
|
|
|
private String gatewayUrl = "seb-gatewayUrl";
|
|
|
private String api;
|
|
|
private String appKey = "esb-key";
|
|
|
private String appSecret = "esb-secret";
|
|
|
private String token;
|
|
|
|
|
|
|
|
|
public void setToken(String token) {
|
|
|
this.token = token;
|
|
|
}
|
|
|
|
|
|
public void setApi(String api) {
|
|
|
this.api = api;
|
|
|
}
|
|
|
|
|
|
public void setGatewayUrl(String gatewayUrl) {
|
|
|
this.gatewayUrl = gatewayUrl;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* TODO 无用可删除
|
|
|
* 获取完整请求地址
|
|
|
* @return
|
|
|
*/
|
|
|
private String completeUrl() {
|
|
|
try {
|
|
|
addParam("api", api, true);
|
|
|
addParam("param", genParam(), false);
|
|
|
addParam("appKey", appKey, true);
|
|
|
addParam("token", token, false);
|
|
|
addParam("v", version, true);
|
|
|
addParam("timestamp", getTimestamp(), false);
|
|
|
addParam("sign", signParam(), true);
|
|
|
|
|
|
String completeUrl = gatewayUrl + genParam();
|
|
|
return completeUrl;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
private void addParam(String paramName, String paramValue, boolean bMust) throws Exception {
|
|
|
if (StringUtil.isEmpty(paramValue)) {
|
|
|
if (bMust) {
|
|
|
throw new Exception(paramName + "参数不能为空.");
|
|
|
} else {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
String encodeValue = URLEncoder.encode(paramValue, "UTF-8");
|
|
|
paramMap.put(paramName, encodeValue);
|
|
|
}
|
|
|
|
|
|
public void addParam(Map<String,String> params) throws Exception {
|
|
|
if (params !=null) {
|
|
|
params.remove("sign");
|
|
|
Iterator<Map.Entry<String, String>> iterator = params.entrySet().iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
Map.Entry<String, String> next = iterator.next();
|
|
|
String key = next.getKey();
|
|
|
String value = next.getValue();
|
|
|
String encodeValue = URLEncoder.encode(value, "UTF-8");
|
|
|
paramMap.put(key, encodeValue);
|
|
|
}
|
|
|
|
|
|
}else {
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* sign 签名生成 ( md5(secret + params拼接字符串 + secret) )
|
|
|
* @return
|
|
|
*/
|
|
|
public String signParam() {
|
|
|
Iterator<Map.Entry<String, String>> iterator = paramMap.entrySet().iterator();
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
builder.append(appSecret);
|
|
|
while (iterator.hasNext()) {
|
|
|
Map.Entry<String, String> next = iterator.next();
|
|
|
String key = next.getKey();
|
|
|
String value = next.getValue();
|
|
|
builder.append(key);
|
|
|
builder.append(value);
|
|
|
}
|
|
|
|
|
|
builder.append(appSecret);
|
|
|
try {
|
|
|
return MD5.hash(builder.toString());
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* http请求 参数拼接
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
public String genParam() throws Exception {
|
|
|
Iterator<Map.Entry<String, String>> iterator = paramMap.entrySet().iterator();
|
|
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
while (iterator.hasNext()) {
|
|
|
Map.Entry<String, String> next = iterator.next();
|
|
|
String key = next.getKey();
|
|
|
String value = next.getValue();
|
|
|
if (value == null) {
|
|
|
throw new Exception("参数错误:参数" + key + "值为空.");
|
|
|
}
|
|
|
if (builder.length() == 0) {
|
|
|
builder.append("?");
|
|
|
} else {
|
|
|
builder.append("&");
|
|
|
}
|
|
|
builder.append(key);
|
|
|
builder.append("=");
|
|
|
builder.append(URLEncoder.encode(value, "UTF-8"));
|
|
|
}
|
|
|
|
|
|
return builder.toString();
|
|
|
}
|
|
|
|
|
|
|
|
|
//TODO 获取应用token
|
|
|
public String getToken() {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取 ISO 8601格式的时间戳
|
|
|
* @return
|
|
|
*/
|
|
|
private String getTimestamp() {
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSZ");
|
|
|
timestamp = format.format(new Date());
|
|
|
|
|
|
return timestamp;
|
|
|
}
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
ParamVerifyBean paramSignUtil = new ParamVerifyBean();
|
|
|
paramSignUtil.setApi("collect");
|
|
|
paramSignUtil.addParam("patientId","11111",true);
|
|
|
paramSignUtil.addParam("eventNo","2222222",true);
|
|
|
paramSignUtil.genParam();
|
|
|
String s = paramSignUtil.signParam();
|
|
|
System.out.println(s);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|