Couchdb _session POST不从Angular 7应用中返回Chrome中的set-cookie标头

时间:2019-06-10 12:30:56

标签: authentication post cookies couchdb angular7

我有一个Angular 7应用程序,向长沙发_sessions api发出POST请求以进行身份​​验证。根据{{​​3}},我应该在响应标头中有一个set-cookie标头,但是没有set-cookie标头。

这是响应的样子:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:5800
Access-Control-Expose-Headers: content-type, cache-control, accept-ranges, etag, server, x-couch-request-id, x-couch-update-newrev, x-couchdb-body-time
Cache-Control: must-revalidate
Connection: keep-alive
Content-Length: 46
Content-Type: application/json
Date: Mon, 10 Jun 2019 20:21:53 GMT
Server: nginx/1.10.3 (Ubuntu)

但是如果我通过curl发送相同的请求:

  

curl -v documentation -H   “ Content-Type:应用程序/ json
  “ -d'{” name“:” user“,” password“:” password“}'

这就是我得到的:

< HTTP/1.1 200 OK
< Server: nginx/1.10.3 (Ubuntu)
< Date: Mon, 10 Jun 2019 20:25:54 GMT
< Content-Type: application/json
< Content-Length: 46
< Connection: keep-alive
< Cache-Control: must-revalidate
< Set-Cookie: AuthSession=c2JxMTIzOjVDRkVCQ0QyOvjMZQhdhyUqRjmDjl_Hipiz7hxa; Version=1; Expires=Mon, 10-Jun-2019 20:35:54 GMT; Max-Age=600; Path=/; HttpOnly
<
{ [46 bytes data]
100    87  100    46  100    41     46     41  0:00:01 --:--:--  0:00:01   154{"ok":true,"name":"username","roles":["sales"]}

我也尝试将{withCredentials:true}放在标题中,但仍然没有得到任何cookie。 chrome dev-tools应用程序标签中以及浏览器的cookie部分中也没有该cookie。

    const _httpHeaders = new HttpHeaders();
    const headers = _httpHeaders.append('withCredentials', 'true');
    console.log(headers);
    return this._httpService.doAsyncTask(AppConstants.LOGIN_URL, 'POST', data, {headers});

我该如何解决?

1 个答案:

答案 0 :(得分:0)

使用Basic Authorization标头和withCredentials: true应该有效。

let httpHeaders = new HttpHeaders();
httpHeaders = httpHeaders.set('Accept', 'application/json');
httpHeaders = httpHeaders.set('Content-type', 'application/json');
httpHeaders = httpHeaders.set('Authorization', 'Basic ' + btoa(user + ':' + password));
const options = { headers: httpHeaders, withCredentials: true };

this._httpService.doAsyncTask(AppConstants.LOGIN_URL, 'POST', data, options);

btoa()使用base-64编码用户/密码。 data对象不应再次包含凭据。

相关问题