修改配置
This commit is contained in:
parent
9e4640376a
commit
7f06574ce9
16
pom.xml
16
pom.xml
|
|
@ -29,6 +29,8 @@
|
|||
<lombok.version>1.18.30</lombok.version>
|
||||
<jjwt.version>0.11.5</jjwt.version>
|
||||
<resilience4j.version>2.1.0</resilience4j.version>
|
||||
<springdoc.version>2.3.0</springdoc.version>
|
||||
<commons-lang3.version>3.14.0</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
|
@ -93,6 +95,20 @@
|
|||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringDoc -->
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>${springdoc.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Commons Lang3 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Resilience4j -->
|
||||
<!-- Spring Boot 3集成模块,提供自动配置、注解支持和健康指标,是与Spring Boot集成的核心依赖 -->
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -34,16 +34,6 @@
|
|||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Alibaba -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MyBatis Plus -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
|
|
@ -61,7 +51,6 @@
|
|||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Common Module -->
|
||||
|
|
@ -75,6 +64,7 @@
|
|||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
spring:
|
||||
application:
|
||||
name: tacit-admin
|
||||
config:
|
||||
import:
|
||||
- nacos: # 添加此行
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
|
|
|
|||
|
|
@ -34,16 +34,6 @@
|
|||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Alibaba -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MyBatis Plus -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
|
|
@ -61,7 +51,6 @@
|
|||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>2.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Common Module -->
|
||||
|
|
@ -75,6 +64,7 @@
|
|||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
spring:
|
||||
application:
|
||||
name: tacit-app-api
|
||||
config:
|
||||
import:
|
||||
- nacos: # 添加此行
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
|
|
|
|||
|
|
@ -20,16 +20,6 @@
|
|||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Alibaba -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
|
|
@ -40,17 +30,16 @@
|
|||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MyBatis Plus -->
|
||||
<!-- Spring Cloud Bootstrap (Required for Spring Boot 3.x to load bootstrap.yml) -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Database -->
|
||||
<!-- Feign -->
|
||||
<dependency>
|
||||
<groupId>com.oceanbase</groupId>
|
||||
<artifactId>oceanbase-client</artifactId>
|
||||
<version>2.4.0</version>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- JJWT -->
|
||||
|
|
@ -81,6 +70,24 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Commons Lang3 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
|||
|
|
@ -44,4 +44,8 @@ public class ResponseResult<T> implements Serializable {
|
|||
public static <T> ResponseResult<T> fail(Integer code, String message) {
|
||||
return new ResponseResult<>(code, message, null);
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return this.code == 200;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,16 +20,6 @@
|
|||
<artifactId>spring-cloud-starter-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Alibaba -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Security -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
@ -41,26 +31,33 @@
|
|||
<groupId>com.tacit</groupId>
|
||||
<artifactId>tacit-common</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud Bootstrap for loading bootstrap.yml in Spring Boot 3.x -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Commons Lang3 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.14.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Logging -->
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-logging</artifactId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
|
@ -72,7 +69,18 @@
|
|||
<version>${spring-boot.version}</version>
|
||||
<configuration>
|
||||
<skip>false</skip>
|
||||
<mainClass>com.tacit.gateway.GatewayApplication</mainClass>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<mainClass>com.tacit.gateway.GatewayApplication</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ import com.tacit.common.constant.CommonConstant;
|
|||
import com.tacit.common.utils.JwtUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
|
@ -17,9 +19,12 @@ import reactor.core.publisher.Mono;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
/**
|
||||
* JWT 认证过滤器 - 作为 GatewayFilterFactory 使用
|
||||
*/
|
||||
@Component("JwtAuthenticationFilter")
|
||||
@Slf4j
|
||||
public class JwtAuthenticationFilter implements GlobalFilter, Ordered {
|
||||
public class JwtAuthenticationFilter extends AbstractGatewayFilterFactory<JwtAuthenticationFilter.Config> {
|
||||
|
||||
// 不需要认证的路径
|
||||
private static final List<String> WHITE_LIST = List.of(
|
||||
|
|
@ -29,54 +34,63 @@ public class JwtAuthenticationFilter implements GlobalFilter, Ordered {
|
|||
"/v3/api-docs"
|
||||
);
|
||||
|
||||
public JwtAuthenticationFilter() {
|
||||
super(Config.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
String path = request.getURI().getPath();
|
||||
public GatewayFilter apply(Config config) {
|
||||
return new GatewayFilter() {
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
String path = request.getURI().getPath();
|
||||
|
||||
// 检查是否在白名单中
|
||||
if (isWhiteList(path)) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
// 检查是否在白名单中
|
||||
if (isWhiteList(path)) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
// 获取Authorization头
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
String authorization = headers.getFirst(HttpHeaders.AUTHORIZATION);
|
||||
// 获取Authorization头
|
||||
HttpHeaders headers = request.getHeaders();
|
||||
String authorization = headers.getFirst(HttpHeaders.AUTHORIZATION);
|
||||
|
||||
// 检查Authorization头是否存在
|
||||
if (StringUtils.isBlank(authorization)) {
|
||||
return unauthorizedResponse(exchange, "缺少认证令牌");
|
||||
}
|
||||
// 检查Authorization头是否存在
|
||||
if (StringUtils.isBlank(authorization)) {
|
||||
return unauthorizedResponse(exchange, "缺少认证令牌");
|
||||
}
|
||||
|
||||
// 检查Authorization头格式
|
||||
if (!authorization.startsWith(CommonConstant.JWT_PREFIX)) {
|
||||
return unauthorizedResponse(exchange, "认证令牌格式错误");
|
||||
}
|
||||
// 检查Authorization头格式
|
||||
if (!authorization.startsWith(CommonConstant.JWT_PREFIX)) {
|
||||
return unauthorizedResponse(exchange, "认证令牌格式错误");
|
||||
}
|
||||
|
||||
// 提取JWT令牌
|
||||
String token = authorization.substring(CommonConstant.JWT_PREFIX.length());
|
||||
// 提取JWT令牌
|
||||
String token = authorization.substring(CommonConstant.JWT_PREFIX.length());
|
||||
|
||||
// 验证JWT令牌
|
||||
try {
|
||||
JwtUtils.validateToken(token);
|
||||
// 验证JWT令牌
|
||||
try {
|
||||
JwtUtils.validateToken(token);
|
||||
|
||||
// 从令牌中获取用户信息并添加到请求头
|
||||
Long userId = JwtUtils.getUserIdFromToken(token);
|
||||
String username = JwtUtils.getUsernameFromToken(token);
|
||||
String role = JwtUtils.getRoleFromToken(token);
|
||||
// 从令牌中获取用户信息并添加到请求头
|
||||
Long userId = JwtUtils.getUserIdFromToken(token);
|
||||
String username = JwtUtils.getUsernameFromToken(token);
|
||||
String role = JwtUtils.getRoleFromToken(token);
|
||||
|
||||
// 将用户信息添加到请求头
|
||||
ServerHttpRequest mutatedRequest = request.mutate()
|
||||
.header("X-User-Id", String.valueOf(userId))
|
||||
.header("X-Username", username)
|
||||
.header("X-Role", role)
|
||||
.build();
|
||||
// 将用户信息添加到请求头
|
||||
ServerHttpRequest mutatedRequest = request.mutate()
|
||||
.header("X-User-Id", String.valueOf(userId))
|
||||
.header("X-Username", username)
|
||||
.header("X-Role", role)
|
||||
.build();
|
||||
|
||||
return chain.filter(exchange.mutate().request(mutatedRequest).build());
|
||||
} catch (Exception e) {
|
||||
log.error("JWT认证失败: {}", e.getMessage());
|
||||
return unauthorizedResponse(exchange, e.getMessage());
|
||||
}
|
||||
return chain.filter(exchange.mutate().request(mutatedRequest).build());
|
||||
} catch (Exception e) {
|
||||
log.error("JWT认证失败: {}", e.getMessage());
|
||||
return unauthorizedResponse(exchange, e.getMessage());
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -97,8 +111,8 @@ public class JwtAuthenticationFilter implements GlobalFilter, Ordered {
|
|||
return response.writeWith(Mono.just(response.bufferFactory().wrap(responseBody.getBytes())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return -100;
|
||||
|
||||
public static class Config {
|
||||
// 可以添加配置属性
|
||||
}
|
||||
}
|
||||
|
|
@ -1,50 +0,0 @@
|
|||
server:
|
||||
port: 8080
|
||||
|
||||
spring:
|
||||
cloud:
|
||||
gateway:
|
||||
discovery:
|
||||
locator:
|
||||
enabled: true
|
||||
lower-case-service-id: true
|
||||
routes:
|
||||
# Admin Service Route
|
||||
- id: tacit-admin
|
||||
uri: lb://tacit-admin
|
||||
predicates:
|
||||
- Path=/admin/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
|
||||
# App API Service Route
|
||||
- id: tacit-app-api
|
||||
uri: lb://tacit-app-api
|
||||
predicates:
|
||||
- Path=/api/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
- JwtAuthenticationFilter
|
||||
|
||||
# Swagger UI Routes
|
||||
- id: swagger-admin
|
||||
uri: lb://tacit-admin
|
||||
predicates:
|
||||
- Path=/swagger-admin/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
|
||||
- id: swagger-app-api
|
||||
uri: lb://tacit-app-api
|
||||
predicates:
|
||||
- Path=/swagger-app-api/**
|
||||
filters:
|
||||
- StripPrefix=1
|
||||
|
||||
# Logging Configuration
|
||||
logging:
|
||||
level:
|
||||
org.springframework.cloud.gateway: debug
|
||||
org.springframework.http.server.reactive: debug
|
||||
org.springframework.web.reactive: debug
|
||||
reactor.netty: debug
|
||||
|
|
@ -1,16 +1,19 @@
|
|||
spring:
|
||||
application:
|
||||
name: tacit-gateway
|
||||
main:
|
||||
web-application-type: reactive
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
enabled: true
|
||||
server-addr: localhost:8848
|
||||
namespace: public
|
||||
username: nacos
|
||||
password: nacos
|
||||
config:
|
||||
enabled: true
|
||||
server-addr: localhost:8848
|
||||
namespace: public
|
||||
file-extension: yml
|
||||
group: DEFAULT_GROUP
|
||||
refresh-enabled: true
|
||||
profiles:
|
||||
active: dev
|
||||
file-extension: yaml
|
||||
username: nacos
|
||||
password: nacos
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue