前言
因为各个站点之间的登录是独立的,一个一个登录很麻烦,况且有一个单点登录系统很酷,于是我之前就很想搞一个。但是奈何这东西有点复杂,于是几番折腾之下我放弃了。
最近又看到博友在搞Casdoor,于是我重拾旧本,重新探索,终于在搜索引擎与ChatGPT的帮助之下部署完成。
环境
- 1Panel 面板
- Docker(基于1Panel面板的应用商店)
- OpenResty(反代服务,大坑!!!)
流程
1. 在1Panel 应用商店安装 Casdoor
Casdoor是一个支持 OAuth 2.0、OIDC、SAML 和 CAS 的 Web UI 优先的 IAM/SSO 平台。
2. 在1Panel 网站添加应用 Casdoor
即为反代本地8000端口,同时记得配置https证书并解析域名到服务器ip。
3. 在 Casdoor 上创建基本信息
初始账号为admin
,密码为123
,进入后台后按照下图进行配置。
4. 在 OpenList 上配置单点登录
单点登录平台
选择OIDC
,单点登录端点名称
为 Casdoor 的链接,末尾不带/
。
其他按照上图进行填写。
5. 测试登录:错误!
就在我满心欢喜地开始使用单点登录时,不幸的事情发生了:
{"code":400,"message":"404 Not Found: \u003chtml\u003e\r\n\u003chead\u003e\u003ctitle\u003e404 Not Found\u003c/title\u003e\u003c/head\u003e\r\n\u003cbody\u003e\r\n\u003ccenter\u003e\u003ch1\u003e404 Not Found\u003c/h1\u003e\u003c/center\u003e\r\n\u003chr\u003e\u003ccenter\u003eopenresty\u003c/center\u003e\r\n\u003c/body\u003e\r\n\u003c/html\u003e\r\n","data":null}
6. 寻找错误
于是我就把这个错误贴给了ChatGPT,它给我的答复是:
反代/网关配置问题:你前面有 Nginx/OpenResty 做反代,结果转发的时候找不到后端对应的 location,返回 404。
但是ChatGPT给出的解决办法并没有什么用......于是我开始控制变量来验证错误:
- 使用原端口原ip:可以
- 使用反代ip:不可以
- 使用https域名:不可以
- 使用http域名:不可以
因此可以得出,问题的确是在OpenResty的反代上。
7. 解决反代问题
我在搜索引擎上试图寻找遇到相同问题的文章,可惜没有。但是我知道了Casdoor的请求接口是:/.well-known/openid-configuration
,因此原来的404问题就出现在这个接口上。
我看到这个接口就有点发现问题所在了,中间的一个文件夹为/.well-known
,有次域名验证,文件上传到这个文件夹也是404。
于是我就把问题丢给了ChatGPT,它给了我解决方案:
# 新增 location,放在 ^~ /.well-known 之前
location = /.well-known/openid-configuration {
proxy_pass http://127.0.0.1:8000/.well-known/openid-configuration;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
把这个添加到OpenResty配置文件中,请求接口/.well-known/openid-configuration
就可以正常访问了。
再次访问OpenList单点登录,登录界面终于可以显示了,但是实际登录却又报错:
{"code":400,"message":"failed to verify signature: fetching keys oidc: get keys failed: 404 Not Found \u003chtml\u003e\r\n\u003chead\u003e\u003ctitle\u003e404 Not Found\u003c/title\u003e\u003c/head\u003e\r\n\u003cbody\u003e\r\n\u003ccenter\u003e\u003ch1\u003e404 Not Found\u003c/h1\u003e\u003c/center\u003e\r\n\u003chr\u003e\u003ccenter\u003eopenresty\u003c/center\u003e\r\n\u003c/body\u003e\r\n\u003c/html\u003e\r\n","data":null}
于是我又请教了一番ChatGPT,它又给出了解决方案:
和之前解决 /openid-configuration 类似,给 /.well-known/jwks 写单独 location:
location = /.well-known/jwks {
proxy_pass http://127.0.0.1:8000/.well-known/jwks;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
我再次添加到配置文件中,最后终于登录成功了!!!
总结:Casdoor 实现 OpenList 单点登录中,反代问题解决方案:
在OpenResty配置文件中加入:
# 新增 location,放在 ^~ /.well-known 之前
location = /.well-known/openid-configuration {
proxy_pass http://127.0.0.1:8000/.well-known/openid-configuration;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location = /.well-known/jwks {
proxy_pass http://127.0.0.1:8000/.well-known/jwks;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
参考资料
https://doc.oplist.org/guide/advanced/sso#_3-5-casdoor-1
https://www.whuanle.cn/archives/21725
https://blog.xioxix.com/archives/479
结尾
折腾了一个晚上总算是把单点登录解决了,幸好有众多大佬的文章与ChatGPT的帮助,最终问题才能解决。
但是使用AI解决问题并不是如文中所述一张一帆风顺,而是布满荆棘,解决问题的过程不乏我们的坚持与思考能力。
AI的出现不是替代人类,而是帮助人类;人类可以自我选择利用AI且不被AI替代。