huangzhiyong 8 years ago
parent
commit
fd5b0baa35

+ 57 - 9
hos-camel2/src/main/java/camel/gateway/processor/GatewayProcessor.java

@ -7,6 +7,7 @@ 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.Message;
import org.apache.camel.Processor;
import java.io.IOException;
@ -23,6 +24,11 @@ public class GatewayProcessor implements Processor {
    static Map<String, AppApi> apiMap = new HashMap<>();
    public void process(Exchange exchange) throws Exception {
        Message inMessage = exchange.getIn();
        String appKey = exchange.getIn().getHeaders().get("appKey").toString();
        String secret = getSecret(appKey);
        exchange.setOut(inMessage);
        exchange.getOut().setHeader("secret",secret);
    }
    public String secret(@Body String body, Exchange exchange) throws IOException, ParseException {
@ -31,17 +37,13 @@ public class GatewayProcessor implements Processor {
        return "restlet:http://sdw2:10000/api/v1.0/admin/apps/" + appKey;
    }
    public String route(@Body Map<String, Object> body, Exchange exchange) throws IOException, ParseException {
    public String route( 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) {
        Map<String, Object> params = exchange.getIn().getHeaders();
        String secret = params.get("secret").toString();
        if (secret==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) {
@ -120,7 +122,26 @@ public class GatewayProcessor implements Processor {
    }
    private boolean checkAuthorized(Map<String, Object> params) {
        return true;
        ObjectMapper objectMapper = new ObjectMapper();
        //TODO 设置固定的验证入口地址
        HTTPResponse response = HttpClientKit.get("http://localhost:10000/api/v1.0/admin/appApiAuth?appId=" + params.get("appKey") + "&apiName="+ params.get("api"));
        if (response.getStatusCode() != 200) {
            System.out.println("请求失败!");
            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 {
@ -185,5 +206,32 @@ public class GatewayProcessor implements Processor {
        return appApi;
    }
    private String getSecret(String appKey) {
        ObjectMapper objectMapper = new ObjectMapper();
        //TODO 设置固定的验证入口地址
        HTTPResponse response = HttpClientKit.get("http://localhost:10000/api/v1.0/admin/apps/" +appKey);
        if (response.getStatusCode() != 200) {
            System.out.println("请求失败!");
            return null;
        }
        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 null;
                }
                String secret = obj.get("secret").toString();
                return secret;
            }else {
                System.out.println("验证失败:"+map.get("errorMsg"));
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}