2017-01-23 1 views
1

리소스 서버와 인증 서버가있는 스프링 부팅을 사용하여 개발 된 OAuth2 용 Rest API가 있습니다. cURL, POSTMAN Rest 클라이언트를 사용하여 성공적으로 토큰을 가져올 수 있었고 OAuth가 제공 한 토큰을 사용하여 부여 된 서비스를 요청할 수있었습니다. 이제 OAuth2 client_id, angle_2를 사용하는 secret_id를 전달하는 방법을 알아야합니다. 내가 getOAuthToken (clientCredintial)와 같은 요청 전달하고 {Angular 2를 사용하여 OAuth2의 클라이언트 ID와 비밀 ID를 보내는 방법은 무엇입니까?

var body = `username=admin&password=nex1234`; 
var headers = new Headers(); 
headers.append('Content-Type', 'application/x-www-form-urlencoded'); 
let response = this.http.post(`${AppSettings.BACK_ENDPOINT}/oauth/token`,body,{ headers: headers }); 
let oauthTokenItems = response.map(mapOAuthTokenItems); 
return oauthTokenItems; 

} 관찰 가능한을하지만 난 옵션 http://localhost:8080/rwsweb/oauth/token 401()

oauth-token:1 XMLHttpRequest cannot load     http://localhost:8080/rwcweb/oauth/token. Response for preflight has invalid  HTTP status code 401 
error_handler.js:47 EXCEPTION: Response with status: 0 for URL: null 
ErrorHandler.handleError @ error_handler.js:47 
next @ application_ref.js:272 
schedulerFn @ async.js:82 

Subscriber.js로 브라우저에 예외를 얻고있다 : 227 catch되지 않은 응답 {_body : ProgressEvent, status : 0, ok : false, statusText : "", 헤더 : 헤더 ...} enter code here

답변

0

코드 사용 :

import { Injectable } from '@angular/core' 
import { Http } from '@angular/http'; 
import { Headers, RequestOptions } from '@angular/http'; 

@Injectable() 
export class LoginService { 
    private urlLogin: string; 

    constructor(private http: Http) { 
     this.urlLogin = "http://yourserver/oauth/token"; 
    } 

    public login(user) { 

     let headers = new Headers({ 
      "Content-Type": "application/x-www-form-urlencoded", 
      "Accept": "application/json", 
      "Authorization": "Basic " + btoa("yourclientid" + ':' + "yourclientsecret") 
     }); 

     let options = new RequestOptions({ headers: headers }); 

     let data = "username=" + user.yourlogin + "&password=" + encodeURIComponent(user.yourpass) + "&grant_type=password&" + 
      "client_secret=yourclientsecret&client_id=yourclientid"; 

     return this.http.post(this.urlLogin, data, options) 
      .map(res => res.json()); 
    } 

} 
+0

나는 위의 시도했지만 XMLHttpRequest를 HTTP를로드 할 수 없기 때문에 지금은 크롬 brawser에 한 가지 예외를 얻고있다 : // localhost를 : 8080/rwcweb/OAuth를/토큰입니다. 프리 플라이트에 대한 응답에 유효하지 않은 HTTP 상태 코드가 있습니다. –

+0

자격 증명이 맞는지 확인하십시오. 귀하의 사용자, 패스, grant_type ... –

+0

우편 배달부에서의 테스트? –

관련 문제