title: Spring Boot: Cors policy missing 'access-control-allow-origin'
body: <p>Error: I get the following in Firefox: Foreign Site Query Blocked: The same origin policy does not allow reading of the remote resource http: // localhost: 8080 / api / v1 / post /. (Cause: The 'Access-Control-Allow-Origin' CORS header does not exist)</p>
  <p>I have spent several hours to allow CORS communication with my Spring Boot server in order to make my REACT UI communicate with the server. There are lot of similarly phrased questions on Stack Overflow, but non of the suggested solutions have solved my problem...</p>
  <p>Spring Boot suggests different solutions on <a href="https://spring.io/guides/gs/rest-service-cors/" rel="nofollow noreferrer">https://spring.io/guides/gs/rest-service-cors/</a>. One local solution is using annotations. A global solution can be obtained with a config file.</p>
  <p>I have tried annotating the controller with <code>@CrossOrigin</code> as seen in my code below:</p>
  <pre><code>package com.example.Blogging.api;
  
  import com.example.Blogging.model.Post;
  import com.example.Blogging.service.PostService;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.*;
  
  import java.util.List;
  import java.util.Optional;
  import java.util.UUID;
  
  
  @CrossOrigin(origins = &quot;http://localhost:3000&quot;)
  @RequestMapping(&quot;api/v1/post&quot;)
  @RestController
  public class PostController {
  
      private final PostService postService;
  
      @Autowired
      public PostController(PostService postService) {
          this.postService = postService;
      }
  
  
      @PostMapping
      public void addPost(@RequestBody Post post){
          postService.addPost(post);
  
      }
  
      @GetMapping
      public List&lt;Post&gt; getAllPosts(){
          return postService.getAllPosts();
  
      }
  
      @GetMapping(path = &quot;{id}&quot;)
      public Optional&lt;Post&gt; getPostById(@PathVariable(&quot;id&quot;) UUID id){
          return postService.getPostById(id);
  
      }
  
      @PutMapping(path=&quot;{id}&quot;)
      public void updatePostById(@PathVariable(&quot;id&quot;) UUID id, @RequestBody Post post){
          postService.updatePost(id,post);
      }
  
  
      @DeleteMapping(path=&quot;{id}&quot;)
      public void deletePostById(@PathVariable(&quot;id&quot;) UUID id){
          postService.deletePost(id);
      }
  
  
  }
  
  </code></pre>
  <p>This did not work.. I have also tried annotating on each method instead of the whole controller class.</p>
  <p>Furthermore I have tried to make a config file:</p>
  <pre><code>package com.example.Blogging.config;
  
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.servlet.config.annotation.CorsRegistry;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  
  @Configuration
  @EnableWebMvc
  public class WebConfig extends WebMvcConfigurerAdapter {
  
      @Override
      public void addCorsMappings(CorsRegistry registry) {
          registry.addMapping(&quot;/**&quot;)
          .allowedOrigins(&quot;*&quot;);
      }
  }
  </code></pre>
  <p>Nothing really seems to work. The console in my browser keeps saying 'access-control-allow-origin' is missing in the response header..</p>
  <p>For reference, I will also put my pom.xml below:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.5.2&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.example&lt;/groupId&gt;
      &lt;artifactId&gt;Blogging&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;Blogging&lt;/name&gt;
      &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
  
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- SPRING SECURITY --&gt;
          &lt;!--&lt;dependency&gt;
              &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
              &lt;artifactId&gt;spring-security-web&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;--&gt;
  
  
  
          &lt;!-- SPRING BOOT STARTER SECURITY --&gt;
          &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
              &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.flywaydb&lt;/groupId&gt;
              &lt;artifactId&gt;flyway-core&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;8.0.25&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
  
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.0.2&lt;/version&gt;
              &lt;/plugin&gt;
  
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  <p>Anyone who can suggest a solution? Thank you in advance!</p>