BasicAuth (基本认证) 中间件

BasicAuth 中间件提供了 HTTP 的基本认证方式。

  • 对于有效的请求则继续执行后面的处理。
  • 对于无效的请求,返回”401 - Unauthorized”响应。

用法

  1. e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
  2. if username == "joe" && password == "secret" {
  3. return true, nil
  4. }
  5. return false, nil
  6. }))

自定义配置

用法

  1. e.Use(middleware.BasicAuthWithConfig(middleware.BasicAuthConfig{}))

配置

  1. BasicAuthConfig struct {
  2. // Skipper 定义了一个跳过中间间的函数
  3. Skipper Skipper
  4. // Validator 是一个用来验证 BasicAuth 是否合法的函数
  5. // Validator 是必须的.
  6. Validator BasicAuthValidator
  7. // Realm 是一个用来定义 BasicAuth 的 Realm 属性的字符串
  8. // 默认是 "Restricted"
  9. Realm string
  10. }

默认配置

  1. DefaultBasicAuthConfig = BasicAuthConfig{
  2. Skipper: defaultSkipper,
  3. }