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개 답변
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
전문가
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠