Skip to content

Commit 7de67ac

Browse files
Merge pull request #29 from Laceyoo/main
docker支持
2 parents b63e917 + 132671b commit 7de67ac

5 files changed

Lines changed: 238 additions & 0 deletions

File tree

‎Dockerfile‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM golang:1.20
2+
3+
WORKDIR /app/Open-OAuth2Playground
4+
5+
COPY ./go.mod .
6+
COPY ./go.sum .
7+
8+
ENV GOPROXY=https://goproxy.cn,direct
9+
RUN go mod download
10+
COPY . .
11+
RUN go build -o OAuth2Playground .
12+
13+
EXPOSE 80
14+
15+
CMD ["./OAuth2Playground"]

‎README.MD‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Open-OAuth2Playground 是一个仿 Google [OAuth 2.0 Playground](https://develop
1919
- [后端编译](#后端编译)
2020
- [前端编译](#前端编译)
2121
- [统一打包](#统一打包)
22+
- [docker运行](#docker运行)
2223
- [配置](#配置)
2324
- [后端配置](#后端配置)
2425
- [后端配置说明](#后端配置说明)
@@ -81,6 +82,87 @@ chmod +x control
8182
./control pack
8283
```
8384

85+
### docker运行
86+
87+
(内置测试的oauth2 server服务)
88+
89+
#### 1.给`cas_init_script.sh`文件添加执行权限
90+
91+
执行如下命令
92+
```shell
93+
chmod +x cas_init_script.sh
94+
```
95+
96+
#### 2. 修改`docker-compose.yml`文件
97+
98+
##### 2.1 设置环境变量
99+
100+
修改`cas-demo`容器的`environment`字段
101+
102+
```yaml
103+
environment:
104+
- CAS_SERVER_NAME=
105+
- SERVER_PORT=
106+
```
107+
108+
如未设置,默认如下
109+
110+
```yaml
111+
environment:
112+
- CAS_SERVER_NAME=http://localhost:8444
113+
- SERVER_PORT=8444
114+
```
115+
116+
##### 2.2 修改端口映射
117+
118+
修改容器的`ports`字段
119+
120+
如第1步中`SERVER_PORT`非默认值8444,则需将`cas-demo`容器的端口修改为`SERVER_PORT`的值,注意容器和宿主机端口必须相同
121+
122+
```yml
123+
# open-oauth2playground容器端口,可自行修改
124+
ports:
125+
- "8080:80"
126+
# cas-demo容器
127+
ports:
128+
- "your_port:your_port"
129+
```
130+
131+
#### 3. 修改`cfg.json`配置
132+
133+
将`cfg.json`文件中的`endpoints`字段中`cas server`域名设置为第1步的`CAS_SERVER_NAME`,如第1步中未设置,则设置为`http://localhost:8444`
134+
135+
```json
136+
"endpoints": {
137+
"authorization": "http://localhost:8444/cas/oauth2.0/authorize",
138+
"token": "http://localhost:8444/cas/oauth2.0/accessToken",
139+
"userinfo": "http://localhost:8444/cas/oauth2.0/profile"
140+
}
141+
```
142+
143+
144+
#### 4. 启动容器
145+
146+
在`docker-compose.yml`所在目录下执行如下命令
147+
148+
```shell
149+
docker-compose up
150+
```
151+
152+
在`cas-domo`容器日志中看到`ready`字样,即启动成功,访问`http://localhost:8080`即可。
153+
154+
cas测试用户为:
155+
```txt
156+
user:cas
157+
password:123456
158+
```
159+
可编辑`cas_init_script.sh`文件或启动后,进入`cas-demo`容器内部修改
160+
161+
162+
#### (可选)自定义`cas_init_script.sh`脚本
163+
164+
根据需要对cas的配置进行修改,如数据库中增加用户
165+
84166
### 配置
85167
#### 后端配置
86168
参考 `cfg.json.example`, 创建 `cfg.jon` 配置文件,修改配置即可

‎README_en.md‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ When used for server-side deployment, it is also very suitable for synchronizing
1919
- [Backend Compilation](#backend-compilation)
2020
- [Frontend Compilation](#frontend-compilation)
2121
- [Unified Packaging](#unified-packaging)
22+
- [Running via Docker](#running-via-docker)
2223
- [Configuration](#configuration)
2324
- [Backend Configuration](#backend-configuration)
2425
- [Backend Configuration Description](#backend-configuration-description)
@@ -80,6 +81,87 @@ chmod +x control
8081
./control pack
8182
```
8283

84+
### Running via Docker
85+
(oauth2 server service with built-in tests)
86+
87+
#### 1. Grant execute permission to the `cas_init_script.sh` file
88+
89+
Execute the following command
90+
```shell
91+
chmod +x cas_init_script.sh
92+
```
93+
94+
#### 2. Modify the `docker-compose.yml` file
95+
96+
##### 2.1 Setting Environment Variables
97+
Modify the `environment` field of the `cas-demo` container in the `docker-compose.yml` file
98+
99+
```yaml
100+
environment:
101+
- CAS_SERVER_NAME=
102+
- SERVER_PORT=
103+
```
104+
105+
If not set, the default is as follows
106+
107+
```yaml
108+
environment:
109+
- CAS_SERVER_NAME=http://localhost:8444
110+
- SERVER_PORT=8444
111+
```
112+
113+
##### 2.2 Modify the port mapping
114+
115+
Modify the `ports` field of the container in the `docker-compose.yml` file
116+
117+
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.
118+
119+
```yml
120+
# open-oauth2playground container, you can modify it on your own
121+
ports:
122+
- "8080:80"
123+
# cas-demo container
124+
ports:
125+
- "your_port:your_port"
126+
```
127+
128+
#### 3. Modify the `cfg.json` configuration
129+
130+
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
131+
132+
```json
133+
"endpoints": {
134+
"authorization": "http://localhost:8444/cas/oauth2.0/authorize",
135+
"token": "http://localhost:8444/cas/oauth2.0/accessToken",
136+
"userinfo": "http://localhost:8444/cas/oauth2.0/profile"
137+
}
138+
```
139+
140+
#### 4. Start the container
141+
142+
Execute the following command in the directory where `docker-compose.yml` is located
143+
144+
```shell
145+
docker-compose up
146+
```
147+
148+
If you see the word `ready` in the `cas-domo` container log, the startup was successful.
149+
150+
And the cas user for test is:
151+
152+
153+
```txt
154+
user:cas
155+
password:123456
156+
```
157+
You can edit the `cas_init_script.sh` file or enter the `cas-demo` container after startup to update your user.
158+
159+
160+
#### (Optional) Customize the `cas_init_script.sh` script
161+
162+
Make changes to the cas configuration as needed, such as adding users to the database
163+
164+
83165
### Configuration
84166
#### Backend Configuration
85167
Refer to `cfg.json.example`, create `cfg.jon` configuration file, modify configuration as needed.

‎cas_init_script.sh‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
# cas_init_script.sh
3+
4+
mkdir -p /export/data/
5+
chmod 777 /export/data/
6+
sqlite3 /export/data/cas.db <<EOF
7+
CREATE TABLE IF NOT EXISTS user (username TEXT, password TEXT, name TEXT);
8+
DELETE FROM user;
9+
INSERT INTO user (username, password, name) VALUES ('cas', '123456', '测试用户');
10+
EOF
11+
12+
echo "cas.db created successfully!"
13+
14+
# 读取环境变量,如果未设置,则使用默认值
15+
CAS_SERVER_NAME=${CAS_SERVER_NAME:-"http://localhost:8444"}
16+
SERVER_PORT=${SERVER_PORT:-"8444"}
17+
18+
# 配置文件路径
19+
CAS_PROPERTIES_FILE="/etc/cas/config/cas.properties"
20+
21+
# 检查并替换或添加 server.port
22+
if grep -q "server.port" "$CAS_PROPERTIES_FILE"; then
23+
sed -i "s#server.port=.*#server.port=${SERVER_PORT}#" "$CAS_PROPERTIES_FILE"
24+
else
25+
echo "server.port=${SERVER_PORT}" >> "$CAS_PROPERTIES_FILE"
26+
fi
27+
28+
# 检查并替换或添加 cas.server.name
29+
if grep -q "cas.server.name" "$CAS_PROPERTIES_FILE"; then
30+
sed -i "s#cas.server.name=.*#cas.server.name=${CAS_SERVER_NAME}#" "$CAS_PROPERTIES_FILE"
31+
else
32+
echo "cas.server.name=${CAS_SERVER_NAME}" >> "$CAS_PROPERTIES_FILE"
33+
fi
34+
35+
echo "read configuration successfully!"

‎docker-compose.yml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: '3'
2+
services:
3+
open-oauth2playground:
4+
image: lacey620/open-oauth2playground:v1.0
5+
container_name: open-oauth2playground
6+
restart: always
7+
ports:
8+
- "8080:80"
9+
volumes:
10+
- ./cfg.json:/app/Open-OAuth2Playground/cfg.json
11+
command: ["/app/Open-OAuth2Playground/OAuth2Playground"]
12+
cas-demo:
13+
image: lacey620/cas-demo:v6.5.9
14+
container_name: cas-demo
15+
restart: always
16+
ports:
17+
- "8444:8444"
18+
environment:
19+
- CAS_SERVER_NAME=
20+
- SERVER_PORT=
21+
volumes:
22+
- ./cas_init_script.sh:/cas-overlay/cas_init_script.sh
23+
entrypoint: ["/bin/bash", "-c"]
24+
command: ["/cas-overlay/cas_init_script.sh && java -server -noverify -Xmx2048M -jar /cas-overlay/cas.war"]

0 commit comments

Comments
 (0)