fix(gateway): 补充预检请求标识过滤器
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package com.baiyue.gateway.filter;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
import org.springframework.web.server.WebFilterChain;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/** 确保跨域预检和安全失败响应也携带可追踪的请求标识。 */
|
||||
@Component
|
||||
public class CorrelationIdWebFilter implements WebFilter, Ordered {
|
||||
public static final String HEADER = "X-Request-Id";
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
String requestId = validRequestId(exchange.getRequest().getHeaders())
|
||||
? exchange.getRequest().getHeaders().getFirst(HEADER)
|
||||
: UUID.randomUUID().toString();
|
||||
exchange.getResponse().getHeaders().set(HEADER, requestId);
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
|
||||
private static boolean validRequestId(HttpHeaders headers) {
|
||||
String value = headers.getFirst(HEADER);
|
||||
return value != null && !value.isBlank() && value.length() <= 128
|
||||
&& value.chars().allMatch(character -> character >= 0x21 && character <= 0x7e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Ordered.HIGHEST_PRECEDENCE;
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baiyue.gateway.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.gateway.config.GatewayProperties;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest(properties = {
|
||||
"spring.cloud.nacos.discovery.enabled=false",
|
||||
"spring.cloud.nacos.discovery.register-enabled=false",
|
||||
"spring.cloud.nacos.config.enabled=false",
|
||||
"spring.cloud.gateway.discovery.locator.enabled=false",
|
||||
"baiyue.security.jwt-secret=test-jwt-secret-at-least-32-bytes-long"
|
||||
})
|
||||
class GatewayFileRouteConfigurationTest {
|
||||
@Autowired
|
||||
private GatewayProperties gatewayProperties;
|
||||
|
||||
@Test
|
||||
void qualificationRequestsRouteToRegisteredFileServiceName() {
|
||||
var route = gatewayProperties.getRoutes().stream()
|
||||
.filter(candidate -> "file-service".equals(candidate.getId()))
|
||||
.findFirst()
|
||||
.orElseThrow();
|
||||
|
||||
assertThat(route.getUri().toString()).isEqualTo("lb://file-service");
|
||||
assertThat(route.getPredicates()).singleElement().satisfies(predicate -> {
|
||||
assertThat(predicate.getName()).isEqualTo("Path");
|
||||
String paths = String.join(",", predicate.getArgs().values());
|
||||
assertThat(paths)
|
||||
.contains("/api/merchant/v1/files/**")
|
||||
.contains("/api/platform/v1/files/**");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user