|
@ -1,208 +0,0 @@
|
|
|
package gateway.processor;
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.yihu.hos.core.datatype.StringUtil;
|
|
|
import com.yihu.hos.core.http.HTTPResponse;
|
|
|
import com.yihu.hos.core.http.HttpClientKit;
|
|
|
import org.apache.camel.Body;
|
|
|
import org.apache.camel.Exchange;
|
|
|
import org.apache.camel.Processor;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
|
|
|
public class GatewayProcessor implements Processor {
|
|
|
static Map<String, AppApi> apiMap = new HashMap<>();
|
|
|
|
|
|
public void process(Exchange exchange) throws Exception {
|
|
|
}
|
|
|
|
|
|
public String secret(@Body String body, Exchange exchange) throws IOException, ParseException {
|
|
|
System.out.println(body);
|
|
|
String appKey = exchange.getIn().getHeaders().get("appKey").toString();
|
|
|
return "restlet:http://sdw2:10000/api/v1.0/admin/apps/" + appKey;
|
|
|
}
|
|
|
|
|
|
public String route(@Body Map<String, Object> body, Exchange exchange) throws IOException, ParseException {
|
|
|
// body = URLDecoder.decode(body, "UTF-8");
|
|
|
if ("true".equals(body.get("successFlg"))) {
|
|
|
return "jetty:http://0.0.0.0:9999/error/paramError"; //TODO:
|
|
|
}
|
|
|
Map<String, Object> obj = (Map) body.get("obj");
|
|
|
if (obj == null) {
|
|
|
return "jetty:http://0.0.0.0:9999/error/paramError"; //TODO:
|
|
|
}
|
|
|
String secret = obj.get("secret").toString();
|
|
|
Map<String, Object> params = exchange.getIn().getHeaders();
|
|
|
|
|
|
boolean pass = checkParams(params);
|
|
|
if (!pass) {
|
|
|
return "jetty:http://0.0.0.0:9999/error/paramError"; //TODO:
|
|
|
}
|
|
|
|
|
|
pass = checkTimeStamp(params);
|
|
|
if (!pass) {
|
|
|
return "jetty:http://0.0.0.0:9999/error/outdataError"; //TODO:
|
|
|
}
|
|
|
|
|
|
pass = checkSign(params, secret);
|
|
|
if (!pass) {
|
|
|
return "jetty:http://0.0.0.0:9999/error/signValidError"; //TODO:
|
|
|
}
|
|
|
|
|
|
pass = checkAuthorized(params);
|
|
|
if (!pass) {
|
|
|
return "jetty:http://0.0.0.0:9999/error/unauthorizedError"; //TODO:
|
|
|
}
|
|
|
|
|
|
return genEndpoint(params, exchange);
|
|
|
}
|
|
|
|
|
|
private boolean checkParams(Map<String, Object> params) {
|
|
|
Object api = params.get("api"); // API接口名称
|
|
|
Object sign = params.get("sign"); // 簽名
|
|
|
Object timestamp = params.get("timestamp"); // 时间戳
|
|
|
Object appKey = params.get("appKey"); // HOP分配给应用的AppKey ,创建应用时可获得
|
|
|
Object version = params.get("v"); // API协议版本
|
|
|
return !(StringUtil.isEmpty(api) || StringUtil.isEmpty(timestamp)
|
|
|
|| StringUtil.isEmpty(appKey) || StringUtil.isEmpty(version)
|
|
|
|| StringUtil.isEmpty(sign));
|
|
|
}
|
|
|
|
|
|
private boolean checkTimeStamp(Map<String, Object> params) throws ParseException {
|
|
|
final long ONE_MIN = 60000; //millisecond
|
|
|
|
|
|
String timestamp = params.get("timestamp").toString();
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
|
|
|
|
|
|
java.util.Date ts = format.parse(timestamp);
|
|
|
if (ts == null) {
|
|
|
return false; //时间格式不正确
|
|
|
}
|
|
|
|
|
|
Calendar date = Calendar.getInstance();
|
|
|
long timeInMillis = date.getTimeInMillis();
|
|
|
java.util.Date min = new java.util.Date(timeInMillis - 5 * ONE_MIN);
|
|
|
java.util.Date max = new java.util.Date(timeInMillis + 5 * ONE_MIN);
|
|
|
|
|
|
return ts.after(min) && ts.before(max);
|
|
|
|
|
|
}
|
|
|
|
|
|
private boolean checkSign(Map<String, Object> params, String secret) {
|
|
|
try {
|
|
|
String sign = params.get("sign").toString(); // 簽名
|
|
|
|
|
|
ParamVerifyBean paramSign = new ParamVerifyBean();
|
|
|
paramSign.addParam(params);
|
|
|
// paramSign.genParam();
|
|
|
//TODO 获取app secret传入验证
|
|
|
String md5Sign = paramSign.signParam(secret);
|
|
|
if (!md5Sign.equals(sign)) {
|
|
|
System.out.println("传递的签名:" + sign);
|
|
|
System.out.println("生成的签名:" + md5Sign);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private boolean checkAuthorized(Map<String, Object> params) {
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
HTTPResponse response = HttpClientKit.get("http://localhost:10000/api/v1.0/admin/appApiAuth?appId=" + params.get("appKey") + "&apiName="+ apiMap.get("api"));
|
|
|
if (response.getStatusCode() != 200) {
|
|
|
System.out.println("验证API权限,请求失败!");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
Map map = objectMapper.readValue(response.getBody(), Map.class);
|
|
|
if ((Boolean) map.get("successFlg")){
|
|
|
return true;
|
|
|
}else {
|
|
|
System.out.println("验证失败:"+map.get("errorMsg"));
|
|
|
return false;
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private Map<String, String> parseBody(String body) throws IOException {
|
|
|
return Stream.of(body.split("&")).map(obj -> obj.split("="))
|
|
|
.collect(Collectors.toMap(entry -> entry[0], entry -> entry[1]));
|
|
|
}
|
|
|
|
|
|
private String genEndpoint(Map<String, Object> params, Exchange exchange) {
|
|
|
String api = params.get("api").toString(); // API接口名称
|
|
|
String param = params.get("param").toString();
|
|
|
|
|
|
AppApi appApi = getApiInfo(api);
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
try {
|
|
|
if (appApi == null) {
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
JsonNode jsonNode = objectMapper.readValue(param, JsonNode.class);
|
|
|
String host = appApi.getMicroServiceUri().split(",")[0];
|
|
|
String endPoint = "restlet:http://" + host + appApi.getMethodName() + "restletMethods=" + appApi.getMethod();
|
|
|
final String[] body = {""};
|
|
|
appApi.getParameters().forEach(p -> {
|
|
|
String value = jsonNode.asText(p.getName());
|
|
|
if (p.getType().equals("2")) { //path param
|
|
|
endPoint.replace("{" + p.getName() + "}", value);
|
|
|
}
|
|
|
|
|
|
body[0] += ("&" + p.getName() + "=" + value);
|
|
|
|
|
|
});
|
|
|
|
|
|
if (body[0].length() != 0) {
|
|
|
exchange.getOut().setBody(body[0].substring(1));
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
private AppApi getApiInfo(String api) {
|
|
|
AppApi appApi = apiMap.get(api);
|
|
|
if (appApi != null) {
|
|
|
return appApi;
|
|
|
}
|
|
|
|
|
|
HTTPResponse response = HttpClientKit.get("http://localhost:10000/api/v1.0/admin/appApi/search?filters=msMethodName%3D" + api + "&size=15&page=1");
|
|
|
if (response.getStatusCode() != 200) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
appApi = AppApi.parse(response.getBody());
|
|
|
apiMap.put(api, appApi);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
return appApi;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|