BasicAuth
basicAuth 中间件只允许经过授权的用户访问服务。
配置示例¶
# Declaring the user list
http:
middlewares:
test-auth:
basicAuth:
users:
- "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"
- "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"# Declaring the user list
[http.middlewares]
[http.middlewares.test-auth.basicAuth]
users = [
"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
]# Declaring the user list
#
# Note: when used in docker-compose.yml all dollar signs in the hash need to be doubled for escaping.
# To create user:password pair, it's possible to use this command:
# echo $(htpasswd -nB user) | sed -e s/\\$/\\$\\$/g
#
# Also, note that dollar signs should NOT be doubled when not evaluated (e.g. Ansible docker_container module).
labels:
- "traefik.http.middlewares.test-auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"{
// ...
"Tags": [
"traefik.http.middlewares.test-auth.basicauth.users=test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"
]
}# Declaring the user list
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-auth
spec:
basicAuth:
secret: secretName配置选项¶
| 字段 | 说明 | 默认值 | 必填 |
|---|---|---|---|
users |
授权用户数组。每个用户必须使用 name:hashed-password 格式声明。(更多信息见此处) |
"" | 否 |
usersFile |
包含此中间件授权用户的外部文件路径。 文件内容是 name:hashed-password 列表。(更多信息见此处) |
"" | 否 |
realm |
允许自定义认证域。 | "traefik" | 否 |
headerField |
允许定义用于存储已认证用户的请求头字段。 | "" | 否 |
removeHeader |
允许在将请求转发给服务之前移除认证请求头。 | false | 否 |
密码格式¶
密码必须使用 MD5、SHA1 或 BCrypt 进行哈希处理。
使用 htpasswd 生成密码。
users 与 usersFile¶
- 如果同时提供
users和usersFile,两者将合并。users中的值优先于usersFile中的内容。 - 由于在 Kubernetes 中无法引用文件路径,因此 Kubernetes IngressRoute 不使用
users和usersFile字段,而应使用secret字段。
Kubernetes Secret 资源¶
users 选项支持 Kubernetes Secret。
Kubernetes kubernetes.io/basic-auth Secret 类型
Kubernetes 支持一种特殊的 kubernetes.io/basic-auth Secret 类型。
此 Secret 必须包含两个键:username 和 password。
请注意,这些键没有经过任何哈希或加密处理,因此安全性低于其他方式。 更多信息参阅 Kubernetes 基本认证 Secret 文档。
在生产环境使用 Traefik OSS?
如果你在工作中使用 Traefik,可以考虑为 Traefik OSS 增加企业级 API 网关能力或商业支持。
为 Traefik OSS 增加 API 网关能力快捷且平滑,无需推倒重来,现有配置也会完整保留。请通过这个简短视频了解实际效果。