main
黄海 11 months ago
parent fc282bdebc
commit d7769f1f7d

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

@ -1,9 +1,8 @@
package com.dsideal.gw; package com.dsideal.gw;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import com.dsideal.gw.Controller.BaseController;
import com.dsideal.gw.Controller.IndexController; import com.dsideal.gw.Controller.IndexController;
import com.dsideal.gw.Interceptor.AuthInterceptor;
import com.dsideal.gw.Interceptor.RuteInterceptor;
import com.dsideal.gw.Util.LogBackLogFactory; import com.dsideal.gw.Util.LogBackLogFactory;
import com.jfinal.config.*; import com.jfinal.config.*;
import com.jfinal.server.undertow.UndertowServer; import com.jfinal.server.undertow.UndertowServer;
@ -63,6 +62,7 @@ public class gwApplication extends JFinalConfig {
@Override @Override
public void configRoute(Routes me) { public void configRoute(Routes me) {
me.add("/", IndexController.class); me.add("/", IndexController.class);
me.add("/base", BaseController.class);
} }
@Override @Override
@ -81,8 +81,8 @@ public class gwApplication extends JFinalConfig {
*/ */
@Override @Override
public void configInterceptor(Interceptors me) { public void configInterceptor(Interceptors me) {
me.addGlobalActionInterceptor(new AuthInterceptor()); //鉴权拦截器
me.addGlobalActionInterceptor(new RuteInterceptor()); // me.add(new AuthInterceptor());
} }
/** /**

Loading…
Cancel
Save