title: Spring security, cors error when enable Oauth2
body: <p>I'm getting an error while querying my oauth/token endpoint. </p>
  
  <p>I've configured cors enable for my resource / also tried to allow all resources but nothing worked. </p>
  
  <blockquote>
    <p>XMLHttpRequest cannot load <a href="http://localhost:8080/oauth/token" rel="noreferrer">http://localhost:8080/oauth/token</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:1111" rel="noreferrer">http://localhost:1111</a>' is therefore not allowed
    access. The response had HTTP status code 401.</p>
  </blockquote>
  
  <pre><code>vendor.js:1837 ERROR SyntaxError: Unexpected token u in JSON at position 0
      at JSON.parse (&lt;anonymous&gt;)
      at CatchSubscriber.selector (app.js:7000)
      at CatchSubscriber.error (vendor.js:36672)
      at MapSubscriber.Subscriber._error (vendor.js:282)
      at MapSubscriber.Subscriber.error (vendor.js:256)
      at XMLHttpRequest.onError (vendor.js:25571)
      at ZoneDelegate.invokeTask (polyfills.js:15307)
      at Object.onInvokeTask (vendor.js:4893)
      at ZoneDelegate.invokeTask (polyfills.js:15306)
      at Zone.runTask (polyfills.js:15074)
  defaultErrorLogger @ vendor.js:1837
  ErrorHandler.handleError @ vendor.js:1897
  next @ vendor.js:5531
  schedulerFn @ vendor.js:4604
  SafeSubscriber.__tryOrUnsub @ vendor.js:392
  SafeSubscriber.next @ vendor.js:339
  Subscriber._next @ vendor.js:279
  Subscriber.next @ vendor.js:243
  Subject.next @ vendor.js:14989
  EventEmitter.emit @ vendor.js:4590
  NgZone.triggerError @ vendor.js:4962
  onHandleError @ vendor.js:4923
  ZoneDelegate.handleError @ polyfills.js:15278
  Zone.runTask @ polyfills.js:15077
  ZoneTask.invoke @ polyfills.js:15369
  </code></pre>
  
  <p>With Postman everything works perfect.</p>
  
  <p>My cors security configuration:</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  public class WebConfig extends WebMvcConfigurerAdapter {
      @Override
      public void addCorsMappings(CorsRegistry registry) {
          registry.addMapping("/**")
                  .allowedOrigins("*")
                  .allowedHeaders("*")
                  .allowedMethods("*")
                  .allowCredentials(true);
      }
  }
  </code></pre>
  
  <p>also tried to add <a href="http://localhost:1111" rel="noreferrer">http://localhost:1111</a> in allowed origins</p>
  
  <p>Code in Postman:</p>
  
  <pre><code>require 'uri'
  require 'net/http'
  
  url = URI("http://localhost:8080/oauth/token")
  
  http = Net::HTTP.new(url.host, url.port)
  
  request = Net::HTTP::Post.new(url)
  request["content-type"] = 'application/x-www-form-urlencoded'
  request["authorization"] = 'Basic Y2hhdHRpbzpzZWNyZXRzZWNyZXQ='
  request["cache-control"] = 'no-cache'
  request["postman-token"] = 'daf213da-e231-a074-02dc-795a149a3bb2'
  request.body = "grant_type=password&amp;username=yevhen%40gmail.com&amp;password=qwerty"
  
  response = http.request(request)
  puts response.read_body
  </code></pre>