diff --git a/ds-gw/src/main/java/com/dsideal/gw/Controller/ApiController.java b/ds-gw/src/main/java/com/dsideal/gw/Controller/ApiController.java index bb9607f9..9e550a47 100644 --- a/ds-gw/src/main/java/com/dsideal/gw/Controller/ApiController.java +++ b/ds-gw/src/main/java/com/dsideal/gw/Controller/ApiController.java @@ -1,40 +1,34 @@ package com.dsideal.gw.Controller; +import com.dsideal.gw.Start; import com.jfinal.core.Controller; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; -import org.yaml.snakeyaml.Yaml; import java.io.IOException; -import java.io.InputStream; import java.util.List; import java.util.Map; public class ApiController extends Controller { private final OkHttpClient client = new OkHttpClient(); - private List> routes; + private final List> routes; public ApiController() { - Yaml yaml = new Yaml(); - try (InputStream input = getClass().getClassLoader().getResourceAsStream("application_dev.yaml")) { - if (input == null) { - System.out.println("Sorry, unable to find routes.yaml"); - return; - } - Map>> yamlData = yaml.load(input); - routes = yamlData.get("routes"); - } catch (IOException ex) { - ex.printStackTrace(); - } + Map jfinalConfig = (Map) Start.yamlConfig.get("jfinal"); + routes = (List>) jfinalConfig.get("routes"); + } public void index() { - String path = getPara(); // 获取请求路径 - System.out.println(path); + String path = getRequest().getRequestURI(); // 获取请求路径 + if (path.equals("/")) { + renderText("Welcome to JFinal Gateway."); + return; + } String serviceUrl = route(path); // 根据路径决定转发到哪个微服务 if (serviceUrl == null) { @@ -43,7 +37,9 @@ public class ApiController extends Controller { } try { + //方法类型 String method = getRequest().getMethod(); + //传递jwt token Request.Builder requestBuilder = new Request.Builder() .url(serviceUrl) .header("Authorization", getHeader("Authorization")); @@ -59,7 +55,9 @@ public class ApiController extends Controller { Response response = client.newCall(request).execute(); if (response.isSuccessful()) { - renderJson(response.body().string()); + if (response.body() != null) { + renderJson(response.body().string()); + } } else { renderJson("error", "服务请求失败"); } @@ -68,6 +66,13 @@ public class ApiController extends Controller { } } + + /** + * 功能:路由 + * + * @param path + * @return + */ private String route(String path) { if (routes != null) { for (Map route : routes) { diff --git a/ds-gw/src/main/java/com/dsideal/gw/Interceptor/AuthInterceptor.java b/ds-gw/src/main/java/com/dsideal/gw/Interceptor/AuthInterceptor.java index c9472962..0e8cf0a8 100644 --- a/ds-gw/src/main/java/com/dsideal/gw/Interceptor/AuthInterceptor.java +++ b/ds-gw/src/main/java/com/dsideal/gw/Interceptor/AuthInterceptor.java @@ -1,18 +1,47 @@ package com.dsideal.gw.Interceptor; +import com.dsideal.gw.Start; import com.jfinal.aop.Interceptor; import com.jfinal.aop.Invocation; import com.jfinal.core.Controller; +import java.util.List; +import java.util.Map; + public class AuthInterceptor implements Interceptor { + //白名单 + private final List whitelist; + + public AuthInterceptor() { + Map jfinalConfig = (Map) Start.yamlConfig.get("jfinal"); + whitelist = (List) jfinalConfig.get("whitelist"); + } + + /** + * 功能:判断路径是否在白名单内 + * + * @param path + * @return + */ + private boolean isWhitelisted(String path) { + if (whitelist != null) { + for (String allowedPath : whitelist) { + if (path.startsWith(allowedPath)) { + return true; + } + } + } + return false; + } + @Override public void intercept(Invocation inv) { Controller controller = inv.getController(); String token = controller.getHeader("Authorization"); // 这里添加你的鉴权逻辑,例如检查 token 是否有效 - if (!isValidToken(token)) { - controller.renderJson("error", "Unauthorized"); + if (!isWhitelisted(controller.getRequest().getRequestURI()) && !isValidToken(token)) { + controller.renderJson("error", "请登录后访问此接口"); } else { inv.invoke(); } diff --git a/ds-gw/src/main/java/com/dsideal/gw/Start.java b/ds-gw/src/main/java/com/dsideal/gw/Start.java index 55808e67..f8afbd10 100644 --- a/ds-gw/src/main/java/com/dsideal/gw/Start.java +++ b/ds-gw/src/main/java/com/dsideal/gw/Start.java @@ -9,8 +9,24 @@ import com.jfinal.server.undertow.UndertowServer; import com.jfinal.template.Engine; import java.io.File; +import java.io.InputStream; +import java.util.Map; + +import org.yaml.snakeyaml.Yaml; public class Start extends JFinalConfig { + public static Map yamlConfig; + + //通过构造函数读取配置文件 + public Start() { + Yaml yaml = new Yaml(); + InputStream input = getClass().getClassLoader().getResourceAsStream("application_dev.yaml"); + if (input == null) { + System.out.println("Sorry, unable to find application.yaml"); + return; + } + yamlConfig = yaml.load(input); + } //配置文件 public static void main(String[] args) { @@ -24,6 +40,11 @@ public class Start extends JFinalConfig { public void configConstant(Constants me) { //使用LogBack me.setLogFactory(new LogBackLogFactory()); + //开发模式配置,有其它数据库配置时,参考这里的用法 + if (yamlConfig != null) { + Map jfinalConfig = (Map) yamlConfig.get("jfinal"); + me.setDevMode((boolean) jfinalConfig.get("devMode")); + } } /** diff --git a/ds-gw/src/main/resources/application_dev.yaml b/ds-gw/src/main/resources/application_dev.yaml index 361bc11a..f4adc236 100644 --- a/ds-gw/src/main/resources/application_dev.yaml +++ b/ds-gw/src/main/resources/application_dev.yaml @@ -1,8 +1,14 @@ -# 路由配置 -routes: - - prefix: /base - url: http://localhost:9002 +jfinal: + # 开发模式 + devMode: true + # 路由配置 + routes: + - prefix: /base + url: http://localhost:9002 + # 白名单 + whitelist: + # 根 + - / + # 登录接口 + - /base/doLogin -# 白名单 -whitelist: - - /base/doLogin diff --git a/ds-gw/target/classes/application_dev.yaml b/ds-gw/target/classes/application_dev.yaml index 361bc11a..f4adc236 100644 --- a/ds-gw/target/classes/application_dev.yaml +++ b/ds-gw/target/classes/application_dev.yaml @@ -1,8 +1,14 @@ -# 路由配置 -routes: - - prefix: /base - url: http://localhost:9002 +jfinal: + # 开发模式 + devMode: true + # 路由配置 + routes: + - prefix: /base + url: http://localhost:9002 + # 白名单 + whitelist: + # 根 + - / + # 登录接口 + - /base/doLogin -# 白名单 -whitelist: - - /base/doLogin