BasicAuth

原文: https://docs.gofiber.io/api/middleware/basicauth

BasicAuth net/http 到 Fiber - net/http 中间件到 Fiber

Basic Authentication middleware for Fiber that provides an HTTP basic authentication. It calls the next handler for valid credentials and 401 Unauthorized or a custom response for missing or invalid credentials.

​ Fiber 的基本身份验证中间件,提供 HTTP 基本身份验证。它为有效凭据调用下一个处理程序,并为缺失或无效凭据提供 401 未授权或自定义响应。

Signatures 签名

1
func New(config Config) fiber.Handler

Examples 示例

Import the middleware package that is part of the Fiber web framework

​ 导入 Fiber Web 框架的一部分中间件包

1
2
3
4
import (
  "github.com/gofiber/fiber/v2"
  "github.com/gofiber/fiber/v2/middleware/basicauth"
)

After you initiate your Fiber app, you can use the following possibilities:

​ 在启动 Fiber 应用后,您可以使用以下可能性:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Provide a minimal config
app.Use(basicauth.New(basicauth.Config{
    Users: map[string]string{
        "john":  "doe",
        "admin": "123456",
    },
}))

// Or extend your config for customization
app.Use(basicauth.New(basicauth.Config{
    Users: map[string]string{
        "john":  "doe",
        "admin": "123456",
    },
    Realm: "Forbidden",
    Authorizer: func(user, pass string) bool {
        if user == "john" && pass == "doe" {
            return true
        }
        if user == "admin" && pass == "123456" {
            return true
        }
        return false
    },
    Unauthorized: func(c *fiber.Ctx) error {
        return c.SendFile("./unauthorized.html")
    },
    ContextUsername: "_user",
    ContextPassword: "_pass",
}))

Config 配置

Property 属性Type 输入Description 说明Default 默认
Next 下一步func(*fiber.Ctx) boolNext defines a function to skip this middleware when returned true. 接下来定义一个函数,在返回 true 时跳过此中间件。nil
Users 用户map[string]stringUsers defines the allowed credentials. 用户定义允许的凭据。map[string]string{}
Realm 领域stringRealm is a string to define the realm attribute of BasicAuth. The realm identifies the system to authenticate against and can be used by clients to save credentials. 领域是一个字符串,用于定义 BasicAuth 的领域属性。领域标识要进行身份验证的系统,客户端可以使用它来保存凭据。"Restricted"
Authorizer 授权者func(string, string) boolAuthorizer defines a function to check the credentials. It will be called with a username and password and is expected to return true or false to indicate approval. 授权者定义一个函数来检查凭据。它将使用用户名和密码调用,并希望返回 true 或 false 来指示批准。nil
Unauthorized 未授权fiber.HandlerUnauthorized defines the response body for unauthorized responses. Unauthorized 定义未授权响应的响应主体。nil
ContextUsernameinterface{}ContextUsername is the key to store the username in Locals. ContextUsername 是将用户名存储在 Locals 中的键。"username"
ContextPasswordinterface{}ContextPassword is the key to store the password in Locals. ContextPassword 是将密码存储在 Locals 中的键。"password"

Default Config 默认配置

1
2
3
4
5
6
7
8
9
var ConfigDefault = Config{
    Next:            nil,
    Users:           map[string]string{},
    Realm:           "Restricted",
    Authorizer:      nil,
    Unauthorized:    nil,
    ContextUsername: "username",
    ContextPassword: "password",
}
最后修改 February 5, 2024: 更新 (f57b279)