Discourse 零代码集成 SSO 教程
# Discourse 是什么?
Discourse 是 Stack Overflow 的联合创始人 Jeff Atwood 推出的一个新的开源论坛项目,其目的是为了改变十年未变的论坛软件,摒弃了传统论坛的话题讨论形式、拥有自学习系统、全 Web 应用同时适用于桌面和移动终端。
# Authing 是什么?
[Authing](https://www.authing.cn/?utm_source=v0&utm_campaign=blog) 是国内首款以开发者为中心的全场景身份云产品,集成了所有主流身份认证协议,为企业和开发者提供完善安全的用户认证和访问管理服务。
以「API First」作为产品基石,把身份领域所有常用功能都进行了模块化的封装,通过全场景编程语言 SDK 将所有能力 API 化提供给开发者。同时,用户可以灵活的使用 Authing 开放的 RESTful APIs 进行功能拓展,满足不同企业不同业务场景下的身份管理需求。
## 传统系统 SSO 单点登录改造
首先,需要对原有系统的用户体系进行改造,使其能够适应通用的用户认证标准协议(如 OAuth)。如果系统体量较大,甚至还需要考虑将原有业务中的用户体系抽出,专门做成单点登录的用户中心。需要进行设计、编码、测试、升级、扩容等一系列复杂的开发运维操作,才能够实现。
该方式耗时间,耗精力,耗成本,风险大(需要在业务领域之外不断去踩坑)。
## Authing SSO 集成
如果您的应用是基于 Authing 提供的身份系统进行开发,那么恭喜你,可以使用较少的代码(或者配置)即可轻松几步,快速完成单点登录的集成。
该方式低成本,无须额外设计和开发,采用标准协议,轻松打通。
# Discourse 安装
参考资料:
- Discourse 官方 Docker 仓库: [https://github.com/discourse/discourse_docker](https://github.com/discourse/discourse_docker)
- 安装指南文档: [https://github.com/discourse/discourse/blob/main/docs/INSTALL-cloud.md](https://github.com/discourse/discourse/blob/main/docs/INSTALL-cloud.md)
建议将 Discourse Docker 仓库 Fork,并在本地先进行开发调试确认无误后再进行产品环境的安装。下面是简单的安装步骤说明及配置中常见问题的 FAQ。
注意:请在 Linux 服务器或者 macOS 下进行安装(Windows 需要自行摸索一下)。
## 拉取 Discourse Docker 仓库
```bash
git clone https://github.com/discourse/discourse_docker.git /var/discourse
cd /var/discourse
```
使用自动化配置脚本:
```bash
./discourse-setup
```
非 `root` 用户的话,需要在前面加 `sudo` 运行。根据提示一步一步输入配置项,完成安装配置。默认的配置在本地运行的时候大概率会运行不起来。
打开 `containers/app.yml` 配置文件进行配置调整,如果手动配置,也可以执行:
```bash
cp samples/standalone.yml containers/app.yml
```
复制一个示例模板作为开始。
## 应用配置
在开始配置之前,先确保域名已经绑定到服务器上(DNS 中的 A 记录绑定),或者本地修改 `/etc/hosts` 文件(不要使用 example.com 或者需要强制 https 的域名后缀作为本地开发调试,如: `.app`、`.dev`等)。
环境变量设置参考:
```yaml
env:
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LANGUAGE: en_US.UTF-8
EMBER_CLI_PROD_ASSETS: 1
# DISCOURSE_DEFAULT_LOCALE: en
## How many concurrent web requests are supported? Depends on memory and CPU cores.
## will be set automatically by bootstrap based on detected CPUs, or you can override
#UNICORN_WORKERS: 3
## TODO: The domain name this Discourse instance will respond to
## Required. Discourse will not work with a bare IP number.
DISCOURSE_HOSTNAME: 'discourse.local'
## Uncomment if you want the container to be started with the same
## hostname (-h option) as specified above (default "$hostname-$config")
# DOCKER_USE_HOSTNAME: true
## TODO: List of comma delimited emails that will be made admin and developer
## on initial signup example '[email protected],[email protected]'
DISCOURSE_DEVELOPER_EMAILS: '[email protected]'
## TODO: The SMTP mail server used to validate new accounts and send notifications
# SMTP ADDRESS, username, and password are required
# WARNING the char '#' in SMTP password can cause problems!
DISCOURSE_SMTP_ADDRESS: smtp.mail.qq.com
DISCOURSE_SMTP_PORT: 25
DISCOURSE_SMTP_USER_NAME: [email protected]
DISCOURSE_SMTP_PASSWORD: 'password'
DISCOURSE_SMTP_ENABLE_START_TLS: fales # (optional, default true)
DISCOURSE_SMTP_AUTHENTICATION: plain
DISCOURSE_SMTP_OPENSSL_VERIFY_MODE: none
DISCOURSE_NOTIFICATION_EMAIL: [email protected]
## If you added the Lets Encrypt template, uncomment below to get a free SSL certificate
#LETSENCRYPT_ACCOUNT_EMAIL: [email protected]
## The http or https CDN address for this Discourse instance (configured to pull)
## see https://meta.discourse.org/t/14857 for details
#DISCOURSE_CDN_URL: https://discourse-cdn.example.com
## The maxmind geolocation IP address key for IP address lookup
## see https://meta.discourse.org/t/-/137387/23 for details
#DISCOURSE_MAXMIND_LICENSE_KEY: 1234567890123456
```
简单说明:
- `UNICORN_WORKERS`:根据 CPU 数量决定 Worker 数量或者手动配置
- `DISCOURSE_HOSTNAME`:正确配置域名
- `DISCOURSE_SMTP_XXX`:正确配置 SMTP 邮件发送服务,不然会收不到验证码和通知邮件
插件配置:
```yaml
## Plugins go here
## see https://meta.discourse.org/t/19157 for details
hooks:
after_code:
- exec:
cd: $home/plugins
cmd:
- git clone https://github.com/discourse/docker_manager.git
- git clone https://github.com/discourse/discourse-oauth2-basic.git
- git clone https://github.com/angelim/discourse_user_auto_activation.git
```
这三个插件的作用:
- Docker 管理,自带默认
- OAuth 2 集成,用于与 Authing SSO 连接实现单点登录
- 自动激活(邮箱)插件,可选
## 启动论坛服务
提醒:先配置好 SSL 证书再启动服务(参考 FAQ SSL 证书配置,或者使用 Let's Encrypt 签发证书)。
配置完成后制作镜像:
```bash
./launcher bootstrap app
./launcher start app
```
如果已经初始化过,可以通过重新制作镜像的方式:
```bash
./launcher rebuild app
```
过程漫长,耐心等待。下面整理了我在调试过程中遇到的一些问题和一些提示。
## FAQ
### 域名配置
主要修改模板:
```yaml
templates:
- 'templates/postgres.template.yml'
- 'templates/redis.template.yml'
- 'templates/web.template.yml'
- 'templates/web.ratelimited.template.yml'
## Uncomment these two lines if you wish to add Lets Encrypt (https)
- 'templates/web.ssl.template.yml'
#- "templates/web.letsencrypt.ssl.template.yml"
```
- 如果需要 SSL 支持, `"templates/web.ssl.template.yml" ` 这个模板文件需要引入。
- 如果需要免费的域名证书更新, `"templates/web.letsencrypt.ssl.template.yml"` 这个模板需要引入。
同时根据需要,将端口开放出来:
```yaml
## which TCP/IP ports should this container expose?
## If you want Discourse to share a port with another webserver like Apache or nginx,
## see https://meta.discourse.org/t/17247 for details
expose:
- '80:80' # http
- '443:443' # https
```
### SSL 证书配置
首先确认一下文件目录的映射是否正确:
```yaml
## The Docker container is stateless; all data is stored in /shared
volumes:
- volume:
host: /var/discourse/shared/standalone
guest: /shared
- volume:
host: /var/discourse/log/var-log
guest: /var/log
```
类似这样的目录映射,将证书文件放置到对应的 ssl 目录下,检查这两个文件路径是否正确:
```yaml
/var/discourse/shared/standalone/ssl/ssl.crt
/var/discourse/shared/standalone/ssl/ssl.key
```
自签证书参考:
```bash
openssl genrsa -aes256 -passout pass:gsahdg -out ssl.pass.key 4096
openssl rsa -passin pass:gsahdg -in ssl.pass.key -out ssl.key
openssl req -new -key ssl.key -out ssl.csr
# You are about to be asked to enter information that will be incorporated
# into your certificate request.
# What you are about to enter is what is called a Distinguished Name or a DN.
# There are quite a few fields but you can leave some blank
# For some fields there will be a default value,
# If you enter '.', the field will be left blank.
#