Basic Auth

实现 HTTP Basic Authentication

配置参数

realm string
Default:
"Basic Authentication"
users record
errorMessage string
Default:
"401 Unauthorized"

Description

这个中间件用于通过 basic access authentication 保护网站访问。

Installation

这个中间件必须与 Lume 的 HTTP 服务器 一起使用。为了在生产环境中使用它,你需要一个运行 Deno 服务器的主机,例如 Deno Deploy

创建一个入口点文件(例如, serve.ts ),包含以下代码:

import Server from "lume/core/server.ts";
import basicAuth from "lume/middlewares/basic_auth.ts";

const server = new Server();

server.use(basicAuth({
  users: {
    user1: "password1",
    user2: "password2",
  },
}));

server.start();

Important

为了安全起见,不建议将用户名和密码硬编码在你的代码中。 推荐使用环境变量,例如:

const user = Deno.env.get("AUTH_USERNAME");
const password = Deno.env.get("AUTH_PASSWORD");

server.use(basicAuth({
  users: {
    [user]: password,
  },
}));

Local development

你可以配置 Lume 的开发服务器,在 _config.ts 文件中使用这个中间件:

import lume from "lume/mod.ts";
import basicAuth from "lume/middlewares/basic_auth.ts";

const site = lume({
  server: {
    middlewares: [
      basicAuth({
        users: { demo: "1234" },
      }),
    ],
  },
});

export default site;