Casdoor 是一個(gè)基于 OAuth 2.0 / OIDC 的中心化的單點(diǎn)登錄(SSO)身份驗(yàn)證平臺(tái)售睹,簡(jiǎn)單來(lái)說(shuō)幢痘,就是 Casdoor 可以幫你解決用戶管理的難題瀑踢,你無(wú)需開(kāi)發(fā)用戶登錄辨宠、注冊(cè)等與用戶鑒權(quán)相關(guān)的一系列功能悯辙,只需幾個(gè)步驟進(jìn)行簡(jiǎn)單配置琳省,與你的主應(yīng)用配合迎吵,便可完全托管你的用戶模塊,簡(jiǎn)單省心针贬,功能強(qiáng)大击费。
- 官網(wǎng): https://casdoor.org/
- 代碼: https://github.com/casdoor/casdoor
官網(wǎng)有 demo 體驗(yàn),及文檔桦他。本文是依照文檔「服務(wù)器安裝」「使用 Docker 運(yùn)行」于 Ubuntu 22 上的實(shí)踐記錄蔫巩。
安裝環(huán)境
安裝 Go
# 下載,依據(jù)系統(tǒng)選擇 Linux x86-64 的發(fā)布包
curl -O -L https://go.dev/dl/go1.20.4.linux-amd64.tar.gz
# 解壓
tar -xzvf go1.20.4.linux-amd64.tar.gz
# 重命名快压,帶上版本號(hào)
mv go go1.20.4.linux-amd64
# 軟鏈圆仔,便于配置或切版本
sudo ln -sfT `pwd`/go1.20.4.linux-amd64 /usr/local/go
# 配置,GOPATH 用自己的工作目錄
cat <<-EOF >> ~/.bashrc
# go
export GOROOT=/usr/local/go
export GOPATH=\$HOME/Codes/Go
export PATH=\$GOROOT/bin:\$GOPATH/bin:\$PATH
EOF
# 檢查
go version
go env
安裝 Node.js
# 下載蔫劣,選了當(dāng)前最新的 LTS 版本坪郭,可用
curl -O -L https://nodejs.org/dist/v18.16.0/node-v18.16.0-linux-x64.tar.xz
# 解壓
tar -xvf node-v18.16.0-linux-x64.tar.xz
# 軟鏈,便于配置或切版本
sudo ln -sfT `pwd`/node-v18.16.0-linux-x64 /usr/local/node
# 配置脉幢,GOPATH 用自己的工作目錄
cat <<-EOF >> ~/.bashrc
# node
export NODE_HOME=/usr/local/node
export PATH=\$NODE_HOME/bin:\$PATH
EOF
# 檢查
node -v
npm -v
安裝 Yarn
npm install yarn -g
# 檢查
yarn -v
安裝 MySQL
sudo apt update -y
# 安裝
sudo apt install mysql-server -y
# 檢查
systemctl status mysql.service
# 或啟動(dòng)
systemctl start mysql.service
配置 MySQL:
1 修改 root 用戶的密碼歪沃,
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword';
exit
不然,執(zhí)行 mysql_secure_installation
會(huì)遇到如下錯(cuò)誤:
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.
2 執(zhí)行配置腳本 mysql_secure_installation
把不安全的功能都給關(guān)了嫌松,
$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
3 恢復(fù) sudo mysql
登錄沪曙,
用客戶端的話,跳過(guò)這一步萎羔。
# 密碼登錄
mysql -u root -p
# 恢復(fù) sudo mysql 登錄
ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
# 退出
exit
安裝 MySQL 客戶端:
# 例如液走,用 MySQL Workbench
sudo snap install mysql-workbench-community
# 或者,用 phpMyAdmin 等
選擇 Local 實(shí)例外驱,用密碼登錄育灸,
創(chuàng)建一個(gè)名為 casdoor 的數(shù)據(jù)庫(kù),
另外昵宇,可創(chuàng)建一個(gè)名為 casdoor 的新用戶磅崭,專門(mén)管理該數(shù)據(jù)庫(kù)。
獲取源碼
進(jìn)工作目錄瓦哎,獲取 Casdoor 源碼砸喻,
# 獲取源碼
git clone --depth 1 https://github.com/casdoor/casdoor.git
配置
配置位于 casdoor/conf/app.conf
,
appname = casdoor
httpport = 8000
runmode = dev
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 = 2000
logPostOnly = true
origin =
staticBaseUrl = "https://cdn.casbin.org"
isDemoMode = false
batchSize = 100
ldapServerPort = 389
languages = en,zh,es,fr,de,id,ja,ko,ru,vi
quota = {"organization": -1, "user": -1, "application": -1, "provider": -1}
目前先只配置數(shù)據(jù)庫(kù)字段 driverName
dataSourceName
dbName
蒋譬。更多字段說(shuō)明割岛,見(jiàn)官方文檔「服務(wù)器安裝 / 通過(guò)-ini-文件配置」。
運(yùn)行
開(kāi)發(fā)模式
運(yùn)行后端:
cd casdoor/
go run main.go
如果發(fā)生錯(cuò)誤 checksum mismatch
犯助,可執(zhí)行:
go clean -modcache
rm go.sum
go mod tidy
# 還不行癣漆,切個(gè)代理,再試一次
# 可能代理緩存不一致剂买;可寫(xiě)進(jìn) ~/.bashrc
export GOPROXY="https://goproxy.cn,direct"
運(yùn)行前端:
cd casdoor/web
yarn install
yarn start
訪問(wèn) http://localhost:7001/惠爽,用戶 admin 密碼 123 登錄癌蓖,
生產(chǎn)模式
運(yùn)行后端:
cd casdoor/
go build
./casdoor
運(yùn)行前端:
cd casdoor/web
yarn install
yarn build
容器運(yùn)行
Docker 準(zhǔn)備
Install Docker Desktop on Ubuntu,
$ docker -v
Docker version 23.0.6, build ef23cbc
$ docker compose version
Docker Compose version v2.17.3
Docker 運(yùn)行
Casdoor 可以使用 docker-compose 運(yùn)行婚肆,它帶有獨(dú)立的數(shù)據(jù)庫(kù)租副,
cd casdoor/
docker compose up
可以如下修改,用本地已有的數(shù)據(jù)庫(kù)较性,
- 編輯
docker-compose.yml
- 刪掉
services/casdoor
下用僧,-
entrypoint
里的--createDatabase=true
參數(shù) -
depends_on
里的db
依賴
-
- 刪掉
services/db
的所有配置
- 刪掉
version: '3.1'
services:
casdoor:
restart: always
build:
context: ./
dockerfile: Dockerfile
target: STANDARD
entrypoint: /bin/sh -c './server'
ports:
- "8000:8000"
volumes:
- ./conf:/conf/
- 編輯
Dockerfile
刪掉ENTRYPOINT ["/server"]
之后的 db 內(nèi)容- 遇到
go build
提示版本問(wèn)題,可修改FROM golang:1.17.5 AS BACK
升下版本赞咙,如1.20.4
- 遇到
go test
不過(guò)责循,- 若下載問(wèn)題,可命令前加
export GOPROXY="https://goproxy.cn,direct" &&
用代理 - 若
TestGetVersionInfo
Fail人弓,可git pull --unshallow
拉取更多 commits 即可
- 若下載問(wèn)題,可命令前加
- 遇到 apk 安裝問(wèn)題沼死,可以注掉
RUN sed -i 's/https/http/' /etc/apk/repositories
- 遇到 yarn fresh packages 永不終止着逐,可以注掉
yarn config set registry https://registry.npmmirror.com
- 遇到
此外崔赌,再寫(xiě)個(gè)獨(dú)立的 docker-secret.yaml
來(lái)放 services/casdoor
的數(shù)據(jù)庫(kù)配置:
version: '3.1'
services:
casdoor:
environment:
driverName: "mysql"
dataSourceName: "casdoor:password@tcp(host.docker.internal:3306)/"
dbName: "casdoor"
最后,
# 運(yùn)行服務(wù)
$ docker compose -f docker-compose.yml -f docker-secret.yml up
[+] Running 2/0
? Network casdoor_default Created 0.0s
? Container casdoor-casdoor-1 Created 0.0s
Attaching to casdoor-casdoor-1
casdoor-casdoor-1 | 2023/05/14 06:00:00 Listening on 0.0.0.0:389
casdoor-casdoor-1 | 2023/05/14 06:00:00.000 [I] http server Running on http://:8000
訪問(wèn) http://localhost:8000/耸别,用戶 admin 密碼 123 登錄健芭。
結(jié)語(yǔ)
Casdoor 這里選擇源碼方式安裝,是考慮做定制化修改秀姐;使用容器編譯和運(yùn)行慈迈,是考慮發(fā)布和部署。
至于 Casdoor 功能如何省有、怎么使用痒留,要閱讀官方文檔多做了解,同時(shí)也在運(yùn)行環(huán)境里實(shí)際玩上一玩蠢沿。
GoCoding 個(gè)人實(shí)踐的經(jīng)驗(yàn)分享伸头,可關(guān)注公眾號(hào)!