Struggling to communicate between lightsail containers

0

Hi there,

I am currently struggling to communicate between my lightsail container instances. In one container i have a React app, the other a java spring boot backend. I can curl commands from my local to the backend api and i get a success response, but when i try to do this programatically within the front end, the same call fails.

The documentation is super unclear around this point, can anyone guide me in the right direction?

I have tried using the public domain of the container, the private, with and without the open port. None of this has worked, and it's always unable to resolve the domain.

ERROR

1 Risposta
0

your issue might be related to CORS (Cross-Origin Resource Sharing), which is a security feature implemented in web browsers to prevent web pages from making requests to a different domain than the one that served the web page

ince your React app and Java Spring Boot backend are running on different containers (and therefore have different domains), you'll need to configure CORS properly in your backend to allow your frontend to make requests.

You can enable CORS globally in your Spring Boot application by adding the following configuration to your main application class

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**") // Apply CORS to all endpoints
                        .allowedOrigins("http://your-react-app-domain.com") // Replace with your React app's domain
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS") // Allowed HTTP methods
                        .allowCredentials(true) // Allow cookies to be sent with the request
                        .maxAge(3600); // Maximum time in seconds that the browser should cache the CORS policy
            }
        };
    }
}

hope it works for you

profile picture
ESPERTO
con risposta un anno fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande