微服务改造
This commit is contained in:
13
pom.xml
13
pom.xml
@@ -99,6 +99,19 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Knife4j Gateway Aggregation -->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
<artifactId>knife4j-gateway-spring-boot-starter</artifactId>
|
||||
<version>4.4.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud LoadBalancer -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
|
||||
@@ -25,6 +25,7 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
|
||||
/**
|
||||
@@ -46,6 +47,7 @@ public class GlobalAuthWebFilter implements WebFilter {
|
||||
@Qualifier("reactiveRedisTemplate")
|
||||
private final ReactiveRedisTemplate<String, String> redisTemplate;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final AntPathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
@@ -121,27 +123,13 @@ public class GlobalAuthWebFilter implements WebFilter {
|
||||
return false;
|
||||
}
|
||||
for (String pattern : authProperties.getIgnorePaths()) {
|
||||
if (matches(pattern, requestUri)) {
|
||||
if (pathMatcher.match(pattern, requestUri)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean matches(String pattern, String uri) {
|
||||
if (pattern.endsWith("/**")) {
|
||||
String prefix = pattern.substring(0, pattern.length() - 3);
|
||||
return uri.startsWith(prefix);
|
||||
}
|
||||
if (pattern.endsWith("/*")) {
|
||||
String prefix = pattern.substring(0, pattern.length() - 2);
|
||||
if (!uri.startsWith(prefix)) return false;
|
||||
String suffix = uri.substring(prefix.length());
|
||||
return !suffix.contains("/");
|
||||
}
|
||||
return uri.contains(pattern);
|
||||
}
|
||||
|
||||
private Claims parseToken(String token) {
|
||||
SecretKey key = buildSigningKey();
|
||||
return Jwts.parser()
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.aida.gateway.route;
|
||||
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 路由配置。
|
||||
* <p>
|
||||
* 注意:实际生产环境中建议将路由配置放在 Nacos 配置中心。
|
||||
* StripPrefix=1 将 /seller 前缀剥离,例如:
|
||||
* /seller/designer/check -> /designer/check (发到 aida-seller 的 /api/designer/check)
|
||||
*/
|
||||
@Configuration
|
||||
public class RouteConfig {
|
||||
|
||||
@Bean
|
||||
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
|
||||
return builder.routes()
|
||||
// /internal/** 用于内部服务调用(如 logout 黑名单),不需要 stripPrefix
|
||||
.route("aida-gateway-internal", r -> r
|
||||
.path("/internal/**")
|
||||
.uri("forward:/internal"))
|
||||
// aida-seller 服务
|
||||
.route("aida-seller", r -> r
|
||||
.path("/seller/**")
|
||||
.filters(f -> f.stripPrefix(1))
|
||||
.uri("lb://aida-seller"))
|
||||
// aida-back_001 服务
|
||||
.route("aida-back", r -> r
|
||||
.path("/api/**")
|
||||
.filters(f -> f.stripPrefix(1))
|
||||
.uri("lb://aida-back"))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,24 @@ spring:
|
||||
application:
|
||||
name: aida-gateway
|
||||
|
||||
# ---------- Knife4j 网关聚合配置 ----------
|
||||
knife4j:
|
||||
gateway:
|
||||
enabled: true
|
||||
# 手动指定下游服务的文档聚合,可以灵活应对不同服务的 API 版本或路径前缀
|
||||
strategy: manual
|
||||
routes:
|
||||
- name: 业务服务 (Back)
|
||||
url: /api/v3/api-docs
|
||||
service-name: aida-back
|
||||
context-path: /api
|
||||
order: 1
|
||||
- name: 商家端服务 (Seller)
|
||||
url: /seller/api/v3/api-docs
|
||||
service-name: aida-seller
|
||||
context-path: /seller
|
||||
order: 2
|
||||
|
||||
# ---------- Gateway JWT 认证(gateway 独有) ----------
|
||||
gateway:
|
||||
auth:
|
||||
@@ -21,11 +39,15 @@ gateway:
|
||||
- /favicon.ico
|
||||
- /doc.html
|
||||
- /swagger-ui.html
|
||||
- /swagger-ui
|
||||
- /swagger-ui/**
|
||||
- /swagger-resources/**
|
||||
- /v2/api-docs
|
||||
- /v2/api-docs/**
|
||||
- /v3/api-docs
|
||||
- /v3/api-docs/**
|
||||
- /webjars/**
|
||||
- /**/v3/api-docs/**
|
||||
- /api/account/login
|
||||
- /api/account/preLogin
|
||||
- /api/designer/check
|
||||
|
||||
@@ -8,13 +8,13 @@ spring:
|
||||
application:
|
||||
name: aida-gateway
|
||||
config:
|
||||
import: optional:nacos:aida-public-${NACOS_NAMESPACE:dev}.yml
|
||||
import: optional:nacos:aida-public-${NACOS_NAMESPACE:test}.yml
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
server-addr: ${NACOS_HOST:127.0.0.1:8848}
|
||||
namespace: ${NACOS_NAMESPACE:dev}
|
||||
namespace: ${NACOS_NAMESPACE:test}
|
||||
config:
|
||||
server-addr: ${NACOS_HOST:127.0.0.1:8848}
|
||||
namespace: ${NACOS_NAMESPACE:dev}
|
||||
namespace: ${NACOS_NAMESPACE:test}
|
||||
file-extension: yaml
|
||||
|
||||
Reference in New Issue
Block a user