基本认证
Basic Auth - 基本认证
原文:https://echo.labstack.com/docs/middleware/basic-auth
 基本认证中间件提供了HTTP基本认证。
- 对于有效的凭证,它调用下一个处理程序。
- 对于缺失或无效的凭证,它发送"401 - Unauthorized"的响应。
使用方法
| 1
2
3
4
5
6
7
8
9
 | e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
    // Be careful to use constant time comparison to prevent timing attacks
    // 请注意使用恒定时间比较来防止时序攻击
    if subtle.ConstantTimeCompare([]byte(username), []byte("joe")) == 1 &&
        subtle.ConstantTimeCompare([]byte(password), []byte("secret")) == 1 {
        return true, nil
    }
    return false, nil
}))
 | 
自定义配置
使用方法
| 1
 | e.Use(middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{}))
 | 
配置
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 | BasicAuthConfig struct {
  // Skipper 定义一个跳过中间件的函数。
  Skipper Skipper
  // Validator 是一个用于验证 BasicAuth 凭证的函数。
  // 必需。
  Validator BasicAuthValidator
  // Realm 是定义 BasicAuth 的域(realm)属性的字符串。
  // 默认值为"Restricted"。
  Realm string
}
 | 
默认配置
| 1
2
3
 | DefaultBasicAuthConfig = BasicAuthConfig{
    Skipper: DefaultSkipper,
}
 |