Casdoor 是一個基於 OAuth 2.0 / OIDC 的中心化的單點登入(SSO)身份驗證平臺,簡單來說,就是 Casdoor 可以幫你解決使用者管理的難題,你無需開發使用者登入、註冊等與使用者鑑權相關的一系列功能,只需幾個步驟進行簡單設定,與你的主應用配合,便可完全託管你的使用者模組,簡單省心,功能強大。
官網有 demo 體驗,及檔案。本文是依照檔案「伺服器安裝」「使用 Docker 執行」於 Ubuntu 22 上的實踐記錄。
# 下載,依據系統選擇 Linux x86-64 的釋出包
curl -O -L https://go.dev/dl/go1.20.4.linux-amd64.tar.gz
# 解壓
tar -xzvf go1.20.4.linux-amd64.tar.gz
# 重新命名,帶上版本號
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
# 下載,選了當前最新的 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
npm install yarn -g
# 檢查
yarn -v
sudo apt update -y
# 安裝
sudo apt install mysql-server -y
# 檢查
systemctl status mysql.service
# 或啟動
systemctl start mysql.service
設定 MySQL:
1 修改 root 使用者的密碼,
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourPassword';
exit
不然,執行 mysql_secure_installation
會遇到如下錯誤:
... 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 執行設定指令碼 mysql_secure_installation
把不安全的功能都給關了,
$ 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 恢復 sudo mysql
登入,
用使用者端的話,跳過這一步。
# 密碼登入
mysql -u root -p
# 恢復 sudo mysql 登入
ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
# 退出
exit
安裝 MySQL 使用者端:
# 例如,用 MySQL Workbench
sudo snap install mysql-workbench-community
# 或者,用 phpMyAdmin 等
選擇 Local 範例,用密碼登入,
建立一個名為 casdoor 的資料庫,
另外,可建立一個名為 casdoor 的新使用者,專門管理該資料庫。
進工作目錄,獲取 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}
目前先只設定資料庫欄位 driverName
dataSourceName
dbName
。更多欄位說明,見官方檔案「伺服器安裝 / 通過-ini-檔案設定」。
執行後端:
cd casdoor/
go run main.go
如果發生錯誤 checksum mismatch
,可執行:
go clean -modcache
rm go.sum
go mod tidy
# 還不行,切個代理,再試一次
# 可能代理快取不一致;可寫進 ~/.bashrc
export GOPROXY="https://goproxy.cn,direct"
執行前端:
cd casdoor/web
yarn install
yarn start
存取 http://localhost:7001/,使用者 admin 密碼 123 登入,
執行後端:
cd casdoor/
go build
./casdoor
執行前端:
cd casdoor/web
yarn install
yarn build
Install Docker Desktop on Ubuntu,
$ docker -v
Docker version 23.0.6, build ef23cbc
$ docker compose version
Docker Compose version v2.17.3
Casdoor 可以使用 docker-compose 執行,它帶有獨立的資料庫,
cd casdoor/
docker compose up
可以如下修改,用本地已有的資料庫,
docker-compose.yml
services/casdoor
下,
entrypoint
裡的 --createDatabase=true
引數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 內容
go build
提示版本問題,可修改 FROM golang:1.17.5 AS BACK
升下版本,如 1.20.4
go test
不過,
export GOPROXY="https://goproxy.cn,direct" &&
用代理TestGetVersionInfo
Fail,可 git pull --unshallow
拉取更多 commits 即可RUN sed -i 's/https/http/' /etc/apk/repositories
yarn config set registry https://registry.npmmirror.com
此外,再寫個獨立的 docker-secret.yaml
來放 services/casdoor
的資料庫設定:
version: '3.1'
services:
casdoor:
environment:
driverName: "mysql"
dataSourceName: "casdoor:password@tcp(host.docker.internal:3306)/"
dbName: "casdoor"
最後,
# 執行服務
$ 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
存取 http://localhost:8000/,使用者 admin 密碼 123 登入。
Casdoor 這裡選擇原始碼方式安裝,是考慮做客製化化修改;使用容器編譯和執行,是考慮釋出和部署。
至於 Casdoor 功能如何、怎麼使用,要閱讀官方檔案多做了解,同時也在執行環境裡實際玩上一玩。
GoCoding 個人實踐的經驗分享,可關注公眾號!