Skip to content

Commit

Permalink
Merge pull request #29 from Laceyoo/main
Browse files Browse the repository at this point in the history
docker支持
  • Loading branch information
freedomkk-qfeng committed Jan 12, 2024
2 parents b63e917 + 132671b commit 7de67ac
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM golang:1.20

WORKDIR /app/Open-OAuth2Playground

COPY ./go.mod .
COPY ./go.sum .

ENV GOPROXY=https://goproxy.cn,direct
RUN go mod download
COPY . .
RUN go build -o OAuth2Playground .

EXPOSE 80

CMD ["./OAuth2Playground"]
82 changes: 82 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Open-OAuth2Playground 是一个仿 Google [OAuth 2.0 Playground](https://develop
- [后端编译](#后端编译)
- [前端编译](#前端编译)
- [统一打包](#统一打包)
- [docker运行](#docker运行)
- [配置](#配置)
- [后端配置](#后端配置)
- [后端配置说明](#后端配置说明)
Expand Down Expand Up @@ -81,6 +82,87 @@ chmod +x control
./control pack
```

### docker运行

(内置测试的oauth2 server服务)

#### 1.给`cas_init_script.sh`文件添加执行权限

执行如下命令
```shell
chmod +x cas_init_script.sh
```

#### 2. 修改`docker-compose.yml`文件

##### 2.1 设置环境变量

修改`cas-demo`容器的`environment`字段

```yaml
environment:
- CAS_SERVER_NAME=
- SERVER_PORT=
```

如未设置,默认如下

```yaml
environment:
- CAS_SERVER_NAME=http://localhost:8444
- SERVER_PORT=8444
```

##### 2.2 修改端口映射

修改容器的`ports`字段

如第1步中`SERVER_PORT`非默认值8444,则需将`cas-demo`容器的端口修改为`SERVER_PORT`的值,注意容器和宿主机端口必须相同

```yml
# open-oauth2playground容器端口,可自行修改
ports:
- "8080:80"
# cas-demo容器
ports:
- "your_port:your_port"
```

#### 3. 修改`cfg.json`配置

`cfg.json`文件中的`endpoints`字段中`cas server`域名设置为第1步的`CAS_SERVER_NAME`,如第1步中未设置,则设置为`http://localhost:8444`

```json
"endpoints": {
"authorization": "http://localhost:8444/cas/oauth2.0/authorize",
"token": "http://localhost:8444/cas/oauth2.0/accessToken",
"userinfo": "http://localhost:8444/cas/oauth2.0/profile"
}
```


#### 4. 启动容器

`docker-compose.yml`所在目录下执行如下命令

```shell
docker-compose up
```

`cas-domo`容器日志中看到`ready`字样,即启动成功,访问`http://localhost:8080`即可。

cas测试用户为:
```txt
user:cas
password:123456
```
可编辑`cas_init_script.sh`文件或启动后,进入`cas-demo`容器内部修改


#### (可选)自定义`cas_init_script.sh`脚本

根据需要对cas的配置进行修改,如数据库中增加用户

### 配置
#### 后端配置
参考 `cfg.json.example`, 创建 `cfg.jon` 配置文件,修改配置即可
Expand Down
82 changes: 82 additions & 0 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ When used for server-side deployment, it is also very suitable for synchronizing
- [Backend Compilation](#backend-compilation)
- [Frontend Compilation](#frontend-compilation)
- [Unified Packaging](#unified-packaging)
- [Running via Docker](#running-via-docker)
- [Configuration](#configuration)
- [Backend Configuration](#backend-configuration)
- [Backend Configuration Description](#backend-configuration-description)
Expand Down Expand Up @@ -80,6 +81,87 @@ chmod +x control
./control pack
```

### Running via Docker
(oauth2 server service with built-in tests)

#### 1. Grant execute permission to the `cas_init_script.sh` file

Execute the following command
```shell
chmod +x cas_init_script.sh
```

#### 2. Modify the `docker-compose.yml` file

##### 2.1 Setting Environment Variables
Modify the `environment` field of the `cas-demo` container in the `docker-compose.yml` file

```yaml
environment:
- CAS_SERVER_NAME=
- SERVER_PORT=
```

If not set, the default is as follows

```yaml
environment:
- CAS_SERVER_NAME=http://localhost:8444
- SERVER_PORT=8444
```

##### 2.2 Modify the port mapping

Modify the `ports` field of the container in the `docker-compose.yml` file

If `SERVER_PORT` in step 1 is not the default value of 8444, then you need to change the port of the `cas-demo` container to the value of `SERVER_PORT`, noting that the container and host ports must be the same.

```yml
# open-oauth2playground container, you can modify it on your own
ports:
- "8080:80"
# cas-demo container
ports:
- "your_port:your_port"
```

#### 3. Modify the `cfg.json` configuration

Set the `cas server` domain name in the `endpoints` field in the `cfg.json` file to `CAS_SERVER_NAME` from step 1, or to `http://localhost:8444` if not set in step 1

```json
"endpoints": {
"authorization": "http://localhost:8444/cas/oauth2.0/authorize",
"token": "http://localhost:8444/cas/oauth2.0/accessToken",
"userinfo": "http://localhost:8444/cas/oauth2.0/profile"
}
```

#### 4. Start the container

Execute the following command in the directory where `docker-compose.yml` is located

```shell
docker-compose up
```

If you see the word `ready` in the `cas-domo` container log, the startup was successful.

And the cas user for test is:


```txt
user:cas
password:123456
```
You can edit the `cas_init_script.sh` file or enter the `cas-demo` container after startup to update your user.


#### (Optional) Customize the `cas_init_script.sh` script

Make changes to the cas configuration as needed, such as adding users to the database


### Configuration
#### Backend Configuration
Refer to `cfg.json.example`, create `cfg.jon` configuration file, modify configuration as needed.
Expand Down
35 changes: 35 additions & 0 deletions cas_init_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
# cas_init_script.sh

mkdir -p /export/data/
chmod 777 /export/data/
sqlite3 /export/data/cas.db <<EOF
CREATE TABLE IF NOT EXISTS user (username TEXT, password TEXT, name TEXT);
DELETE FROM user;
INSERT INTO user (username, password, name) VALUES ('cas', '123456', '测试用户');
EOF

echo "cas.db created successfully!"

# 读取环境变量,如果未设置,则使用默认值
CAS_SERVER_NAME=${CAS_SERVER_NAME:-"http://localhost:8444"}
SERVER_PORT=${SERVER_PORT:-"8444"}

# 配置文件路径
CAS_PROPERTIES_FILE="/etc/cas/config/cas.properties"

# 检查并替换或添加 server.port
if grep -q "server.port" "$CAS_PROPERTIES_FILE"; then
sed -i "s#server.port=.*#server.port=${SERVER_PORT}#" "$CAS_PROPERTIES_FILE"
else
echo "server.port=${SERVER_PORT}" >> "$CAS_PROPERTIES_FILE"
fi

# 检查并替换或添加 cas.server.name
if grep -q "cas.server.name" "$CAS_PROPERTIES_FILE"; then
sed -i "s#cas.server.name=.*#cas.server.name=${CAS_SERVER_NAME}#" "$CAS_PROPERTIES_FILE"
else
echo "cas.server.name=${CAS_SERVER_NAME}" >> "$CAS_PROPERTIES_FILE"
fi

echo "read configuration successfully!"
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: '3'
services:
open-oauth2playground:
image: lacey620/open-oauth2playground:v1.0
container_name: open-oauth2playground
restart: always
ports:
- "8080:80"
volumes:
- ./cfg.json:/app/Open-OAuth2Playground/cfg.json
command: ["/app/Open-OAuth2Playground/OAuth2Playground"]
cas-demo:
image: lacey620/cas-demo:v6.5.9
container_name: cas-demo
restart: always
ports:
- "8444:8444"
environment:
- CAS_SERVER_NAME=
- SERVER_PORT=
volumes:
- ./cas_init_script.sh:/cas-overlay/cas_init_script.sh
entrypoint: ["/bin/bash", "-c"]
command: ["/cas-overlay/cas_init_script.sh && java -server -noverify -Xmx2048M -jar /cas-overlay/cas.war"]

0 comments on commit 7de67ac

Please sign in to comment.