功能:完善订单流程和加固

This commit is contained in:
xiaowang
2026-05-23 15:16:40 +08:00
parent 9227ec6175
commit 3e6e1cc4e8
90 changed files with 6859 additions and 368 deletions
@@ -17,46 +17,14 @@ import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashSet;
import java.util.List;
/**
* 全局认证过滤器
* 负责 Token 校验和白名单过滤
*
* @author xiaowang
* @date 2026-02-12
*/
@Slf4j
@Component
public class AuthGlobalFilter implements GlobalFilter, Ordered {
private static final List<String> DEFAULT_WHITELIST = List.of(
"/api/user/login",
"/api/user/register",
"/api/user/check/**",
"/api/user/refresh-token",
"/api/user/send-verification-code",
"/api/user/phone-login",
"/api/user/wechat-mini/**",
"/api/user/reset-password-by-phone",
"/api/legal-agreement/current",
"/api/legal-agreement/current/**",
"/api/product/get/**",
"/api/product/list/**",
"/api/shop/get/**",
"/api/shop/list/public",
"/api/shop/list/simple",
"/api/tag/list/all",
"/api/file/get-signed-url/**",
"/api/admin/auth/login",
"/api/admin/auth/login-shops/**",
"/api/admin/auth/refresh",
"/admin/api/auth/login",
"/admin/api/auth/login-shops/**",
"/admin/api/auth/refresh",
"/api/health/**",
"/api/error"
);
private static final List<String> DEFAULT_WHITELIST = AuthWhitelist.defaultPatterns();
@Autowired
private GatewayConfig gatewayConfig;
@@ -92,37 +60,70 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
return chain.filter(exchange);
}
/**
* 判断是否在白名单中
*/
private boolean isWhitelist(String path) {
if (gatewayConfig.getWhitelist() != null) {
for (String whitelistPath : gatewayConfig.getWhitelist()) {
if (pathMatcher.match(whitelistPath, path)) {
return true;
}
}
}
return DEFAULT_WHITELIST.stream().anyMatch(pattern -> pathMatcher.match(pattern, path));
return AuthWhitelist.effectivePatterns(gatewayConfig).stream()
.anyMatch(pattern -> pathMatcher.match(pattern, path));
}
/**
* 无权限响应处理
*/
private Mono<Void> unAuthorized(ServerWebExchange exchange, String msg) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.UNAUTHORIZED);
response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
String result = "{\"code\": 401, \"message\": \"" + msg + "\", \"data\": null}";
DataBuffer buffer = response.bufferFactory().wrap(result.getBytes(StandardCharsets.UTF_8));
return response.writeWith(Mono.just(buffer));
}
@Override
public int getOrder() {
return -1; // 优先级最高
return -1;
}
public static final class AuthWhitelist {
private AuthWhitelist() {
}
public static List<String> defaultPatterns() {
return List.of(
"/api/user/login",
"/api/user/register",
"/api/user/check/**",
"/api/user/refresh-token",
"/api/user/send-verification-code",
"/api/user/phone-login",
"/api/user/wechat-mini/**",
"/api/user/reset-password-by-phone",
"/api/legal-agreement/current",
"/api/legal-agreement/current/**",
"/api/product/get/**",
"/api/product/list/**",
"/api/comment/list/product/**",
"/api/shop/get/**",
"/api/shop/list/public",
"/api/shop/list/simple",
"/api/tag/list/all",
"/api/file/get-signed-url/**",
"/api/admin/auth/login",
"/api/admin/auth/login-shops/**",
"/api/admin/auth/refresh",
"/admin/api/auth/login",
"/admin/api/auth/login-shops/**",
"/admin/api/auth/refresh",
"/api/health/**",
"/api/error"
);
}
public static List<String> effectivePatterns(GatewayConfig gatewayConfig) {
if (gatewayConfig == null || gatewayConfig.getWhitelist() == null || gatewayConfig.getWhitelist().isEmpty()) {
return defaultPatterns();
}
LinkedHashSet<String> patterns = new LinkedHashSet<>(gatewayConfig.getWhitelist());
patterns.addAll(DEFAULT_WHITELIST);
return List.copyOf(patterns);
}
}
}
@@ -0,0 +1,38 @@
package com.xiaowang.yumitailgateway.filter;
import com.xiaowang.yumitailgateway.config.GatewayConfig;
import com.xiaowang.yumitailgateway.filter.AuthGlobalFilter.AuthWhitelist;
import org.junit.jupiter.api.Test;
import org.springframework.util.AntPathMatcher;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class AuthGlobalFilterWhitelistTest {
private final AntPathMatcher pathMatcher = new AntPathMatcher();
@Test
void effectiveWhitelistCoversPublicCommentListAndRejectsOrderApi() {
GatewayConfig config = new GatewayConfig();
config.setWhitelist(List.of("/api/user/login"));
List<String> patterns = AuthWhitelist.effectivePatterns(config);
assertThat(matches(patterns, "/api/comment/list/product/1")).isTrue();
assertThat(matches(patterns, "/api/order/list")).isFalse();
assertThat(matches(patterns, "/api/admin/user/list")).isFalse();
}
@Test
void defaultWhitelistIncludesOnlyExpectedPublicBusinessAreas() {
assertThat(AuthWhitelist.defaultPatterns())
.contains("/api/user/login", "/api/comment/list/product/**", "/api/admin/auth/login")
.doesNotContain("/api/order/**", "/api/shop/delete/**", "/api/permission/**");
}
private boolean matches(List<String> patterns, String path) {
return patterns.stream().anyMatch(pattern -> pathMatcher.match(pattern, path));
}
}