From de9f31a1033c15ffdee5dfe2c1cc8625e42d89d0 Mon Sep 17 00:00:00 2001 From: OTTO <731554297@qq.com> Date: Fri, 11 Oct 2024 08:40:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=B0=83=E7=94=A8=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E5=BE=AE=E6=9C=8D=E5=8A=A1):=20=E5=A4=84=E7=90=86Naco?= =?UTF-8?q?s=E8=AF=B7=E6=B1=82=E8=BD=AC=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 识别到Nacos请求时,调用Nacos登录接口获取Token并写入到请求头中,用于Nacos后续接口的鉴权操作 Signed-off-by: OTTO <731554297@qq.com> --- .../system/api/RemoteGatewayService.java | 18 ++++++++++++-- .../factory/RemoteGatewayFallbackFactory.java | 8 ++++++- .../common/core/utils/CookieUtil.java | 2 +- .../gateway/filter/AuthFilter.java | 24 ++++++++++++++++--- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/RemoteGatewayService.java b/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/RemoteGatewayService.java index 661f3ebc9..b3088158f 100644 --- a/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/RemoteGatewayService.java +++ b/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/RemoteGatewayService.java @@ -2,15 +2,20 @@ package com.microservices.system.api; import com.microservices.common.core.constant.ServiceNameConstants; import com.microservices.system.api.factory.RemoteGatewayFallbackFactory; +import feign.Headers; import feign.Response; import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RequestPart; /** * 网关 * * @author microservices */ +@Component @FeignClient(contextId = "remoteGatewayService", value = ServiceNameConstants.GATEWAY_SERVICE, fallbackFactory = RemoteGatewayFallbackFactory.class) public interface RemoteGatewayService { /** @@ -18,6 +23,15 @@ public interface RemoteGatewayService { * * @return 响应 */ - @PostMapping("/sentinel/auth/login?password=sentinel&username=sentinel") - public Response loginSentinel(); + @PostMapping("/sentinel/auth/login") + Response loginSentinel(@RequestParam("username") String username, @RequestParam("password") String password); + + /** + * 登录Nacos + * + * @return 响应 + */ + @PostMapping("/nacos/v1/auth/users/login") + @Headers("Content-Type: application/x-www-form-urlencoded") + Response loginNacos(@RequestPart("username") String username, @RequestPart("password") String password); } diff --git a/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteGatewayFallbackFactory.java b/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteGatewayFallbackFactory.java index 85d534e6a..a09370d31 100644 --- a/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteGatewayFallbackFactory.java +++ b/microservices-api/microservices-api-system/src/main/java/com/microservices/system/api/factory/RemoteGatewayFallbackFactory.java @@ -20,8 +20,14 @@ public class RemoteGatewayFallbackFactory implements FallbackFactory future = executorService.submit(() -> remoteGatewayService.loginSentinel()); + // 网关采用异步架构,所以此处需要通过异步请求获取Sentinel Token + Future future = executorService.submit(() -> remoteGatewayService.loginSentinel("sentinel", "sentinel")); try { Response response = future.get(1, TimeUnit.SECONDS); Map> header = response.headers(); @@ -91,6 +94,21 @@ public class AuthFilter implements GlobalFilter, Ordered { log.error("获取Sentinel Token失败:{0}", e); } } + // 处理Nacos请求 + if (url.toLowerCase().startsWith("/nacos")) { + // 网关采用异步架构,所以此处需要通过异步请求获取Nacos Token + Future future = executorService.submit(() -> remoteGatewayService.loginNacos("nacos", "nacos")); + try { + Response res = future.get(1, TimeUnit.SECONDS); + StringWriter writer = new StringWriter(); + IOUtils.copy(res.body().asInputStream(), writer, StandardCharsets.UTF_8.name()); + String str = writer.toString(); + System.out.println(str); + } catch (Exception e) { + log.error("获取Nacos Token失败:{0}", e); + } + } + } @Override