博客 / 詳情

返回

Casdoor 開始

Casdoor 是一個基於 OAuth 2.0 / OIDC 的中心化的單點登錄(SSO)身份驗證平台,簡單來説,就是 Casdoor 可以幫你解決用户管理的難題,你無需開發用户登錄、註冊等與用户鑑權相關的一系列功能,只需幾個步驟進行簡單配置,與你的主應用配合,便可完全託管你的用户模塊,簡單省心,功能強大。

  • 官網: https://casdoor.org/
  • 代碼: https://github.com/casdoor/casdoor

官網有 demo 體驗,及文檔。本文是依照文檔「服務器安裝」「使用 Docker 運行」於 Ubuntu 22 上的實踐記錄。

安裝環境

  • Go 1.17+
  • Node.js LTS (16或14)
  • Yarn 1.x

安裝 Go

# 下載,依據系統選擇 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

安裝 Node.js

# 下載,選了當前最新的 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
# 或啓動
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

容器運行

Docker 準備

Install Docker Desktop on Ubuntu,

$ docker -v
Docker version 23.0.6, build ef23cbc

$ docker compose version
Docker Compose version v2.17.3

Docker 運行

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 即可
    • 遇到 apk 安裝問題,可以注掉 RUN sed -i 's/https/http/' /etc/apk/repositories
    • 遇到 yarn fresh packages 永不終止,可以注掉 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 個人實踐的經驗分享,可關注公眾號!
user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.