title: Angular2 How to get token by sending credentials
body: <p>I'm using Angular2 to get access token from a Java Spring back-end application.
  I can get the token via CURL but not via Angular form.</p>
  
  <pre><code> curl localhost:8085/uaa/oauth/token --data "grant_type=password&amp;scope=write&amp;username=MY-USERNAME&amp;password=MY-PASSWORD" --user user:pwd
  </code></pre>
  
  <p>I enabled Cors on Java back-end like this:</p>
  
  <pre><code> public void doFilter(ServletRequest servletRequest, ServletResponse   servletResponse, FilterChain chain) throws IOException, ServletException {
      final HttpServletResponse response = (HttpServletResponse) servletResponse;
      response.setHeader("Access-Control-Allow-Origin", "*");
      response.setHeader("Access-Control-Allow-Credentials", "true");
      response.setHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE, GET, HEAD, OPTIONS");
      response.setHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, If-Modified-Since");
      chain.doFilter(servletRequest, servletResponse);
  }
  </code></pre>
  
  <p>My Angular code looks like this:</p>
  
  <pre><code>import {Injectable, Component} from 'angular2/core';
  import {Observable} from 'rxjs/Rx';
  import {Http, HTTP_PROVIDERS, Headers} from 'angular2/http';
  
  @Component({
     viewProviders: [HTTP_PROVIDERS]
  })
  
  @Injectable()
  export class Authentication {
    token:string;
    http:Http;
  
    constructor(http:Http) {
      this.token = localStorage.getItem('token');
      this.http = http;
    }
  
    login(username:String, password:String) {
  
      var url = 'http://localhost:8085/uaa/oauth/token',
          body = JSON.stringify({
              username: username,
              password: password
          }),
          options = {
              headers: new Headers({
                  'credentials': 'true',
                  'grant_type': 'password',
                  'scope': 'write',
                  'Accept': 'application/json',
                  'Content-Type': 'application/x-www-form-urlencoded'
              })
          };
  
        return this.http.post(url, body, options)
          .map((res:any) =&gt; {
              let data = res.json();
              this.token = data.token;
              localStorage.setItem('token', this.token);
          });
     }
  }
  </code></pre>
  
  <p>The response from the server is:</p>
  
  <pre><code>Request URL:http://localhost:8085/uaa/oauth/token
  Request Method:OPTIONS
  Status Code:401 Unauthorized
  Remote Address:[::1]:8085
  </code></pre>