feat(调用第三方微服务): 处理Nacos请求转发

识别到Nacos请求时,调用Nacos登录接口获取Token并写入到请求头中,用于Nacos后续接口的鉴权操作

Signed-off-by: OTTO <731554297@qq.com>
This commit is contained in:
OTTO 2024-10-11 08:40:04 +08:00
parent 0029075542
commit de9f31a103
4 changed files with 45 additions and 7 deletions

View File

@ -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);
}

View File

@ -20,8 +20,14 @@ public class RemoteGatewayFallbackFactory implements FallbackFactory<RemoteGatew
public RemoteGatewayService create(Throwable throwable) {
log.error("网关服务调用失败:{}", throwable.getMessage());
return new RemoteGatewayService() {
@Override
public Response loginSentinel() {
public Response loginSentinel(String password, String username) {
return null;
}
@Override
public Response loginNacos(String username, String password) {
return null;
}
};

View File

@ -12,7 +12,7 @@ public class CookieUtil {
for (String cookieStr : cookies) {
String[] cookieArr = cookieStr.split("=");
String key = cookieArr[0].trim();
String value = cookieArr[0].trim();
String value = cookieArr[1].trim();
map.put(key, value);
}
}

View File

@ -18,6 +18,7 @@ import com.microservices.system.api.domain.SysUserDeptRole;
import com.microservices.system.api.utils.FeignUtils;
import feign.Response;
import io.jsonwebtoken.Claims;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -30,6 +31,8 @@ import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ -70,15 +73,15 @@ public class AuthFilter implements GlobalFilter, Ordered {
if (StringUtils.isEmpty(url)) {
return;
}
// 处理Sentinel请求
if (url.toLowerCase().startsWith("/sentinel")) {
// 检查Cookie中是否携带sentinel cookie
String cookie = request.getHeaders().getFirst(TokenConstants.Cookie);
if (StringUtils.isNotEmpty(CookieUtil.getCookieValue(cookie, TokenConstants.Sentinel_Token_Key))) {
cookie = CookieUtil.removeCookieKey(cookie, TokenConstants.Sentinel_Token_Key);
}
// 网关采用异步架构所以此处需要通过异步请求获取本系统Token
Future<Response> future = executorService.submit(() -> remoteGatewayService.loginSentinel());
// 网关采用异步架构所以此处需要通过异步请求获取Sentinel Token
Future<Response> future = executorService.submit(() -> remoteGatewayService.loginSentinel("sentinel", "sentinel"));
try {
Response response = future.get(1, TimeUnit.SECONDS);
Map<String, Collection<String>> 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<Response> 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