question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

XForwardPrefixPathAdjuster when having multiple proxys

See original GitHub issue

Hi i’m currently using v2.9.2 on my services.

I have a microservice environment, where all services are behind a Spring Cloud Gateway API proxy. Additional to this, the whole system is running on a Kubernetes cluster with a Traefik Ingress controller in place. So Traefik and Spring Cloud Gateway act as reverse proxies until the request eventually reaches the service in the backend. There, Swagger2 is running and provides the API docs.

Unfortunately it generates the field basePath wrong, because the header contains a comma separated list of stripped prefixes for every proxy hop: /api,/my-service.

The logic in XForwardPrefixPathAdjuster only uses the value in this header as a whole, but should in my opinion merge all stripped prefixes again (i.e. just remove commas) to generate the base-path for use in the browser UI.

What do you think? Am i on the right path?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
george510257commented, Aug 10, 2020

I have the same problem. In my gateway project the source like this.

@Component
@Primary
@Slf4j
public class GatewaySwaggerResourcesProvider implements SwaggerResourcesProvider {

    private static final String API_URI = "/v3/api-docs";
    private static Map<String, SwaggerResource> swaggerResources = new HashMap<>();
    private final RouteDefinitionLocator routeLocator;

    @Value("${spring.cloud.gateway.discovery.locator.route-id-prefix}")
    private String routeIdPrefix;

    @Value("${spring.application.name}")
    private String gatewayName;

    public GatewaySwaggerResourcesProvider(RouteDefinitionLocator routeLocator) {
        this.routeLocator = routeLocator;
        loadSwaggerResources();
    }

    private void loadSwaggerResources() {
        // 从DiscoveryClientRouteDefinitionLocator 中取出routes,构造成swaggerResource
        routeLocator.getRouteDefinitions().subscribe(routeDefinition -> {

            String name = routeDefinition.getId().substring(routeIdPrefix.length());
            String location = routeDefinition.getPredicates().get(0).getArgs().get("pattern").replace("/**", API_URI);

            if (!gatewayName.equalsIgnoreCase(name)) {
                swaggerResources.put(name, swaggerResource(name, location));
            }
        });
    }

    @Override
    public List<SwaggerResource> get() {
        loadSwaggerResources();
        return new ArrayList<>(swaggerResources.values());
    }

    private SwaggerResource swaggerResource(String name, String location) {
        log.info("name: " + name + " url: " + location);
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion("3.0.3");
        return swaggerResource;
    }

the url is right. image but request url is not right. image

1reaction
CRUDCodingcommented, Apr 17, 2020

I also encountered the same problem.swagger’s basePath is affected by x-forwarded-prefix in HTTP request header.The spring cloud gateway will automatically add x-forwarded-prefix when forwarding the request.This is the cause of the problem. So I disabled auto-add x-forwarded-prefix through the spring.cloud.gateway.x-forwarded.prefix-enabled=false configuration.Then added the correct x-forwarded-prefix through the - AddRequestHeader=X-Forwarded-Prefix, /api/my-service configuration. This solved my problem, but I don’t know if there’s a better way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[2112.12904] Quantile Regression with Multiple Proxy Variables
This study considered quantile regression estimation when a key covariate had multiple proxies across several datasets.
Read more >
Ember CLI with Multiple Proxies - Stack Overflow
I have an Ember CLI Application that will consume multiple APIs, which I need to proxy to in development mode. Background:.
Read more >
Set up proxy to work with multiple APIs in create react app
To handle multiple API, we need to install a NPM package “http-proxy-middleware” as dev dependency, create a file with name setupProxy.js ...
Read more >
Articles If running multiple Duo Authentication Proxies for high ...
Yes. Having [cloud] sections in each of your Duo Authentication Proxies is perfectly fine, so long as they are all associated with the...
Read more >
API Product With Multiple Proxy and Limited Resource
Hello Team, I have two proxies: Contacts and Investors with the endpoints /metadata, /refdata, /{externaId}/accounts in both of them.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found