一些问题的修复
This commit is contained in:
+21
-35
@@ -17,6 +17,7 @@ import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 全局认证过滤器
|
||||
@@ -29,6 +30,23 @@ import java.nio.charset.StandardCharsets;
|
||||
@Component
|
||||
public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
|
||||
private static final List<String> DEFAULT_WHITELIST = List.of(
|
||||
"/api/user/login",
|
||||
"/api/user/register",
|
||||
"/api/user/refresh-token",
|
||||
"/api/user/send-verification-code",
|
||||
"/api/user/phone-login",
|
||||
"/api/user/reset-password-by-phone",
|
||||
"/api/user/get/account/**",
|
||||
"/api/shop/list/simple",
|
||||
"/api/admin/auth/login",
|
||||
"/api/admin/auth/refresh",
|
||||
"/admin/api/auth/login",
|
||||
"/admin/api/auth/refresh",
|
||||
"/api/health/**",
|
||||
"/api/error"
|
||||
);
|
||||
|
||||
@Autowired
|
||||
private GatewayConfig gatewayConfig;
|
||||
|
||||
@@ -39,34 +57,15 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
String path = request.getURI().getPath();
|
||||
|
||||
log.info("Gateway received request: {}", path);
|
||||
|
||||
// 打印白名单配置
|
||||
if (gatewayConfig.getWhitelist() != null) {
|
||||
log.info("Whitelist from config: {}", gatewayConfig.getWhitelist());
|
||||
} else {
|
||||
log.info("Whitelist from config is null");
|
||||
}
|
||||
|
||||
// 1. Whitelist check (login and register interfaces don't need authentication)
|
||||
log.info("Checking if path is in whitelist: {}", path);
|
||||
boolean isWhitelist = isWhitelist(path);
|
||||
log.info("Whitelist check result: {}", isWhitelist);
|
||||
if (isWhitelist) {
|
||||
log.info("Path is in whitelist, passing through directly");
|
||||
if (isWhitelist(path)) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
log.info("Path is not in whitelist, need to check Token");
|
||||
|
||||
// 2. Token 校验
|
||||
String token = request.getHeaders().getFirst("Authorization");
|
||||
if (!StringUtils.hasText(token)) {
|
||||
return unAuthorized(exchange, "未携带Token,拒绝访问");
|
||||
}
|
||||
|
||||
// 3. 双 Token 校验模式
|
||||
// accessToken 用于 API 访问,refreshToken 用于刷新 accessToken
|
||||
// 这里只验证 accessToken 是否存在,具体验证由后端服务处理
|
||||
if (!token.startsWith("Bearer ")) {
|
||||
return unAuthorized(exchange, "Token格式错误");
|
||||
}
|
||||
@@ -78,7 +77,6 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
* 判断是否在白名单中
|
||||
*/
|
||||
private boolean isWhitelist(String path) {
|
||||
// 首先检查配置文件中的白名单
|
||||
if (gatewayConfig.getWhitelist() != null) {
|
||||
for (String whitelistPath : gatewayConfig.getWhitelist()) {
|
||||
if (pathMatcher.match(whitelistPath, path)) {
|
||||
@@ -86,20 +84,8 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 硬编码白名单作为备用
|
||||
return pathMatcher.match("/api/user/login", path) ||
|
||||
pathMatcher.match("/api/user/register", path) ||
|
||||
pathMatcher.match("/api/user/refresh-token", path) ||
|
||||
pathMatcher.match("/api/user/send-verification-code", path) ||
|
||||
pathMatcher.match("/api/user/phone-login", path) ||
|
||||
pathMatcher.match("/api/user/get/account/**", path) ||
|
||||
pathMatcher.match("/api/shop/list/simple", path) ||
|
||||
pathMatcher.match("/api/admin/auth/login", path) ||
|
||||
pathMatcher.match("/admin/api/auth/login", path) ||
|
||||
pathMatcher.match("/api/health/**", path) ||
|
||||
pathMatcher.match("/api/error", path) ||
|
||||
pathMatcher.match("/api/admin/object-storage-file/upload", path);
|
||||
|
||||
return DEFAULT_WHITELIST.stream().anyMatch(pattern -> pathMatcher.match(pattern, path));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -89,6 +89,7 @@ spring:
|
||||
- RewritePath=/api/(?<remaining>.*), /${remaining}
|
||||
# 后台管理API路由(/api/admin/** 格式)
|
||||
- id: admin-api-route
|
||||
order: -10
|
||||
uri: lb://yumi-nail-core
|
||||
predicates:
|
||||
- Path=/api/admin/**
|
||||
@@ -96,8 +97,35 @@ spring:
|
||||
- RewritePath=/api/admin/(?<remaining>.*), /admin/${remaining}
|
||||
# 后台管理API路由(/admin/api/** 格式)
|
||||
- id: admin-route
|
||||
order: -9
|
||||
uri: lb://yumi-nail-core
|
||||
predicates:
|
||||
- Path=/admin/api/**
|
||||
filters:
|
||||
- RewritePath=/admin/api/(?<remaining>.*), /admin/${remaining}
|
||||
# 前台API兜底路由,避免 Core 新增模块后 Gateway 未同步导致 404
|
||||
- id: frontend-fallback-route
|
||||
order: 100
|
||||
uri: lb://yumi-nail-core
|
||||
predicates:
|
||||
- Path=/api/**
|
||||
filters:
|
||||
- RewritePath=/api/(?<remaining>.*), /${remaining}
|
||||
|
||||
yuminail:
|
||||
gateway:
|
||||
whitelist:
|
||||
- /api/user/login
|
||||
- /api/user/register
|
||||
- /api/user/refresh-token
|
||||
- /api/user/send-verification-code
|
||||
- /api/user/phone-login
|
||||
- /api/user/reset-password-by-phone
|
||||
- /api/user/get/account/**
|
||||
- /api/shop/list/simple
|
||||
- /api/health/**
|
||||
- /api/error
|
||||
- /api/admin/auth/login
|
||||
- /api/admin/auth/refresh
|
||||
- /admin/api/auth/login
|
||||
- /admin/api/auth/refresh
|
||||
|
||||
Reference in New Issue
Block a user