|
@ -0,0 +1,77 @@
|
|
|
package camel.gateway.processor;
|
|
|
|
|
|
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.Exchange;
|
|
|
import org.apache.camel.Message;
|
|
|
import org.apache.camel.Processor;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 测试使用,用户获取sign
|
|
|
*/
|
|
|
public class SignProcessor implements Processor {
|
|
|
static final String agUrl = "http://192.168.1.221:10000";
|
|
|
|
|
|
public void process(Exchange exchange) throws Exception {
|
|
|
Message outMessage = exchange.getOut();
|
|
|
Message inMessage = exchange.getIn();
|
|
|
String appKey = inMessage.getHeaders().get("appKey").toString();
|
|
|
String secret = getSecret(appKey);
|
|
|
if (StringUtil.isEmpty(secret)){
|
|
|
outMessage.setBody("获取secret失败,APPKey不存在对应的应用");
|
|
|
}else {
|
|
|
Map<String, Object> params = inMessage.getHeaders();
|
|
|
String sign = getSign(params, secret);
|
|
|
outMessage.setBody("sign,"+sign);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private String getSecret(String appKey) {
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
//TODO 设置固定的验证入口地址
|
|
|
HTTPResponse response = HttpClientKit.get(agUrl + "/api/v1.0/admin/apps/" + appKey);
|
|
|
if (response.getStatusCode() != 200) {
|
|
|
System.out.println("获取app的secret请求失败!");
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
Map map = objectMapper.readValue(response.getBody(), Map.class);
|
|
|
if ((Boolean) map.get("successFlg")) {
|
|
|
Map<String, Object> obj = (Map) map.get("obj");
|
|
|
if (obj == null) {
|
|
|
return "";
|
|
|
}
|
|
|
String secret = obj.get("secret").toString();
|
|
|
return secret;
|
|
|
} else {
|
|
|
System.out.println("验证失败:" + map.get("errorMsg"));
|
|
|
return "";
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
return "";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private String getSign(Map<String, Object> params, String secret) {
|
|
|
try {
|
|
|
ParamVerifyBean paramSign = new ParamVerifyBean();
|
|
|
paramSign.addParam(params);
|
|
|
String md5Sign = paramSign.signParam(secret);
|
|
|
System.out.println("生成的签名:" + md5Sign);
|
|
|
|
|
|
return md5Sign;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|