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