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.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());
}
}

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

Loading…
Cancel
Save