feat(gateway): 自定义网管添加Token内容

This commit is contained in:
Bunny 2025-02-10 23:03:21 +08:00
parent ee182a9c08
commit 0acd71150e
2 changed files with 34 additions and 1 deletions

View File

@ -0,0 +1,32 @@
package cn.bunny.filter;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import java.util.UUID;
@Component
public class OnceTokenGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {
@Override
public GatewayFilter apply(NameValueConfig config) {
return (exchange, chain) -> chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
ServerHttpResponse response = exchange.getResponse();
HttpHeaders headers = response.getHeaders();
String value = config.getValue();
if ("uuid".equalsIgnoreCase(value)) {
value = UUID.randomUUID().toString();
} else if ("uuid2".equalsIgnoreCase(value)) {
value = "JWT生产内容";
}
headers.add(config.getName(), value);
}));
}
}

View File

@ -39,4 +39,5 @@ spring:
- RewritePath=/api/cloud2/?(?<segment>.*), /cloud2/$\{segment} # 网关路径重写
# 默认过滤器
default-filters:
- AddResponseHeader=X-Response-Abc,123 # 在请求头中添加
- AddResponseHeader=X-Response-Abc,123 # 在请求头中添加
- OnceToken=X-Response-Token, uuid # 配置完成后在响应头中添加 UUID