title: Enable CORS for OPTIONS request using Spring Framework
body: <p>Every time I make a PUT Ajax call to my service, it return the following error:</p>
  
  <blockquote>
    <p>XMLHttpRequest cannot load <a href="http://localhost:8080/users/edit" rel="noreferrer">http://localhost:8080/users/edit</a>. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="http://localhost:63342" rel="noreferrer">http://localhost:63342</a>' is therefore not allowed access. The response had HTTP status code 403.</p>
  </blockquote>
  
  <p>After 2 days of investigation, I've reached to try the next solution on my code.</p>
  
  <p>This is the <strong><em>main class</em></strong> where I load the necessary classes and run the application:</p>
  
  <pre><code>@SpringBootApplication
  @EnableAutoConfiguration
  public class Application extends SpringBootServletInitializer{
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          return application.sources(DispatcherServletInitializer.class, OptionsController.class,Application.class);
      }
  }
  </code></pre>
  
  <p>The <strong><em>DispatcherServilet initializer</em></strong>, where I enable the dispatchOptionsRequest:</p>
  
  <pre><code>public abstract class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      protected void customizeRegistration(ServletRegistration.Dynamic registration) {
          registration.setInitParameter("dispatchOptionsRequest", "true");
          super.customizeRegistration(registration);
      }
  }
  </code></pre>
  
  <p>A <strong><em>controller</em></strong> for handle all OPTIONS request:    </p>
  
  <pre><code>@Controller
  public class OptionsController {
  
      @RequestMapping(method = RequestMethod.OPTIONS)
      public HttpServletResponse handle(HttpServletResponse theHttpServletResponse) throws IOException {
          theHttpServletResponse.addHeader("Access-Control-Allow-Headers", "origin, content-type, accept, x-requested-with");
          theHttpServletResponse.addHeader("Access-Control-Max-Age", "60"); 
          theHttpServletResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
          theHttpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
          return theHttpServletResponse;
      }
  
  }
  </code></pre>
  
  <h2><strong>What I'm doing wrong with the configuration?</strong></h2>