authentication - Couchdb _session POST 未从 Angular 7 应用程序返回 Chrome 中的 set-cookie 标头
问题描述
我有一个 Angular 7 应用程序向 couchDb _sessions api 发出 POST 请求以进行身份验证。根据文档,我应该在响应标头中有一个 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 https://example.org/_session -H "Content-Type:application/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。cookie 也不存在于 chrome dev-tools 应用程序选项卡和浏览器的 cookie 部分中。
const _httpHeaders = new HttpHeaders();
const headers = _httpHeaders.append('withCredentials', 'true');
console.log(headers);
return this._httpService.doAsyncTask(AppConstants.LOGIN_URL, 'POST', data, {headers});
我怎样才能找到解决方案?
解决方案
使用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
对象不应再次包含凭据。
推荐阅读
- java - Does Gson’s fromJSON have a parsing limit on the string parameter?
- r - 使用 2 个不同大小的数据帧 R 的子集
- sockets - TServerSocket:如何在发送消息之前检查 ListView 上的特定客户端是否仍然连接?
- angular6 - How to show/hide the agm circle when user clicks on each marker pin - Angular 8
- r - R function writing - getting error: NaNs producedError in tsort[U + 1]only 0's may be mixed with negative subscripts
- amazon-web-services - 阻止公共访问设置的哪些组合使我的 s3 存储桶对所有人可见?
- javascript - Rest parameters produces an error in my react-bootstrap component?
- git - docker run 通过别名运行时的行为不同,然后 docker run
- api - 我们能否根据时间跟踪用户、组、ChatMessage 或 Planner 对象上的 Microsoft Delta 更改。“/我/计划者/所有/三角洲”
- css - 覆盖 Bootstrap CSS(链接颜色)