chore(config): Unify deployment orchestration and monitoring integration
This commit is contained in:
@@ -54,6 +54,16 @@
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
@@ -104,4 +114,4 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
</project>
|
||||
|
||||
+26
-8
@@ -12,6 +12,7 @@ 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.data.redis.core.ReactiveStringRedisTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
@@ -35,17 +36,20 @@ import java.util.Map;
|
||||
@RequiredArgsConstructor
|
||||
public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
|
||||
@Value("${jwt.secret:video-platform-secret-key-2024-nuaa}")
|
||||
private static final String USER_ACCESS_TOKEN_PREFIX = "user:access-token:";
|
||||
|
||||
@Value("${jwt.secret}")
|
||||
private String secret;
|
||||
|
||||
@Value("${jwt.token-header:Authorization}")
|
||||
private String tokenHeader;
|
||||
|
||||
@Value("${gateway.ignore.urls:/**/auth/login,/**/auth/register,/**/auth/captcha,/content/video/public/**,/content/category/tree,/content/category/list,/comment/reply/public/**,/comment/public/**,/admin/auth/captcha,/admin/auth/login}")
|
||||
@Value("${gateway.ignore.urls:/**/auth/login,/**/auth/register,/**/auth/captcha,/**/auth/refresh,/auth/username/**,/content/video/public/**,/content/category/tree,/content/category/list,/content/tag/list,/content/tag/page,/content/danmaku/all/**,/content/danmaku/time,/content/stat/**,/user/profile/public/**,/user/profile/search,/user/follow/following/**,/user/follow/followers/**,/comment/reply/public/**,/comment/public/**,/recommend/home,/recommend/hot/**,/recommend/similar/**,/recommend/tag/**,/admin/auth/captcha,/admin/auth/login}")
|
||||
private List<String> ignoreUrls;
|
||||
|
||||
private final AntPathMatcher pathMatcher = new AntPathMatcher();
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private final ReactiveStringRedisTemplate redisTemplate;
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
@@ -63,15 +67,29 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
return unauthorized(exchange, "缺少token");
|
||||
}
|
||||
|
||||
if (path.startsWith("/admin/")) {
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
try {
|
||||
// 验证token
|
||||
Claims claims = parseToken(token);
|
||||
return redisTemplate.hasKey(USER_ACCESS_TOKEN_PREFIX + token)
|
||||
.flatMap(exists -> {
|
||||
if (!Boolean.TRUE.equals(exists)) {
|
||||
return unauthorized(exchange, "Token已失效");
|
||||
}
|
||||
|
||||
// 将用户信息传递到下游服务
|
||||
ServerHttpRequest.Builder mutate = request.mutate();
|
||||
mutate.header("userId", claims.getSubject());
|
||||
mutate.header("username", claims.get("username", String.class));
|
||||
return chain.filter(exchange.mutate().request(mutate.build()).build());
|
||||
// 将用户信息传递到下游服务
|
||||
ServerHttpRequest.Builder mutate = request.mutate();
|
||||
mutate.header("userId", claims.getSubject());
|
||||
mutate.header("username", claims.get("username", String.class));
|
||||
return chain.filter(exchange.mutate().request(mutate.build()).build());
|
||||
})
|
||||
.onErrorResume(e -> {
|
||||
log.error("Redis校验token失败", e);
|
||||
return unauthorized(exchange, "认证服务暂不可用");
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("Token验证失败", e);
|
||||
return unauthorized(exchange, "Token验证失败");
|
||||
@@ -141,4 +159,4 @@ public class AuthGlobalFilter implements GlobalFilter, Ordered {
|
||||
return response.setComplete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,12 @@ server:
|
||||
spring:
|
||||
application:
|
||||
name: video-gateway
|
||||
data:
|
||||
redis:
|
||||
host: ${REDIS_HOST:redis}
|
||||
port: ${REDIS_PORT:6379}
|
||||
password: ${REDIS_PASSWORD}
|
||||
database: 0
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
@@ -106,4 +112,8 @@ spring:
|
||||
args:
|
||||
retries: 3
|
||||
statuses: BAD_GATEWAY,SERVICE_UNAVAILABLE
|
||||
methods: GET,POST
|
||||
methods: GET,POST
|
||||
|
||||
jwt:
|
||||
secret: ${USER_JWT_SECRET}
|
||||
token-header: Authorization
|
||||
|
||||
@@ -9,6 +9,7 @@ spring:
|
||||
config:
|
||||
import:
|
||||
- optional:nacos:common.yaml?refreshEnabled=true
|
||||
- optional:nacos:common-redis-${spring.profiles.active}.yaml?refreshEnabled=true
|
||||
- optional:nacos:${spring.application.name}.yaml?refreshEnabled=true
|
||||
cloud:
|
||||
nacos:
|
||||
|
||||
Reference in New Issue
Block a user