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 Answer
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
EXPERT
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions