Htpasswd
htpasswd是Apache的Web服务器内置的工具,用于创建和更新储存用户名和用户基本认证的密码文件
Install
1
| yum -y install httpd-tools
|
Option
1 2 3 4 5 6 7 8 9
| -c: 创建一个新的密码文件 -b: 在命令行中一并输入用户名和密码而不是根据提示输入密码 -D: 删除指定的用户 -n: 不更新密码文件,只将加密后的用户名密码输出到屏幕上 -p: 不对密码进行加密,采用明文的方式 -m: 采用MD5算法对密码进行加密(默认的加密方式) -d: 采用CRYPT算法对密码进行加密 -s: 采用SHA算法对密码进行加密 -B: 采用bcrypt算法对密码进行加密(非常安全)
|
Use
1 2 3
| 命令 指定生成密码文件 密码文件 用户名 htpasswd -c auth putianhui htpasswd -c auth test
|
kubernetes - 为ingress添加basic-auth认证
创建用户密码文件
1 2
| htpasswd -c auth test htpasswd auth putianhui
|
查看用户密码文件
1 2 3
| cat auth test:$apr1$.MVr5Acc$/O7o5pLEi3totJo.X4KnD/ putianhui:$apr1$uKiSFrrL$i2wlD8lPl1uCl8OluG069/
|
创建secret资源存储用户密码
1 2 3 4
| example: kubectl -n <namespace> create secret generic basic-auth --from-file=auth
kubectl -n test create secret generic basic-auth --from-file=auth
|
创建ingress资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| apiVersion: extensions/v1beta1 kind: Ingress metadata: name: springboot namespace: test annotations: nginx.ingress.kubernetes.io/auth-type: basic nginx.ingress.kubernetes.io/auth-secret: basic-auth spec: rules: - host: test.jd.cn http: paths: - path: / backend: serviceName: springboot-svc servicePort: 9090
|
终端验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| curl -I http: HTTP/1.1 401 Unauthorized Server: nginx/1.16.1 Date: Thu, 12 Mar 2021 12:16:35 GMT Content-Type: text/plain Content-Length: 17 Connection: keep-alive Www-Authenticate: Basic realm="traefik"
curl -I http: HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Thu, 12 Mar 2021 12:22:12 GMT Content-Type: text/html Content-Length: 22031 Connection: keep-alive Accept-Ranges: bytes Etag: "5b4e2a7c-560f" Last-Modified: Tue, 17 Jul 2018 17:42:20 GMT
|