0

I am ultimately just trying to get static content (Angular) in a GCP storage bucket to send a POST request to an API Gateway Endpoint which routes to a GCP Function (Spring Boot REST API). For simplicity, I am currently just trying to get it to work without the API gateway.

Everybody is uploaded, but when my click send on my static page I get the standard CORS error message:

Access to XMLHttpRequest at 'https://my-gcf-endpoint/post' from origin 
'https://storage.googleapis.com' has been blocked by CORS policy: Response to 
preflight request doesn't pass access control check: It does not have HTTP ok status.

Researching this has taken me to here to create a JSON document and set it as my CORS config using gsutil cors set cors-json-file gs://<bucket_name> (Finally figured out that I have to do this from the Cloud SDK while in the local folder that has the file)

cors.json:

[
    {
      "origin": ["*"],
      "method": ["GET", "POST"],
      "responseHeader": ["*"],
      "maxAgeSeconds": 3600
    }
]

I used the above gsutil command on both my front end bucket and my backend bucket, but still getting the same error.

Inside my Spring Boot app, I added both global and method based cors config:

Main:

@SpringBootApplication
public class DivrFunctionApplication {

    public static void main(String[] args) {
        SpringApplication.run(DivrFunctionApplication.class, args);
    }

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

Controller:

@Component
@RestController
@CrossOrigin
public class MainController {

  @PostMapping("/postData")
  public ResponseEntity<String> postText(@RequestBody String message) {
    // edited to add these headers, but it still does not work
    HttpHeaders headers = new HttpHeaders();
    headers.set("ACCESS_CONTROL_ALLOW_ORIGIN","*");
    return new ResponseEntity<>(message, headers, HttpStatus.OK);
  }

  @GetMapping("/")
  public String hello() {
    return "hello world!";
  }
}

I have found some docs that show Java / Google Cloud Cors setups, but none are in Spring boot.

Any thoughts on what I can try next?

Front End Code & Back End Code

ETA:

Per the comment below, I also added this to my application.properties:

spring.mvc.dispatch-options-request=true

I will also not that this is as basic of an app as possible. no spring security, authentication of any kind, the storage buckets are all marked as public with AllUser Admin access, Allow Unauthenticated on GCF...

Like if someone randomly throws a rock in Gibraltar, I want it to hit this!

AylaWinters
  • 1,121
  • 2
  • 7
  • 24
  • Does this answer your question? [Spring Boot, CORS problem: Response to preflight request doesn't pass access control check: It does not have HTTP ok status](https://stackoverflow.com/questions/68093398/spring-boot-cors-problem-response-to-preflight-request-doesnt-pass-access-con) – Robert G Dec 14 '22 at 21:03
  • No, I did add the line into my application.properties, but same CORS error and I do not see a 405 anywhere. This all works locally, and there is no spring security, authentication etc, which is why I think it is a GCP issue – AylaWinters Dec 14 '22 at 23:34

0 Answers0