feat: 更新配置文件和代码,优化Swagger文档和负载均衡支持
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package cn.meowrain.aioj.backend.gateway.config;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
@@ -13,9 +14,11 @@ import org.springframework.web.reactive.function.client.WebClient;
|
||||
public class GatewayConfiguration {
|
||||
|
||||
/**
|
||||
* WebClient Bean,用于服务间调用
|
||||
* 支持负载均衡的 WebClient Bean,用于服务间调用
|
||||
* 加上 @LoadBalanced 注解后可以使用 lb://service-name 格式的 URI
|
||||
*/
|
||||
@Bean
|
||||
@LoadBalanced
|
||||
public WebClient.Builder webClientBuilder() {
|
||||
return WebClient.builder();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package cn.meowrain.aioj.backend.gateway.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
/**
|
||||
* 启动时打印 Swagger 文档地址
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SwaggerDocPrinter implements ApplicationRunner {
|
||||
|
||||
@Value("${server.port:8080}")
|
||||
private int port;
|
||||
|
||||
@Value("${server.servlet.context-path:}")
|
||||
private String contextPath;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
String ip = InetAddress.getLocalHost().getHostAddress();
|
||||
String ctx = contextPath == null ? "" : contextPath;
|
||||
|
||||
String line = "=".repeat(60);
|
||||
|
||||
log.info("\n" + line +
|
||||
"\n 应用启动成功!Swagger 文档地址:" +
|
||||
"\n" + line +
|
||||
"\n 本地访问: http://localhost:" + port + ctx + "/doc.html" +
|
||||
"\n 网络访问: http://" + ip + ":" + port + ctx + "/doc.html" +
|
||||
"\n" + line +
|
||||
"\n API 文档资源:" +
|
||||
"\n - Knife4j UI: http://localhost:" + port + ctx + "/doc.html" +
|
||||
"\n - Swagger Resources: http://localhost:" + port + ctx + "/swagger-resources" +
|
||||
"\n" + line);
|
||||
}
|
||||
}
|
||||
@@ -48,19 +48,19 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
String path = request.getURI().getPath();
|
||||
|
||||
log.info("Auth filter processing request: {}", path);
|
||||
log.info("Loaded white list from config: {}", gatewayPropertiesConfiguration.getWhiteList());
|
||||
log.info("🚀 网关拦截请求: {}", path);
|
||||
log.debug("📋 白名单配置: {}", gatewayPropertiesConfiguration.getWhiteList());
|
||||
|
||||
// 检查是否在白名单中
|
||||
if (isWhiteListPath(path)) {
|
||||
log.info("Path {} is in whitelist, skip authentication", path);
|
||||
log.info("✅ 路径 {} 在白名单中,跳过认证", path);
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
// 获取Authorization头
|
||||
String authHeader = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
|
||||
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
|
||||
log.warn("No valid authorization header found for path: {}", path);
|
||||
log.warn("⚠️ 请求缺少有效的 Authorization 头: {}", path);
|
||||
return handleUnauthorized(exchange);
|
||||
}
|
||||
|
||||
@@ -70,15 +70,15 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
return validateToken(token)
|
||||
.flatMap(isValid -> {
|
||||
if (isValid) {
|
||||
log.info("Token validation successful for path: {}", path);
|
||||
log.info("🔓 Token 验证成功: {}", path);
|
||||
return chain.filter(exchange);
|
||||
} else {
|
||||
log.warn("Token validation failed for path: {}", path);
|
||||
log.warn("🔒 Token 验证失败: {}", path);
|
||||
return handleUnauthorized(exchange);
|
||||
}
|
||||
})
|
||||
.onErrorResume(throwable -> {
|
||||
log.error("Token validation error for path: {}", path, throwable);
|
||||
log.error("❌ Token 验证异常: {}", path, throwable);
|
||||
return handleUnauthorized(exchange);
|
||||
});
|
||||
}
|
||||
@@ -105,7 +105,7 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
private Mono<Boolean> validateToken(String token) {
|
||||
return webClientBuilder.build()
|
||||
.post()
|
||||
.uri("lb://auth-service/v1/auth/validate")
|
||||
.uri("lb://auth-service/api/v1/auth/validate")
|
||||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
@@ -116,7 +116,7 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
Result result = objectMapper.readValue(response, Result.class);
|
||||
return Objects.equals(result.getCode(), Result.SUCCESS_CODE);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Failed to parse auth response", e);
|
||||
log.error("❌ 解析认证服务响应失败", e);
|
||||
return false;
|
||||
}
|
||||
})
|
||||
|
||||
@@ -64,11 +64,11 @@ logging:
|
||||
root: INFO
|
||||
# Nacos 相关日志
|
||||
com.alibaba.nacos: INFO
|
||||
com.alibaba.cloud.nacos: DEBUG
|
||||
com.alibaba.cloud.nacos: INFO
|
||||
# LoadBalancer 日志
|
||||
org.springframework.cloud.loadbalancer: DEBUG
|
||||
# Gateway 日志
|
||||
org.springframework.cloud.gateway: DEBUG
|
||||
org.springframework.cloud.loadbalancer: INFO
|
||||
# Gateway 日志 - 改为 INFO 减少输出,需要调试时改回 DEBUG
|
||||
org.springframework.cloud.gateway: INFO
|
||||
# 自定义过滤器日志
|
||||
cn.meowrain.aioj.backend.gateway: DEBUG
|
||||
|
||||
|
||||
Reference in New Issue
Block a user