需求
如題,Swagger-UI頁面需要加權限驗證攔截,但又不想搞太麻煩,於事就決定用Nginx的指定URL權限驗證,來實現攔截swagger-ui.html
實現
以Centos爲例
# 安裝工具
yum -y install httpd
# 創建密碼
htpasswd -c /etc/nginx/passwd admin
# 然後根據提示輸入兩次密碼
New password:
Re-type new password:
# 結束後在/etc/nginx/目錄下就多了一個passwd文件
修改配置文件
/etc/nginx/nginx.conf
# 修改Nginx配置 加上這一段即可
location /swagger-ui.html {
auth_basic "There place don't have three hundred silver in here.";
auth_basic_user_file /etc/nginx/passwd;
# 下面這段根據自己情況配置
proxy_pass http://localhost:8080/service/swagger-ui.html;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_connect_timeout 30;
proxy_read_timeout 30;
proxy_send_timeout 30;
access_log off;
break;
}
# 要是覺得還不保險,再加一個IP限制
location /swagger-ui.html {
# 代表IP4位全部對上才允許訪問
allow 111.111.111.111;
# 代表IP前兩位能對上就允許訪問(用於某些運營商公網IP後兩位經常變更)
allow 111.111.0.0/16;
deny all;
......
}
END
本文同時也會發布在我的個人博客
https://zzzmh.cn/single?id=71