{"id":2277,"date":"2019-12-08T17:17:28","date_gmt":"2019-12-08T17:17:28","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=2277"},"modified":"2019-12-08T17:17:28","modified_gmt":"2019-12-08T17:17:28","slug":"spring-boot-not-adding-cors-headers-in-response-when-using-webmvcconfigurer","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=2277","title":{"rendered":"Spring Boot not adding CORS headers in response when using WebMvcConfigurer"},"content":{"rendered":"<p>I was using this to add CORS headers dynamically based on application.properties.<\/p>\n<p>However initially Spring stubbornly refused to add the headers.<\/p>\n<p>My initial (incorrect) code was as follows:-<\/p>\n<blockquote>\n<p>@Configuration     <br \/>@EnableWebMvc      <br \/>public class WebConfig implements WebMvcConfigurer {      <br \/>&#160; <br \/>&#160;&#160;&#160; @Autowired      <br \/>&#160;&#160;&#160; private ConfigProperties config;      <br \/>&#160;&#160;&#160; <br \/>&#160;&#160;&#160; @Override      <br \/>&#160;&#160;&#160; public void addCorsMappings(CorsRegistry registry) {      <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; config.getCorsOriginsAllowed().forEach(origin -&gt; registry.addMapping(origin).allowedOrigins(origin));      <br \/>&#160;&#160;&#160; }      <br \/>}<\/p>\n<\/blockquote>\n<p>The error was that I had used origin both as the registry mapping key and the resulting allowed origin.<\/p>\n<p>The correct code should have used a URL path as the mapping key. The correct replacement line was as follows:-<\/p>\n<blockquote>\n<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; config.getCorsOriginsAllowed().forEach(origin -&gt; registry.addMapping(&quot;\/**&quot;).allowedOrigins(origin));<\/p>\n<\/blockquote>\n<p>This fixed version mapped every URL path to the given set of origins, using \u201c\/**\u201d as above.<\/p>\n<p><em>This fixed the problem.<\/em><\/p>\n<p><a href=\"https:\/\/www.baeldung.com\/spring-cors\"><strong>This post here by Baeldung<\/strong><\/a> and <a href=\"https:\/\/spring.io\/guides\/gs\/rest-service-cors\/\"><strong>this Spring guide post<\/strong><\/a> detail the various ways to add CORS headers, both in a fixed static way using annotations, and in dynamic ways such as the above.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was using this to add CORS headers dynamically based on application.properties. However initially Spring stubbornly refused to add the headers. My initial (incorrect) code was as follows:- @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { &#160; &#160;&#160;&#160; @Autowired &#160;&#160;&#160; private ConfigProperties config; &#160;&#160;&#160; &#160;&#160;&#160; @Override &#160;&#160;&#160; public void addCorsMappings(CorsRegistry registry) { &#160;&#160;&#160;&#160;&#160;&#160;&#160; config.getCorsOriginsAllowed().forEach(origin -&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[95,219],"tags":[94,221],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2277"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2277"}],"version-history":[{"count":1,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2277\/revisions"}],"predecessor-version":[{"id":2278,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/2277\/revisions\/2278"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}