0

I have a react application that is deployed on NGINX running on 9000 port successfully.

Then I have my spring boot war file deployed on tomcat server 9xx on port 8081.

And I have already enabled/added global cors in spring boot but still facing this issue.

As I have analysed I am not getting any response headers back from the server. enter image description here

In spring boot main class I have added this

@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedHeaders("*").allowedMethods("*").allowedOrigins("http://localhost:9000").allowCredentials(true);
            }
        };
    }

And allowed from http://localhost:9000 server

Request from browser fails with cors error, in this my preflight request is successful enter image description here

As my spring boot deployed on 8081 enter image description here

And my NGINX configs enter image description here

I know this question has been asked a lot, but I am stuck at it from last 7 to 8 hours.

John
  • 276
  • 1
  • 9
  • 29
  • See if this helps https://stackoverflow.com/questions/45986631/how-to-enable-cors-in-nginx-proxy-server/45994114#45994114 – Tarun Lalwani Apr 16 '21 at 18:18
  • 3
    The CORS configuration for the port 9000 nginx server isn’t relevant. It definitely has nothing to do with the error message cited in the question. The request is being sent from frontend JavaScript code (served from the port 9000 server) to the port 8081 Spring server. So as far as CORS configuration goes, the only thing that matters is the CORS configuration on the port 8081 server. So you probably want to update to question to change the title and to remove the details about the nginx CORS configuration. Those are just a distraction from trying to identify the actual cause. – sideshowbarker Apr 16 '21 at 22:39
  • would [this SO thread](https://stackoverflow.com/questions/36968963/how-to-configure-cors-in-a-spring-boot-spring-security-application) help with spring boot setup? – timur Apr 17 '21 at 12:37
  • I am assuming that you are not making the requests from the same machine on which you have your setup while answering. Your allowed origin is incorrect. Please use "*" for now instead of "http://localhost:9000". Let me know if that works. If you want to restrict origins, you should do so via domains. Origin specifies the origin of the request, not what you specified. – Mukul Bansal Apr 18 '21 at 06:18
  • @timur NO as I am not using spring secutiry – John Apr 19 '21 at 03:25
  • @MukulBansal, I am all my setup on 1 machine (Local Environment) and "*" does not work as I have used with credentials true. And have already tried it with various combinations. – John Apr 19 '21 at 03:26
  • @John have you annotated your configuration class appropriately? like this- `@Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); } }` Reference- https://www.baeldung.com/spring-cors – Mukul Bansal Apr 19 '21 at 10:59
  • I have added cors function inside main class, so no need of that. So inside main class annotated with @SpringBootApplication – John Apr 20 '21 at 10:59
  • Try to explicitly provide your allowed Headers and Methods. Sometimes useing '*' causing errors. If this not help you can provide a `CorsWebFilter` Bean. – Tr1monster Apr 23 '21 at 07:48

1 Answers1

-2

You can avoid CORS using proxy_pass. Configure your NGINX to proxy(forward) all the requests with URIs starting with '/api/' to the spring boot located at localhost: 8081.

upstream springboot_service {
    server localhost:8081;
}

location /api/ {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://springboot_service/;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_redirect off;
        }
Janani
  • 71
  • 4
  • @AjeetShah, but spring boot is not attaching response headers back, I checked, I think therefore this error is comming – John May 17 '21 at 06:41
  • @John You should retry with [these answers](https://stackoverflow.com/search?tab=relevance&q=spring%20boot%20cors) and [this tutorial](https://spring.io/guides/gs/rest-service-cors/). If it still doesn't work, I would suggest that you create a new spring boot project following that (official) tutorial to understand it better. There is no way other than as explained in those answer and tutorial. Try with simple and fresh projects; that would be easiest way to debug it. – Ajeet Shah May 17 '21 at 07:04
  • @AjeetShah, preflight request is successful, but actual request fails – John May 17 '21 at 07:34
  • @AjeetShah, can help me out by connecting..., Please I am stuck at it for days now – John May 17 '21 at 10:25
  • 1
    @AjeetShah I was successfully able to deploy resolve the cors on local environment as well as When I tried to deploy on production tomcat server. But still facing CORS issue wit h, JBOSS EAP 7.3 server. – John May 17 '21 at 13:54