【问题标题】:Prevent Authentication popup 401 with CouchDB PouchDB使用 CouchDB PouchDB 防止身份验证弹出 401
【发布时间】:2015-12-16 16:46:06
【问题描述】:

对于 JavaScript Web 应用程序(基于 AngularJS),我正在使用 PouchDB 在我的服务器上复制 CouchDB 数据库。 PouchDB 中的身份验证与pouchdb-authentication 配合得很好。我想通过 html/js 登录屏幕来管理它。

但是,如果用户输入了错误的凭据,我会从 CouchDB 服务器收到 401 Unauthorized,这会导致浏览器弹出窗口询问凭据。

如何防止这个丑陋的身份验证弹出窗口,只处理我的 javascript 中的所有内容?!

【问题讨论】:

  • 您介意将其中一个答案标记为已接受的解决方案吗?

标签: javascript authentication couchdb pouchdb


【解决方案1】:

我终于找到了解决办法:

编辑 CouchDB 配置 local.ini 并更改响应中发送的 HTTP 标头:

WWW-Authenticate = Other realm="app"

原来是这样

WWW-Authenticate = Basic realm="administrator"

或者如果它被评论了,那无论如何都是发送出去的。 WWW-Authenticate = Basic 显然会导致浏览器通过显示其模式来处理(失败的)身份验证。将Basic 更改为其他任何内容都会使浏览器忽略它,您可以自己处理登录。

【讨论】:

  • 虽然这不会导致模式出现,但您似乎无法再登录fauxton。
  • 是的,你是对的 - 我在使用蒲团进行这些更改时也遇到了问题。在我自己的应用程序上登录后,我也获得了 Futon 的身份验证
【解决方案2】:

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"'

我希望有资格的人可以加他/她的两分钱。

【讨论】:

    【解决方案3】:

    我或多或少遇到了同样的问题。虽然我正在使用带有 CouchDB 的“代理”身份验证处理程序的反向代理。当用户尝试做他不允许做的事情(在他不是管理员时创建数据库)时,CouchDB 返回 401 响应,触发 HTTP 基本身份验证。不太好,因为反向代理处理基于 X509 客户端证书的身份验证。我实际上认为 CouchDB 应该返回 403 响应,但这是一个不同的讨论。

    为了解决这个问题,我使用了你的答案,但我并不急于使用外部 Nginx 模块 (ngx_headers_more),实际上我不太喜欢将标头更改为“废话”值。相反,使用 Nginx 中现有的代理模块,您可以一起删除 WWW-Authenticate:

    proxy_pass              http://${COUCHDB_HOSTNAME}:${COUCHDB_PORT};
    proxy_hide_header       WWW-Authenticate;
    

    似乎工作正常。有关其文档,请参阅 https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_hide_header。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-25
      • 2015-07-13
      • 2019-01-02
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      相关资源
      最近更新 更多