|
|
|
@ -1,8 +1,6 @@
|
|
|
|
|
package com.dsideal.gw.Interceptor;
|
|
|
|
|
package com.dsideal.gw.Controller;
|
|
|
|
|
|
|
|
|
|
import com.dsideal.gw.gwApplication;
|
|
|
|
|
import com.jfinal.aop.Interceptor;
|
|
|
|
|
import com.jfinal.aop.Invocation;
|
|
|
|
|
import com.jfinal.core.Controller;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
|
|
|
|
@ -10,14 +8,11 @@ import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class RuteInterceptor implements Interceptor {
|
|
|
|
|
public class BaseController extends Controller {
|
|
|
|
|
private final OkHttpClient client = new OkHttpClient();
|
|
|
|
|
private final List<Map<String, String>> routes;
|
|
|
|
|
Map<String, Object> jfinalConfig = (Map<String, Object>) gwApplication.yamlConfig.get("jfinal");
|
|
|
|
|
private final List<Map<String, String>> routes = (List<Map<String, String>>) jfinalConfig.get("routes");
|
|
|
|
|
|
|
|
|
|
public RuteInterceptor() {
|
|
|
|
|
Map<String, Object> jfinalConfig = (Map<String, Object>) gwApplication.yamlConfig.get("jfinal");
|
|
|
|
|
routes = (List<Map<String, String>>) jfinalConfig.get("routes");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:路由
|
|
|
|
@ -37,31 +32,26 @@ public class RuteInterceptor implements Interceptor {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void intercept(Invocation inv) {
|
|
|
|
|
Controller controller = inv.getController();
|
|
|
|
|
String path = controller.getRequest().getRequestURI();
|
|
|
|
|
if(path.equals("/")){
|
|
|
|
|
inv.invoke();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
public void index() {
|
|
|
|
|
String path = getRequest().getRequestURI();
|
|
|
|
|
String serviceUrl = route(path); // 根据路径决定转发到哪个微服务
|
|
|
|
|
|
|
|
|
|
if (serviceUrl == null) {
|
|
|
|
|
controller.renderJson("error", "服务未找到");
|
|
|
|
|
renderJson("error", "服务未找到");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
//方法类型
|
|
|
|
|
String method = controller.getRequest().getMethod();
|
|
|
|
|
String method = getRequest().getMethod();
|
|
|
|
|
//传递jwt token
|
|
|
|
|
Request.Builder requestBuilder = new Request.Builder()
|
|
|
|
|
.url(serviceUrl)
|
|
|
|
|
.header("Authorization", controller.getHeader("Authorization"));
|
|
|
|
|
|
|
|
|
|
.url(serviceUrl);
|
|
|
|
|
if (getHeader("Authorization") != null) {
|
|
|
|
|
requestBuilder.header("Authorization", getHeader("Authorization"));
|
|
|
|
|
}
|
|
|
|
|
if ("POST".equalsIgnoreCase(method)) {
|
|
|
|
|
String jsonPayload = controller.getRawData(); // 获取请求体中的JSON数据
|
|
|
|
|
String jsonPayload = getRawData(); // 获取请求体中的JSON数据
|
|
|
|
|
MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
|
|
|
|
RequestBody body = RequestBody.create(JSON, jsonPayload);
|
|
|
|
|
requestBuilder.post(body);
|
|
|
|
@ -72,15 +62,16 @@ public class RuteInterceptor implements Interceptor {
|
|
|
|
|
|
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
|
if (response.body() != null) {
|
|
|
|
|
controller.renderJson(response.body().string());
|
|
|
|
|
renderJson(response.body().string());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
controller.renderJson("error", "服务请求失败");
|
|
|
|
|
renderJson("error", "服务请求失败");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
renderJson("error", "服务请求失败");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
inv.invoke();
|
|
|
|
|
renderText("Welcome to Base World." + getRequest().getRequestURI());
|
|
|
|
|
}
|
|
|
|
|
}
|