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

问题描述 投票:1回答:2

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

这是我的代码

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
node.js http https koa
2个回答
0
投票

您可以指定用户名和密码作为URL的一部分:

ctx.redirect('http://username:[email protected]');

0
投票
  1. 您无法使用附加标头重定向(包括基本身份验证),HTTP协议不支持它。
  2. 但是您可以将基本身份验证密钥/值对作为参数放在新URL中: 找到HTTP / 1.x 302 位置:/ api?auth = asdf
  3. 或者将其保存在cookie中 找到HTTP / 1.x 302 位置:/ api Set-Cookie:auth = asdf
© www.soinside.com 2019 - 2024. All rights reserved.