使用基本身份验证重定向到另一个网站(在节点中)

时间:2017-12-15 21:59:51

标签: node.js http https koa

如何使用基本身份验证(在节点中)重定向到其他网站

这是我的代码

const headers = {
  Authorization: "Basic " + 
  new Buffer(USER + ":" + PASS).toString("base64")
};

ctx.response.set(headers);
ctx.response.redirect(URL)

使用基本身份验证的第一个响应返回

Authorization →Basic QWRxXxXxYWRtaW4=
Connection →keep-alive
Content-Length →111
Content-Type →text/html; charset=utf-8
Date →Fri, 15 Dec 2017 21:49:57 GMT
Location →http://localhost:8080/edit/data/P0000013

以下重定向的GET请求不包含基本身份验证并再次重定向到登录页面。

# General
Request URL:http://localhost:8080/edit/data/P0000013
Request Method:GET
Status Code:302 Found
Remote Address:[::1]:8080
Referrer Policy:no-referrer-when-downgrade

# Request Header
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.9
Connection:keep-alive
Cookie:....
Host:localhost:8080
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36

2 个答案:

答案 0 :(得分:0)

您可以指定用户名&密码是URL的一部分:

ctx.redirect('http://username:password@example.com');

答案 1 :(得分:0)

  1. 无法重定向附加标头(包括基本身份验证),HTTP协议不支持。

  2. 但您可以将基本身份验证密钥/值对作为参数放在新网址中:

    发现HTTP / 1.x 302 位置:/ api?auth = asdf

  3. 或将其保存在Cookie中

    发现HTTP / 1.x 302 地点:/ api
    Set-Cookie:auth = asdf

相关问题