YumiNail后端和网关初始化
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
package com.xiaowang.yumitailgateway;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
/**
|
||||
* YumiNail 网关启动类
|
||||
*
|
||||
* @author xiaowang
|
||||
* @date 2026-02-12
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@SpringBootApplication
|
||||
public class YumiNailGatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(YumiNailGatewayApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.xiaowang.yumitailgateway.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 网关白名单配置
|
||||
*
|
||||
* @author xiaowang
|
||||
* @date 2026-02-12
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "yuminail.gateway")
|
||||
public class GatewayConfig {
|
||||
|
||||
/**
|
||||
* 白名单路径列表
|
||||
*/
|
||||
private List<String> whitelist;
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package com.xiaowang.yumitailgateway.filter;
|
||||
|
||||
import com.xiaowang.yumitailgateway.config.GatewayConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 全局认证过滤器
|
||||
* 负责 Token 校验和白名单过滤
|
||||
*
|
||||
* @author xiaowang
|
||||
* @date 2026-02-12
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
|
||||
@Autowired
|
||||
private GatewayConfig gatewayConfig;
|
||||
|
||||
private final AntPathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
String path = request.getURI().getPath();
|
||||
|
||||
log.info("网关接收到请求: {}", path);
|
||||
|
||||
// 1. 白名单校验
|
||||
if (isWhitelist(path)) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
// 2. Token 校验
|
||||
String token = request.getHeaders().getFirst("Authorization");
|
||||
if (!StringUtils.hasText(token)) {
|
||||
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校验失败");
|
||||
}
|
||||
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否在白名单中
|
||||
*/
|
||||
private boolean isWhitelist(String path) {
|
||||
if (gatewayConfig.getWhitelist() == null) {
|
||||
return false;
|
||||
}
|
||||
return gatewayConfig.getWhitelist().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; // 优先级最高
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
server:
|
||||
port: 8888
|
||||
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
|
||||
cloud:
|
||||
gateway:
|
||||
discovery:
|
||||
locator:
|
||||
enabled: true # 开启从注册中心动态创建路由的功能
|
||||
lower-case-service-id: true # 使用小写服务名
|
||||
routes:
|
||||
- id: yuminail-core-route
|
||||
uri: lb://yuminail-core
|
||||
predicates:
|
||||
- Path=/core/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
|
||||
# 自定义配置:白名单
|
||||
yuminail:
|
||||
gateway:
|
||||
whitelist:
|
||||
- /auth/login
|
||||
- /auth/register
|
||||
- /health/check
|
||||
- /doc.html
|
||||
- /webjars/**
|
||||
- /v2/api-docs/**
|
||||
- /swagger-resources/**
|
||||
@@ -0,0 +1,24 @@
|
||||
spring:
|
||||
application:
|
||||
name: yuminail-gateway
|
||||
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: 192.168.5.101:8848,192.168.5.102:8848,192.168.5.103:8848
|
||||
namespace: YumiNail
|
||||
group: DEFAULT_GROUP
|
||||
service: ${spring.application.name}
|
||||
config:
|
||||
server-addr: 192.168.5.101:8848,192.168.5.102:8848,192.168.5.103:8848
|
||||
namespace: YumiNail
|
||||
group: DEFAULT_GROUP
|
||||
name: yuminail-gateway.yaml
|
||||
file-extension: yaml
|
||||
shared-configs:
|
||||
- data-id: redis.yaml
|
||||
group: DEFAULT_GROUP
|
||||
refresh: true
|
||||
- data-id: logging.yaml
|
||||
group: DEFAULT_GROUP
|
||||
refresh: true
|
||||
Reference in New Issue
Block a user