2015.12.18 更新
经过大量测试后,我得出了概述的第二个解决方案。您需要做的就是安装带有 headers-more-module 的 nginx。将以下内容添加到您的 nginx-config 中:
location / {
# forward all request headers to backend
proxy_pass_request_headers on;
# these settings come from the CouchDB wiki
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# your CouchDB backend
proxy_pass http://127.0.0.1:5984;
# replace WWW-Authenticate header in response if authorization failed
more_set_headers -s 401 'WWW-Authenticate: Other realm="App"';
}
# location to handle access to Futon
location /_utils/ {
# forward all request headers to backend
proxy_pass_request_headers on;
# these settings come from the CouchDB wiki
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# your CouchDB backend
proxy_pass http://127.0.0.1:5984;
# DO NOT replace WWW-Authenticate header in response if authorization failed
# more_set_headers -s 401 'WWW-Authenticate: Other realm="App"';
# Handle redirects
proxy_redirect default;
}
你已经准备好了。您可以继续使用 pouchdb-authentication 或编写自己的登录处理程序。
原帖
很抱歉回答,但我不能发表评论(目前)。
我遇到了同样的问题,更糟糕的是,在 OS X 上,WWW-Authenticate 参数在每次重新启动 CouchDB 时都是小写的,因此不再被识别。因此,必须在每次重启后使用 Futon/Fauxton 或 API 进行设置。
您可以尝试使用下一个参数(请参阅http://docs.couchdb.org/en/1.6.1/api/server/authn.html)。原则上,您将身份验证请求发送到(angular2 中的示例):
// assuming you bootstrapped HTTP_PROVIDERS and injected Http
// configure headers
let headers: Headers = new Headers()
headers.append('Content-Type', 'application/json')
headers.append('Accept', 'application/json')
headers.append('Authorization', 'Basic ' + window.btoa(username + ':' + password))
// using the injected Http instance
this.http
// post to _session specifying next and the redirect
.post(
'http://localhost:5984/_session?next=/successfullyLoggedInPage'
, JSON.stringify({'name': username, 'password': password})
, {headers: headers}
)
.map((res: Response) => res.json())
.subscribe(
(res) => {
// successful auth
},
(err) => {
if (err.status === 401) // failed auth
}
)
在我的设置中,网络应用和 CouchDB 来自两个不同的来源。如果由于跨域限制而在 Chrome 中禁用网络安全性,我只能使其正常工作。我相信反向代理可以重写重定向响应,例如。 G。使用 nginx 的 proxy_redirect (http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect)。
我认为最好的解决方案是通过反向代理修改响应标头。对于 nginx,有一个名为 ngx_headers_more 的模块(参见https://github.com/openresty/headers-more-nginx-module#readme)应该能够做到这一点。可以检查 401 响应,然后将标头从 Authentication: Basic 修改为 Authentication: Other,因此禁用模式。原则上 Futon/Fauxton 应该仍然可以工作,不是吗?我还没有尝试过这种方法,但是你需要在 nginx 的 location 块中指定
more_set_headers -s 401 'WWW-Authenticate: Other realm="App"'
我希望有资格的人可以加他/她的两分钱。