Cloudflare wokers 项目添加http密码


帐号:密码 通过BASE64加密,填到下面代码 const authorization 里面的 Basic 后面

admin:password BASE64加密为 YWRtaW46cGFzc3dvcmQ=

/** http basic auth **/
const authorization = "Basic YWRtaW46cGFzc3dvcmQ=";
/** http basic auth **/

var basicauth;

addEventListener('fetch', event => {
  console.log(event.request.headers.get("Authorization"));
  if (event.request.headers.get("Authorization") !== authorization) {
    return event.respondWith(new Response(
      null, {
        status: 401,
        statusText: "'Authentication required.'",
        body: "Unauthorized",
        headers: {
          "WWW-Authenticate": 'Basic realm="User Visible Realm"'
        }
      }
    ))
  }
  event.respondWith(handleRequest(event.request))
})

将代码插入到 workers 里面即可添加 basic auth 认证

声明:Blog on RackNerd|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - Cloudflare wokers 项目添加http密码


本博客托管在 RackNerd 的 VPS 上