diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..47617ba --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/README.MD b/README.MD index 8759811..457869a 100644 --- a/README.MD +++ b/README.MD @@ -19,6 +19,7 @@ Open-OAuth2Playground 是一个仿 Google [OAuth 2.0 Playground](https://develop - [后端编译](#后端编译) - [前端编译](#前端编译) - [统一打包](#统一打包) + - [docker运行](#docker运行) - [配置](#配置) - [后端配置](#后端配置) - [后端配置说明](#后端配置说明) @@ -81,6 +82,82 @@ chmod +x control ./control pack ``` +### docker运行(内置测试的oauth2 server服务) + +#### 1. 设置环境变量 + +在`docker-compose.yml`文件中修改`cas-demo`容器的`environment`字段 + +```yaml +environment: + - CAS_SERVER_NAME= + - SERVER_PORT= +``` + +如未设置,默认如下 + +```yaml +environment: + - CAS_SERVER_NAME=http://localhost:8444 + - SERVER_PORT=8444 +``` + +#### 2. 修改端口映射 + +在`docker-compose.yml`文件中修改容器的`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. (可选)自定义`cas_init_script.sh`脚本 + +给予执行权限 + +```shell +chmod +x cas_init_script.sh +``` + +根据需要对cas的配置进行修改,如数据库中增加用户 + +#### 5. 启动容器 + +在`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`容器内部修改 + + + ### 配置 #### 后端配置 参考 `cfg.json.example`, 创建 `cfg.jon` 配置文件,修改配置即可 diff --git a/README_en.md b/README_en.md index 4343e8c..a077b84 100644 --- a/README_en.md +++ b/README_en.md @@ -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) @@ -80,6 +81,83 @@ chmod +x control ./control pack ``` +### Running via Docker +(oauth2 server service with built-in tests) + +#### 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. 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. (Optional) Customize the `cas_init_script.sh` script + +Grant execute permission to `cas_init_script.sh` + +```shell +chmod +x cas_init_script.sh +``` + +Make changes to the cas configuration as needed, such as adding users to the database + +#### 5. 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. + ### Configuration #### Backend Configuration Refer to `cfg.json.example`, create `cfg.jon` configuration file, modify configuration as needed. diff --git a/cas_init_script.sh b/cas_init_script.sh new file mode 100644 index 0000000..8a23a28 --- /dev/null +++ b/cas_init_script.sh @@ -0,0 +1,35 @@ +#!/bin/bash +# cas_init_script.sh + +mkdir -p /export/data/ +chmod 777 /export/data/ +sqlite3 /export/data/cas.db <> "$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!" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..abe3a90 --- /dev/null +++ b/docker-compose.yml @@ -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"] \ No newline at end of file