spring-cloud / spring-cloud/spring-cloud-config

URI is not changed even using GATEWAY_REQUEST_URL_ATTR

Open
#2,599 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting-for-triage
Dominant language
Java
Stars
2k
Forks
1.3k
Avg merge
2d 59m
Merged PRs (30d)
16

Description

Describe the bug
I need to redirect URI port based on some repository, the repository works well, and it's not the problem, the problem is the default uri http://localhost:9998 is always being fallen in, even if portaBackend is not null and differente from 9998, I tried so many ways and it's still getting the wrong door

Sample

@Configuration
public class Balancer {

    @Autowired
    private AssinanteRepository assinanteRepository;

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("dynamic_route", r -> r.path("/**")
                                .filters(f -> f.filter((exchange, chain) -> {
                                    // GETTING 'CNPJ' FROM COOKIE/HEADER TO MATCH PORT IN REPOSITORY
                                    String cnpj;
                                    HttpCookie cnpjCookie = exchange.getRequest().getCookies().getFirst("cnpj");
                                    if (cnpjCookie != null) { 
                                        cnpj = cnpjCookie.getValue();
                                    } else { // Quando o usuário loga
                                        cnpj = exchange.getRequest().getHeaders().getFirst("cnpj");
                                    }

                                    // LOOKS FOR REGARDING PORT ON REPOSITORY BASED ON CNPJ
                                    Integer portaBackend = assinanteRepository.findById(cnpj)
                                            .map(assinante -> assinante.getApiPORT()).orElse(null);

                                    // MODIFIES THE REQUEST WHEN A PORT IS FOUND
                                    if (portaBackend != null) {
                                        // SET NEW URI
                                        String redirect = "http://localhost:" + portaBackend + exchange.getRequest().getURI().getPath();
                                        URI uri;
                                        try {
                                            uri = new URI(redirect);
                                        } catch (URISyntaxException e) {
                                            return Mono.error(new RuntimeException("Erro na construção da URI"));
                                        }

                                        // ADDING TO CONTEXT
                                        addOriginalRequestUrl(exchange, exchange.getRequest().getURI());

                                        // CHANGING URI REQUEST
                                        ServerHttpRequest modifiedRequest = exchange.getRequest().mutate().uri(uri).build();
                                        ServerWebExchange modifiedExchange = exchange.mutate().request(modifiedRequest).build();

                                        // CHANGING ATTRIBUTE
                                        modifiedExchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, uri);

                                        return chain.filter(modifiedExchange).then(Mono.fromRunnable(() -> {
                                            ServerHttpResponse response = exchange.getResponse();
                                            System.out.println("Requisição redirecionada para: " + redirect);
                                        }));
                                    } else {
                                        return Mono.error(new RuntimeException("CNPJ não encontrado"));
                                    }
                                })).uri("http://localhost:9998")
                        // 
                ).build();
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

First verify that this issue belongs in spring-cloud-config, since the example uses Spring Cloud Gateway routing. Reproduce the custom RouteLocator with GATEWAY_REQUEST_URL_ATTR and determine why the configured http://localhost:9998 URI takes precedence; done when the repository-selected port is used or the required limitation is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.