2016-09-23 2 views
1

Oauth2 인증에 심각한 문제가 있습니다. 저녁 4시 이후에 천천히 포기하고 있습니다. 누군가가 나를 도울 수 있기를 바랍니다.기본 인증 헤더가 oauth/토큰 요청에 대해 제거되었습니다.

클라이언트 : 별도의 프런트 엔드 프로젝트로 Angular2 클라이언트가 있습니다. oauth/token post가 좋아할만한 방법을 알고 있습니다. 왜냐하면 나는 이미 Postman과 테스트를했기 때문입니다. 것은 권한 부여 헤더가 제거되고 서버에 도달하지 않는다는 것입니다. 참고 : Access Control 헤더를 필사적으로 추가하여 작동하도록했습니다. 그것은 어떤 차이가 있을지 ... 확실하지 않다

let headers = new Headers(); 
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8'); 
headers.append('Accept', 'application/json'); 
headers.append('Access-Control-Allow-Origin', '*'); 
headers.append('Access-Control-Allow-Headers', 'Authorization'); 
headers.append('Authorization', 'Basic ' + Base64.encode('angularApp:theBiggestSecret')); 

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

this.http.post(`${this.baseUrl}oauth/token`, "grant_type=password&scope=read&username=test&password=test&client_id=angularApp&client_secret=theBiggestSecret", headers) 
    .subscribe(
    response => console.log(response) 
); 

}

서버 : 나는 springSecurityFilterChain 바로 거기 이니셜의 등록과 스프링 MVC 응용 프로그램은, "WebApplicationInitializer"로 시작했습니다. 모든 구성은 주석을 통해 수행되며 웹 응용 프로그램 컨텐츠는 전혀 수행되지 않습니다. 임베디드 부두에서 시작되었습니다.

동일한 응용 프로그램에서 AuthorizationServer와 ResourceServer를 모두 구성 했으므로 http.addFilterBefore를 통해 SecurityConfiguration에 CorsFilter를 구성 했으므로 작동하는 것을 볼 수 있습니다.

문제점 : 글쎄, 인증 헤더가 제거되어 기본 인증이 실행되지 않고 해당 액세스 토큰을받지 못합니다. 그러나, 나는 지금 제대로 작동 CORS, 내가 브라우저에서 관련 CORS 오류를 받고 있지 않다 내가 응답 헤더로 이것을 볼 수 있다고 생각합니다 :

POST /oauth/token HTTP/1.1 
Host: localhost:8080 
Connection: keep-alive 
Content-Length: 115 
Origin: http://localhost:4200 
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 
content-type: text/plain 
Accept: */* 
Referer: http://localhost:4200/ 
Accept-Encoding: gzip, deflate 
Accept-Language: cs-CZ,cs;q=0.8,en;q=0.6 
:

HTTP/1.1 401 Unauthorized 
Date: Fri, 23 Sep 2016 21:41:34 GMT 
Access-Control-Allow-Origin: * 
Vary: Origin 
Access-Control-Expose-Headers: Authorization, Content-Type 
Cache-Control: no-store 
Pragma: no-cache 
WWW-Authenticate: Bearer error="unauthorized", error_description="There is no client authentication. Try adding an appropriate authentication filter." 
Content-Type: application/json;charset=UTF-8 
X-Content-Type-Options: nosniff 
X-XSS-Protection: 1; mode=block 
Transfer-Encoding: chunked 
Server: Jetty(9.3.11.v20160721) 

이 요청에서 전송됩니다 것입니다

+0

'헤더'를 어떻게 사용하고 있는지 게재 할 수 있습니까? – Brad

+0

안녕하세요, 저는 당신이 무엇을 원하는지 잘 모르겠습니다. 각도에서 HTTP POST 요청에 어떻게 사용합니까? 또는 서버 스프링 구성의 일부입니까? springSecurityFilterChain의 첫 번째 필터로 cors 필터를 추가하는 것 외에는 아무 것도하지 않습니다. – Durin

+0

예, 각진 상태입니다. – Brad

답변

2

post() 방법에서 headers을 올바르게 설정하지 않았습니다.

이 작업을 수행하여 문제를 해결할 수 있습니다

this.http.post(`${this.baseUrl}oauth/token`, '<form data>', { headers: headers }) 
    .subscribe(response => console.log(response)); 

또한 URLSearchParams 객체에 양식 데이터를 생성하고 각도가 자동으로 당신을 위해 application/x-www-form-urlencoded에 콘텐츠 형식을 설정할 수 있도록 본문으로 설정할 수 있습니다.

let body = new URLSearchParams(); 
body.set('grant_type', 'password'); 
body.set('scope', 'read'); 
body.set('username', 'test'); 
body.set('password', 'test'); 
body.set('client_id', 'angularApp'); 
body.set('client_secret', 'theBiggestSecret'); 

this.http.post(`${this.baseUrl}oauth/token`, body, { headers: headers }) 
     .subscribe(response => console.log(response)); 
+0

오, 이런, 내가 잘못한 것을 본다. 나는 거기에 옵션이 아니라 헤더를 넣는다. 고맙습니다. 나는 약간 피곤해. 나는 코를 작동 시키려고 노력한 후에 추측한다. 고맙습니다! – Durin

관련 문제