Nginx 指定URL或静态页面 实现用户登录 用户名密码验证
2019-07-06
阅读 {{counts.readCount}}
评论 {{counts.commentCount}}
需求
如题,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
评论区空空如也,赶紧添加一条评论吧
评论 {{counts.commentCount}}
{{comment.name}}
{{comment.os}}
{{comment.browser}}
{{comment.reply.name}}
{{comment.reply.os}}
{{comment.reply.browser}}