に角度2を使用して要求を作るにはどうすればcurlコマンドでのOAuth2認証要求は以下の通りですhttps://github.com/royclarkson/spring-rest-service-oauth私は春・セキュリティのOAuth2 REST
からの例を使用して春のセキュリティのOAuth2サーバーを作成しました。私はAngular2で同等の構文が必要です。
import { Injectable } from '@angular/core';
import {Http, Headers, RequestOptions, RequestMethod} from '@angular/http';
import {Router} from '@angular/router';
@Injectable()
export class AuthService {
isAuthenticated: boolean = false;
userId;
constructor(private http: Http, private router: Router) { }
login(usercreds){
let client_id = 'clientapp';
let client_secret = '123456';
var basicheader = btoa(client_id + ':' + client_secret);
console.log(basicheader);
var headers = new Headers();
headers.append('Authorization', 'Basic' + basicheader);
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
//let options = new RequestOptions({method: RequestMethod.Post, headers: headers });
var creds = 'username=' + usercreds.username + '&password=' + usercreds.password+'credentials=true&grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp';
console.log(creds);
return new Promise((resolve) => {
this.http.post('http://localhost:8080/oauth/token', JSON.stringify(creds), {headers:headers}).subscribe((data) => {
if(data.json().success) {
this.userId = data.json().userId;
this.isAuthenticated = true;
}
resolve(this.isAuthenticated);
})
})
}
}
しかし、私はこのアプリケーションを起動したときにGoogle Chromeブラウザは、開発者モードでこのエラーを返します:ここで
curl -X POST -vu clientapp:123456 http://localhost:8080/oauth/token -H "Accept: application/json" -d "password=spring&username=roy&grant_type=password&scope=read%20write&client_secret=123456&client_id=clientapp"
は、私が試したものです
- のXMLHttpRequestがhttp://localhost:8080/oauth/tokenをロードすることはできません。プリフライトのための応答が無効なHTTPステータスコード401
- 例外があります:URL 0:ステータスを持つレスポンスヌル