Casdoor 是一个开源的统一身份认证平台,定位类似于“通行证中心”。通过标准化的协议和接口,把分散在不同系统中的用户认证集中起来管理,让登录这件事变得更简单。
——清羽飞扬

下载、配置文件与安装

这个是它的官方文档:https://www.casdoor.org/zh/docs/overview (目前汉化还不完全,而且还有很多不准确的机翻)
可以看到它提供了三种安装方式:
1.二进制安装包,从Github Release下载:https://github.com/casdoor/casdoor/releases
2.Docker容器镜像:https://hub.docker.com/r/casbin/casdoor
3.K8s Helm:https://www.casdoor.org/zh/docs/basic/try-with-helm

拉取镜像

本文将采用Docker容器的方式来安装,配合宝塔面板。依旧是1panel镜像。
下载镜像:

1
docker pull docker.1panel.live/casbin/casdoor

下载完成后保留备用。

数据目录设置与数据库配置

下一步,创建所需目录(在宝塔“终端”中运行):

1
2
3
4
mkdir -p /www/wwwroot/casdoor/config
mkdir -p /www/wwwroot/casdoor/uploads
chmod -R 755 /www/wwwroot/casdoor
chown -R 1000:1000 /www/wwwroot/casdoor # 重要!Docker容器使用uid 1000

然后在/www/wwwroot/casdoor/config/目录下创建app.conf文件
示例配置文件:
https://github.com/casdoor/casdoor/blob/master/conf/app.conf
把它放在/www/wwwroot/casdoor/config/目录下。
官方示例配置文件的完整内容如下(我加了注释,建议去官方仓库复制然后从这里对照着改)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
appname = casdoor
httpport = 8000 #对外访问的端口号
runmode = dev #改成prod以启用生产环境
copyrequestbody = true
driverName = mysql #要使用的数据库
dataSourceName = root:123456@tcp(localhost:3306)/ #目标数据库需要的凭据
dbName = casdoor #数据库名称
tableNamePrefix =
showSql = false
redisEndpoint =
defaultStorageProvider =
isCloudIntranet = false
authState = "casdoor"
socks5Proxy = "127.0.0.1:10808"
verificationCodeTimeout = 10
initScore = 0
logPostOnly = true
isUsernameLowered = false
origin = #后端访问域名,这个很关键。出现Invalid origin时配置下这个
originFrontend = #前端页面的访问地址,前后端都通过一个域名进来的话和上面配置相同的内容即可
staticBaseUrl = "https://cdn.casbin.org"
isDemoMode = false
batchSize = 100
enableErrorMask = false
enableGzip = true
inactiveTimeoutMinutes =
ldapServerPort = 389
ldapsCertId = ""
ldapsServerPort = 636
radiusServerPort = 1812
radiusDefaultOrganization = "built-in"
radiusSecret = "secret"
quota = {"organization": -1, "user": -1, "application": -1, "provider": -1}
logConfig = {"adapter":"file", "filename": "logs/casdoor.log", "maxdays":99999, "perm":"0770"}
initDataNewOnly = false
initDataFile = "./init_data.json"
frontendBaseDir = "../cc_0"

根据实际情况修改以上配置
参见配置内容描述:https://www.casdoor.org/zh/docs/basic/configuration/#backend-configuration-appconf
最关键的就是数据库的部分了,Casdoor需要一个数据库来存放验证用户等数据

Casdoor 支持MySQL、MSSQL、SQLite3和PostgreSQL。 默认情况下,Casto使用MySQL。
对于不同类型的数据库,driverNamedataSourceName各不相同,根据官方文档的说明来配置
MySQL
Casdoor将会把users,nodes和topics信息存储在一个名为casdoor的MySQL数据库中。 如果数据库不存在,则需手动创建。 可以在以下位置指定数据库连接字符串

1
2
3
driverName = mysql
dataSourceName = root:123456@tcp(localhost:3306)/
dbName = casdoor

PostgreSQL
在运行 Casdoor 之前,您需要手动为 PostgreSQL 准备一个数据库,因为当使用 xorm 连接 Postgres 时,Casdoor 要求必须选择一个具体的数据库。
假设你已经准备好了一个名为casdoor的数据库,你应该像这样指定app.conf

1
2
3
driverName = postgres
dataSourceName = user=postgres password=postgres host=localhost port=5432 sslmode=disable dbname=casdoor
dbName = casdoor

SQLite3
要配置SQLite3,您应该像这样指定app.conf:

1
2
3
driverName = sqlite
dataSourceName = file:casdoor.db?cache=shared
dbName = casdoor

参考:https://www.casdoor.org/zh/docs/basic/server-installation#%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E5%BA%93

因为我们使用的是宝塔面板,而且宝塔面板自带一个MySQL数据库,并且备份和维护都很好管理。所以这里就直接用MySQL了。或者你也可以连接远程数据库。
操作流程
进入宝塔面板 → 数据库添加数据库
数据库名:casdoor
用户名:casdoor_user(自定义)
密码:生成强密码(建议16位以上,防止暴力破解)
访问权限:本地服务器(127.0.0.1)
(建议同时开启自带备份,防止数据损坏)

最终配置文件相关字段的内容如下

1
2
3
driverName = mysql
dataSourceName = casdoor_user:你的强密码@tcp(127.0.0.1:3306)/
dbName = casdoor

设置完成后直接保存文件

注意:如果你是后创建的app.conf文件,别忘了再运行一次这个命令

1
chown -R 1000:1000 /www/wwwroot/casdoor

把这个新建文件的所有者设置也包含进去

运行Casdoor

最后我们终于可以开始启动容器了:

1
2
3
4
5
6
docker run -d --name casdoor \
--network host \
-v /www/wwwroot/casdoor/config:/conf \
-v /www/wwwroot/casdoor/uploads:/uploads \
--restart unless-stopped \
docker.1panel.live/casbin/casdoor:latest

注意:别忘记去防火墙放行端口!默认为8000端口,可在配置文件中修改

如果你运行以后它就自动暂停并且再也起不来了的话,重点检查下挂载的文件路径是否有问题,以及数据库是否在容器内访问的到。另外就是文件所有者是否是1000,是否有可读可写权限啥的。
最终的状态一直是运行中的话,那么恭喜你。成功绕过一堆坑把项目跑起来了。

如果你看不到容器日志的话,将近3天改为全部即可。这在出现异常时尤为有用

去 服务器公网IP 或 绑定的域名 下的8000端口 进入Casdoor的后台页面,完成下一步设置

后台配置

进入后台,使用默认的管理员账号登录后台。用户名为admin,密码123

数据安全提醒:默认的管理员账号极其不安全,强烈建议第一次登录时就从设置里把它改掉,换成强密码。

安装向导

第一次进入会有一个向导,里面有三页内容。
Welcome to casdoor
You can learn more about the use of CasDoor at https://casdoor.org/.
1/3
(后面就不说了反正是告诉你面板里面都是什么内容)
这里是没有汉化的,我截取里面有用的部分尝试让AI翻译下:
Organization List
组织是 Casdoor 的基本单位,用于管理用户和应用程序。如果用户登录到一个组织,那么他可以访问该组织所属的所有应用程序,而无需再次登录。
Organization is the basic unit of Casdoor, which manages users and applications. If a user signed in to an organization, then he can access all applications belonging to the organization without signing in again.
Group List
在群组列表页面中,您可以看到组织中的所有群组。
In the groups list pages, you can see all the groups in organizations.
User List
作为身份验证平台,Casdoor能够管理用户。
As an authentication platform, Casdoor is able to manage users.
1/2
Import users
您可以通过上传包含用户信息的XLSX文件来添加新用户或更新现有的Casdoor用户。
You can add new users or update existing Casdoor users by uploading a XLSX file of user information.
2/2
Application List
如果您想使用Casdoor为您的Web应用程序提供登录服务,您可以将它们作为Casdoor应用程序添加。用户可以访问其组织中的所有应用程序,而无需重复登录。
If you want to use Casdoor to provide login service for your web Web APPs, you can add them as Casdoor applications. Users can access all applications in their organizations without login twice.
Provider List
我们有6种类型的提供者:OAuth提供者、短信提供者、邮件提供者、存储提供者、支付提供者、验证码提供者。
We have 6 kinds of providers:OAuth providers、SMS Providers、Email Providers、Storage Providers、Payment Provider、Captcha Provider.
1/2
Provider Add
您必须先将提供者添加到应用程序中,然后才能在您的应用程序中使用该提供者。
You must add the provider to application, then you can use the provider in your application
2/2
Resource List
您可以在Casdoor中上传资源。在上传资源之前,您需要先配置一个存储提供者。请参阅存储提供者相关文档。
You can upload resources in casdoor. Before upload resources, you need to configure a storage provider. Please see Storage Provider.
1/2
Upload Resource
用户可以将文件和图片等资源上传到先前配置的云存储中。
Users can upload resources such as files and images to the previously configured cloud storage.
2/2
Role List
每个用户可能拥有多个角色。您可以在用户的个人资料中查看用户的角色。
Each user may have multiple roles. You can see the user’s roles on the user’s profile.
Permission List
与单个Casdoor组织关联的所有用户都会在该组织的应用程序之间共享,因此这些用户可以访问这些应用程序。有时您可能希望限制用户对特定应用程序的访问权限,或者限制其对某个应用程序中特定资源的访问权限。在这种情况下,您可以使用由Casbin实现的权限(Permission)功能。
All users associated with a single Casdoor organization are shared between the organization’s applications and therefore have access to the applications. Sometimes you may want to restrict users’ access to certain applications, or certain resources in a certain application. In this case, you can use Permission implemented by Casbin.
1/3
Permission Add
在Casdoor Web界面中,您可以在模型配置项中为您的组织添加一个模型,并在权限配置项中为您的组织添加一个策略。
In the Casdoor Web UI, you can add a Model for your organization in the Model configuration item, and a Policy for your organization in the Permission configuration item.
2/3
Permission Upload
通过Casbin在线编辑器,您可以获得适合您使用场景的Model和Policy文件。您可以通过Casdoor Web界面轻松地将Model文件导入到Casdoor中,供内置的Casbin使用。
With Casbin Online Editor, you can get Model and Policy files suitable for your usage scenarios. You can easily import the Model file into Casdoor through the Casdoor Web UI for use by the built-in Casbin.
3/3
Model List
模型定义了您的权限策略结构,以及请求如何匹配这些权限策略及其效果。然后您可以在权限中使用该模型。
Model defines your permission policy structure, and how requests should match these permission policies and their effects. Then you can user model in Permission.
Adapter List
Casdoor支持通过用户界面连接适配器并管理策略规则。在Casbin中,策略存储是通过适配器实现的(也称为Casbin的中间件)。Casbin用户可以使用适配器从存储中加载策略规则,或者将策略规则保存到存储中。
Casdoor supports using the UI to connect the adapter and manage the policy rules. In Casbin, the policy storage is implemented as an adapter (aka middleware for Casbin). A Casbin user can use an adapter to load policy rules from a storage, or save policy rules to it.
Enforcer List
除了用于请求执行权限控制的API接口外,Casdoor还提供其他帮助外部应用程序获取权限策略信息的接口,这些接口也在此列出。
In addition to the API interface for requesting enforcement of permission control, Casdoor also provides other interfaces that help external applications obtain permission policy information, which is also listed here.
Token List
Casdoor基于OAuth构建。令牌即用户的OAuth令牌,您可以在此列表中获取访问令牌。
Casdoor is based on OAuth. Tokens are users’ OAuth token.You can get access token in this list.
Session List
您可以在此列表中获取会话ID。
You can get Session ID in this list.
Session List
您可以添加您想要销售的产品(或服务)。接下来将告诉您如何添加产品。
You can add the product (or service) you want to sell. The following will tell you how to add a product
Payment List
支付成功后,您可以在”支付”中查看产品的交易信息,例如组织、用户、购买时间、产品名称等。
After the payment is successful, you can see the transaction information of the products in Payment, such as organization, user, purchase time, product name, etc.
Plan List
计划(Plan)描述了应用程序功能的列表,每个功能都有自己的名称和价格。计划的功能依赖于Casdoor角色及其权限集。这使得可以独立于命名和价格来描述计划的功能。例如:计划的价格可能会根据国家或日期而有所不同。
Plan describe list of application’s features with own name and price. Plan features depends on Casdoor role with set of permissions.That allow to describe plan’s features independ on naming and price. For example: plan may has diffrent prices depends on county or date.
Price List
Casdoor可以通过计划、定价和订阅用作订阅管理系统。
Casdoor can be used as subscription management system via plan, pricing and subscription.
Subscription List
订阅功能有助于管理用户选择的计划,从而轻松控制对应用程序功能的访问权限。
Subscription helps to manage user’s selected plan that make easy to control application’s features access.

管理工具那栏没啥好说的,这里为了节省篇幅跳过了

Syncer List
Casdoor在用户表中存储用户。当您计划使用Casdoor作为认证平台时,无需担心将应用程序的用户数据迁移到Casdoor中。Casdoor提供了同步器(syncer)来帮助您快速将用户数据同步到Casdoor。
Casdoor stores users in user table. Don’t worry about migrating your application user data into Casdoor, when you plan to use Casdoor as an authentication platform. Casdoor provides syncer to quickly help you sync user data to Casdoor.
Webhook List
事件系统允许您构建集成,这些集成可以订阅 Casdoor 上的特定事件。当其中一个事件被触发时,我们将向配置的 URL 发送一个 POST JSON 有效载荷。应用程序解析该 JSON 有效载荷并执行相应的钩子函数。事件包括注册、登录、注销、更新用户等,这些都存储在记录的操作字段中。事件系统可用于根据用户操作更新外部事务。
Event systems allow you to build integrations, which subscribe to certain events on Casdoor. When one of those event is triggered, we’ll send a POST json payload to the configured URL. The application parsed the json payload and carry out the hooked function. Events consist of signup, login, logout, update users, which are stored in the action field of the record. Event systems can be used to update an external issue from users.

到这里就没了。

创建组织

不同组织之间的用户是相互独立的,要开始,我们需要创建一个组织。
目标:用户管理 -> 组织

数据安全提醒:不要直接使用自带的built-in组织,默认所有用户都是管理员。否则配置不当容易引发安全问题

默认情况下,你可以在 你的域名:8000/login/组织名 中使用特定组织来登录/注册!(注意不要用成“显示名称”了)
如果要隐藏端口号请自行反向代理

创建应用

光有组织是不行的,因为你还缺少这个组织使用到的验证方式。如果只有组织并且它没有绑定应用的话创建用户时会提示没有应用。
目标:身份认证 -> 应用

创建应用时,需要在Basic选项卡下的“组织”选项中指定绑定到的组织。
然后你可以在里面配置是否允许用户同时登录多个会话(是否顶号式,如果不允许将会类似QQ一样只能登录一个会话)以及登录超时次数等

在UI那一块,建议只保留你需要用户填的项目,避免无效字段在前端页面中展示。设置“组织选择模式”为选择时,在登录之前会让用户选择要登录到的目标组织。
Provider选项卡中你可以选择添加由其他平台提供的自定义登录方式。详见创建提供商

创建证书

你需要一个证书实现登录时的端到端加密。
目标:身份认证 -> 证书

公钥可以对外暴露,提供给其他应用验证签名。而私钥必须在服务器里不能泄露

创建提供商

提供商即提供登录验证服务的厂商,国内常见的有QQ登录B站(不对个人开放)、飞书等。
你可以设置多种不同的登录验证方式,例如OAuth、Email、Captcha、Web3等
目标:身份认证 -> 提供商

填写目标开放平台提供的密钥、ck等信息后保存即可。
每种验证方式需独立创建,一个应用可以绑定多个提供商。

关于提供商和不同提供商的接入教程:https://www.casdoor.org/zh/docs/provider/overview

需要注意的点

casdoor的所有回调地址均为你后台地址域名的/callback路径。例如你的casdoor公网域名是outh.domain.com,那么回调给casdoor的地址应该是https://outh.domain.com/callback