OpenWrt 前置 OAuth2-Proxy 保护公网访问
OpenWrt 前置 OAuth2-Proxy 保护公网访问
通过 Nginx 反向代理开放 OpenWrt 登录页面时,若直接暴露在公网容易遭受爆破攻击。
本文将介绍如何使用 OAuth2-Proxy 作为前置认证代理,保护 OpenWrt 登录安全。
准备工作
- 编译 OpenWrt 时启用模块
在 Network → Web Servers/Proxies → nginx-ssl -- Configuration
中,勾选 Enable HTTP auth request module
。
- 安装 OAuth2-Proxy
以 x86 架构(amd64)为例,安装 OAuth2-Proxy 7.8.2:
1 | wget https://github.com/oauth2-proxy/oauth2-proxy/releases/download/v7.8.2/oauth2-proxy-v7.8.2.linux-amd64.tar.gz |
- x86 架构使用
amd64
,ARM 架构使用arm
。
一、创建 OAuth2-Proxy 配置文件
创建配置文件,例如:
1 | vim /etc/oauth2-proxy/oauth2-openwrt.cfg #文件路径及文件名可自定义,但相关配置文件中要修改一致 |
示例配置内容:
1 | http_address = "127.0.0.1:4180" |
- 生成
cookie_secret
:1
openssl rand -base64 32 | tr -- '+/' '-_'
client_id
和client_secret
可在 GitHub Developer Settings 创建 OAuth 应用获得。- OAuth 应用的 Authorization callback URL 需填写配置中的
redirect_url
。
二、创建 Nginx 配置文件
创建 Nginx 配置,例如:
1 | vim /etc/nginx/conf.d/oauth.conf |
配置内容:
1 | server { |
三、运行测试
- 重载 Nginx 配置
1 | nginx -s reload |
- 启动 OAuth2-Proxy
1 | oauth2-proxy --config=/etc/oauth2-proxy/oauth2-openwrt.cfg |
测试访问域名,若能正常认证并登录 OpenWrt,说明配置成功。
四、OpenWrt 上持久化 OAuth2-Proxy
为了在系统启动时自动运行 OAuth2-Proxy,可以将其加入 OpenWrt 的 init 脚本。
- 创建 init 启动脚本
1 | vim /etc/init.d/oauth2-proxy |
内容如下:
1 |
|
- 赋予执行权限
1 | chmod +x /etc/init.d/oauth2-proxy |
- 启用并启动服务
1 | /etc/init.d/oauth2-proxy enable |
完成后,OAuth2-Proxy 将随系统启动自动运行,并在异常退出时自动重启。
总结
通过以上配置,即可在 OpenWrt 上使用 OAuth2-Proxy + Nginx 实现安全认证防护,避免管理后台暴露在公网环境中被爆破入侵。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 爱折腾的菜鸟!
评论