标签功能初版实现
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.xiaowang.yumitailgateway.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.reactive.CorsWebFilter;
|
||||
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Gateway CORS 配置
|
||||
*
|
||||
* @author xiaowang
|
||||
* @date 2026-02-13
|
||||
*/
|
||||
@Configuration
|
||||
public class CorsConfig {
|
||||
|
||||
@Bean
|
||||
public CorsWebFilter corsWebFilter() {
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
|
||||
config.addAllowedOriginPattern("*");
|
||||
config.addAllowedMethod("*");
|
||||
config.addAllowedHeader("*");
|
||||
config.setAllowCredentials(true);
|
||||
config.setMaxAge(3600L);
|
||||
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
|
||||
return new CorsWebFilter(source);
|
||||
}
|
||||
}
|
||||
+10
-16
@@ -41,7 +41,7 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
|
||||
log.info("网关接收到请求: {}", path);
|
||||
|
||||
// 1. 白名单校验
|
||||
// 1. 白名单校验(登录和注册接口不需要认证)
|
||||
if (isWhitelist(path)) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
@@ -52,16 +52,11 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
return unAuthorized(exchange, "未携带Token,拒绝访问");
|
||||
}
|
||||
|
||||
// TODO: 这里可以集成 Redis 进一步校验 Token 的合法性和有效期
|
||||
// String userJson = redisTemplate.opsForValue().get("TOKEN:" + token);
|
||||
// if (!StringUtils.hasText(userJson)) {
|
||||
// return unAuthorized(exchange, "Token已过期或非法");
|
||||
// }
|
||||
|
||||
// 3. 示例:简单的 Token 校验逻辑(后续可扩展)
|
||||
if (!"yuminail-test-token".equals(token)) {
|
||||
// 仅做演示,实际应解析JWT或查询Redis
|
||||
// return unAuthorized(exchange, "Token校验失败");
|
||||
// 3. 双 Token 校验模式
|
||||
// accessToken 用于 API 访问,refreshToken 用于刷新 accessToken
|
||||
// 这里只验证 accessToken 是否存在,具体验证由后端服务处理
|
||||
if (!token.startsWith("Bearer ")) {
|
||||
return unAuthorized(exchange, "Token格式错误");
|
||||
}
|
||||
|
||||
return chain.filter(exchange);
|
||||
@@ -71,11 +66,10 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
* 判断是否在白名单中
|
||||
*/
|
||||
private boolean isWhitelist(String path) {
|
||||
if (gatewayConfig.getWhitelist() == null) {
|
||||
return false;
|
||||
}
|
||||
return gatewayConfig.getWhitelist().stream()
|
||||
.anyMatch(pattern -> pathMatcher.match(pattern, path));
|
||||
return path.equals("/api/user/login") ||
|
||||
path.equals("/api/user/register") ||
|
||||
path.equals("/api/user/refresh-token") ||
|
||||
path.equals("/api/shop/list/simple");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
spring:
|
||||
cloud:
|
||||
gateway:
|
||||
routes:
|
||||
- id: product-route
|
||||
uri: lb://yumi-nail-core
|
||||
predicates:
|
||||
- Path=/api/product/**
|
||||
- id: shop-route
|
||||
uri: lb://yumi-nail-core
|
||||
predicates:
|
||||
- Path=/api/shop/**
|
||||
- id: user-route
|
||||
uri: lb://yumi-nail-core
|
||||
predicates:
|
||||
- Path=/api/user/**
|
||||
- id: comment-route
|
||||
uri: lb://yumi-nail-core
|
||||
predicates:
|
||||
- Path=/api/comment/**
|
||||
- id: balance-route
|
||||
uri: lb://yumi-nail-core
|
||||
predicates:
|
||||
- Path=/api/balance/**
|
||||
- id: tag-route
|
||||
uri: lb://yumi-nail-core
|
||||
predicates:
|
||||
- Path=/api/tag/**
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Properties>
|
||||
<Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Property>
|
||||
<Property name="LOG_HOME">E:/code/xiaowang/YumiNail-Gateway/logs</Property>
|
||||
</Properties>
|
||||
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="${LOG_PATTERN}"/>
|
||||
</Console>
|
||||
|
||||
<RollingFile name="RollingFile" fileName="${LOG_HOME}/yuminail-gateway.log"
|
||||
filePattern="${LOG_HOME}/yuminail-gateway-%d{yyyy-MM-dd}-%i.log.gz">
|
||||
<PatternLayout pattern="${LOG_PATTERN}"/>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
|
||||
<SizeBasedTriggeringPolicy size="100 MB"/>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy max="30">
|
||||
<Delete basePath="${LOG_HOME}" maxDepth="1">
|
||||
<IfFileName glob="yuminail-gateway-*.log.gz"/>
|
||||
<IfLastModified age="30d"/>
|
||||
</Delete>
|
||||
</DefaultRolloverStrategy>
|
||||
</RollingFile>
|
||||
|
||||
<RollingFile name="ErrorRollingFile" fileName="${LOG_HOME}/yuminail-gateway-error.log"
|
||||
filePattern="${LOG_HOME}/yuminail-gateway-error-%d{yyyy-MM-dd}-%i.log.gz">
|
||||
<PatternLayout pattern="${LOG_PATTERN}"/>
|
||||
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
|
||||
<SizeBasedTriggeringPolicy size="100 MB"/>
|
||||
</Policies>
|
||||
<DefaultRolloverStrategy max="30">
|
||||
<Delete basePath="${LOG_HOME}" maxDepth="1">
|
||||
<IfFileName glob="yuminail-gateway-error-*.log.gz"/>
|
||||
<IfLastModified age="30d"/>
|
||||
</Delete>
|
||||
</DefaultRolloverStrategy>
|
||||
</RollingFile>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<Root level="INFO">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="RollingFile"/>
|
||||
<AppenderRef ref="ErrorRollingFile"/>
|
||||
</Root>
|
||||
|
||||
<Logger name="com.xiaowang.yumitailgateway" level="DEBUG" additivity="false">
|
||||
<AppenderRef ref="Console"/>
|
||||
<AppenderRef ref="RollingFile"/>
|
||||
<AppenderRef ref="ErrorRollingFile"/>
|
||||
</Logger>
|
||||
|
||||
<Logger name="org.springframework" level="INFO"/>
|
||||
<Logger name="com.alibaba.nacos" level="INFO"/>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Reference in New Issue
Block a user