docker部署onlyoffice,开启JWT权限校验Token

原来的部署方式

之前的方式是禁用了JWT:

bash 复制代码
docker run -itd -p 8080:80 --name docserver --network host  -e JWT_ENABLED=false --restart=always onlyoffice/documentserver:8 

新的部署方式

参考文档:https://helpcenter.onlyoffice.com/installation/docs-developer-install-docker.aspx?from=api

启用JWT Token:-e JWT_SECRET=my_jwt_secret

bash 复制代码
docker run -itd -p 8080:80 --name docserver --network host  -e JWT_SECRET=my_jwt_secret --restart=always onlyoffice/documentserver:8 

保存容器的数据:

参考命令:

bash 复制代码
sudo docker run -i -t -d -p 80:80 --restart=always \
    -v /app/onlyoffice/DocumentServer/logs:/var/log/onlyoffice  \
    -v /app/onlyoffice/DocumentServer/data:/var/www/onlyoffice/Data  \
    -v /app/onlyoffice/DocumentServer/lib:/var/lib/onlyoffice \
    -v /app/onlyoffice/DocumentServer/db:/var/lib/postgresql -e JWT_SECRET=my_jwt_secret onlyoffice/documentserver-de

创建相关的目录:

bash 复制代码
cd ~/docker/docserver
mkdir logs data lib db

停止容器:

bash 复制代码
docker stop docserver && docker rm docserver

构建容器:

bash 复制代码
docker run -itd --name docserver -p 18889:80 --restart=always \
    -v /home/zhangdapeng/docker/docserver/logs:/var/log/onlyoffice  \
    -v /home/zhangdapeng/docker/docserver/data:/var/www/onlyoffice/Data  \
    -v /home/zhangdapeng/docker/docserver/lib:/var/lib/onlyoffice \
    -v /home/zhangdapeng/docker/docserver/db:/var/lib/postgresql -e JWT_SECRET=zhangdapeng520 onlyoffice/documentserver:8

查看容器日志:

bash 复制代码
docker logs -f --tail 100 docserver

获取JWT Token

参考文档:https://api.onlyoffice.com/zh/editors/signature/

将配置文件复制出来:

bash 复制代码
docker cp docserver:/etc/onlyoffice/documentserver/local.json .

原始默认内容:

json 复制代码
{
  "services": {
    "CoAuthoring": {
      "sql": {
        "type": "postgres",
        "dbHost": "localhost",
        "dbPort": "5432",
        "dbName": "onlyoffice",
        "dbUser": "onlyoffice",
        "dbPass": "onlyoffice"
      },
      "token": {
        "enable": {
          "request": {
            "inbox": true,
            "outbox": true
          },
          "browser": true
        },
        "inbox": {
          "header": "Authorization",
          "inBody": false
        },
        "outbox": {
          "header": "Authorization",
          "inBody": false
        }
      },
      "secret": {
        "inbox": {
          "string": "zhangdapeng520"
        },
        "outbox": {
          "string": "zhangdapeng520"
        },
        "session": {
          "string": "zhangdapeng520"
        }
      }
    }
  },
  "rabbitmq": {
    "url": "amqp://guest:guest@localhost"
  },
  "wopi": {
    "enable": false,
    "privateKey": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC12HeKvvcytCl7\nvBpXP8uO8SSvntAHTT/hOZIJnCsMRs5OQiETa690iaU+Dos4xY6RyQW7jqioyWlC\n8cQXFu7JG9XhFkEmX2hIM3YouzjVnXr1C75U77q91svj2p2HG7c/UhglS/1plYuG\nmlqTMqxSKqERnbvtxpRweg629o+u9te++dJp+Lco5v0UE17xm5GmW85nG4NpMuf4\nrXx1m43nCjIPJ6+eVyDl1C1q9OoQz9azJjq6B2PDaLC/FI+HVsL55GD+CgFjmAjT\nooyqM7/5adbC4NjNGrxIi6pE28fndSiB3jlgrBYGMzK5G8fwWjbB3K+ZjZWsJY+d\n53PMueH1AgMBAAECggEADol0nDL1W2hoQCi16SkQhbSEv8T2d8oqCkAj16cHdyhH\nGgRF7tUD39EyVG2T0KDua3yCYk+E74iGZMa9OJe2bn2BeBVJqQUlUE9/QygdL4QZ\nQVl3SJ5cTQyZgqh5dMSqitVuJVEkiLPFRY61h4eCzuC/vPZHzaBuqAObhVUQAHPY\n8Y50kXFcCCsQdAuqV+jTG8+Er6nmktBBig6k+QP/XptwLtLpJiAdksVXvCDrHAJC\nzFTfDLCf0D4QIg5ecjIY/TfSOwPmXk11/xLLSmCJ9HnIWh9uReyo+aWKL748MRLk\nsVbRY4xT6DtDv5Ni47lECDQETg5h9d1NsrE4AQn40wKBgQDVbT2lu1Qma7AftFzW\nF1Zdkh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FI/txuHndrjy9a9uu9Uvgv1ep3VOLsodjNIaF8mQRbh1RvJ7hYXxPFCacmoqI67BcmRjsU4iw4+pYl0r2sTIUJOzkYMK5wJkjnhP00H0J6vJPGOyz9XGrx7KbQy976Kd9i1",
    "modulus": "B5D8778ABEF732B4297BBC1A573FCB8EF124AF9ED0074D3FE13992099C2B0C46CE4E4221136BAF7489A53E0E8B38C58E91C905BB8EA8A8C96942F1C41716EEC91BD5E11641265F6848337628BB38D59D7AF50BBE54EFBABDD6CBE3DA9D871BB73F5218254BFD69958B869A5A9332AC522AA1119DBBEDC694707A0EB6F68FAEF6D7BEF9D269F8B728E6FD14135EF19B91A65BCE671B836932E7F8AD7C759B8DE70A320F27AF9E5720E5D42D6AF4EA10CFD6B3263ABA0763C368B0BF148F8756C2F9E460FE0A01639808D3A28CAA33BFF969D6C2E0D8CD1ABC488BAA44DBC7E7752881DE3960AC16063332B91BC7F05A36C1DCAF998D95AC258F9DE773CCB9E1F5",
    "modulusOld": "B5D8778ABEF732B4297BBC1A573FCB8EF124AF9ED0074D3FE13992099C2B0C46CE4E4221136BAF7489A53E0E8B38C58E91C905BB8EA8A8C96942F1C41716EEC91BD5E11641265F6848337628BB38D59D7AF50BBE54EFBABDD6CBE3DA9D871BB73F5218254BFD69958B869A5A9332AC522AA1119DBBEDC694707A0EB6F68FAEF6D7BEF9D269F8B728E6FD14135EF19B91A65BCE671B836932E7F8AD7C759B8DE70A320F27AF9E5720E5D42D6AF4EA10CFD6B3263ABA0763C368B0BF148F8756C2F9E460FE0A01639808D3A28CAA33BFF969D6C2E0D8CD1ABC488BAA44DBC7E7752881DE3960AC16063332B91BC7F05A36C1DCAF998D95AC258F9DE773CCB9E1F5",
    "exponent": 65537,
    "exponentOld": 65537
  },
  "storage": {
    "fs": {
      "secretString": "HD97Gwvu8Z28TVQQOgMS"
    }
  }
}

参数说明:

修改:

json 复制代码
{
  "services": {
    "CoAuthoring": {
      "sql": {
        "type": "postgres",
        "dbHost": "localhost",
        "dbPort": "5432",
        "dbName": "onlyoffice",
        "dbUser": "onlyoffice",
        "dbPass": "onlyoffice"
      },
      "token": {
        "enable": {
          "request": {
            "inbox": true,
            "outbox": true
          },
          "browser": true
        },
        "inbox": {
          "header": "Authorization",
          "inBody": true
        },
        "outbox": {
          "header": "Authorization",
          "inBody": true
        }
      },
      "secret": {
        "inbox": {
          "string": "zhangdapeng520"
        },
        "outbox": {
          "string": "zhangdapeng520"
        },
        "session": {
          "string": "zhangdapeng520"
        }
      }
    }
  },
  "rabbitmq": {
    "url": "amqp://guest:guest@localhost"
  },
  "wopi": {
    "enable": false,
    "privateKey": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC12HeKvvcytCl7\nvBpXP8uO8SSvntAHTT/hOZIJnCsMRs5OQiETa690iaU+Dos4xY6RyQW7jqioyWlC\n8cQXFu7JG9XhFkEmX2hIM3YouzjVnXr1C75U77q91svj2p2HG7c/UhglS/1plYuG\nmlqTMqxSKqERnbvtxpRweg629o+u9te++dJp+Lco5v0UE17xm5GmW85nG4NpMuf4\nrXx1m43nCjIPJ6+eVyDl1C1q9OoQz9azJjq6B2PDaLC/FI+HVsL55GD+CgFjmAjT\nooyqM7/5adbC4NjNGrxIi6pE28fndSiB3jlgrBYGMzK5G8fwWjbB3K+ZjZWsJY+d\n53PMueH1AgMBAAECggEADol0nDL1W2hoQCi16SkQhbSEv8T2d8oqCkAj16cHdyhH\nGgRF7tUD39EyVG2T0KDua3yCYk+E74iGZMa9OJe2bn2BeBVJqQUlUE9/QygdL4QZ\nQVl3SJ5cTQyZgqh5dMSqitVuJVEkiLPFRY61h4eCzuC/vPZHzaBuqAObhVUQAHPY\n8Y50kXFcCCsQdAuqV+jTG8+Er6nmktBBig6k+QP/XptwLtLpJiAdksVXvCDrHAJC\nzFTfDLCf0D4QIg5ecjIY/TfSOwPmXk11/xLLSmCJ9HnIWh9uReyo+aWKL748MRLk\nsVbRY4x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nzlumkZvxXhMU/eYot/hp0vm+1/auj/a2DnpwlMbtu50RoSpSrDKTWpqGi5Vp/UslGFI/txuHndrjy9a9uu9Uvgv1ep3VOLsodjNIaF8mQRbh1RvJ7hYXxPFCacmoqI67BcmRjsU4iw4+pYl0r2sTIUJOzkYMK5wJkjnhP00H0J6vJPGOyz9XGrx7KbQy976Kd9i1",
    "modulus": "B5D8778ABEF732B4297BBC1A573FCB8EF124AF9ED0074D3FE13992099C2B0C46CE4E4221136BAF7489A53E0E8B38C58E91C905BB8EA8A8C96942F1C41716EEC91BD5E11641265F6848337628BB38D59D7AF50BBE54EFBABDD6CBE3DA9D871BB73F5218254BFD69958B869A5A9332AC522AA1119DBBEDC694707A0EB6F68FAEF6D7BEF9D269F8B728E6FD14135EF19B91A65BCE671B836932E7F8AD7C759B8DE70A320F27AF9E5720E5D42D6AF4EA10CFD6B3263ABA0763C368B0BF148F8756C2F9E460FE0A01639808D3A28CAA33BFF969D6C2E0D8CD1ABC488BAA44DBC7E7752881DE3960AC16063332B91BC7F05A36C1DCAF998D95AC258F9DE773CCB9E1F5",
    "modulusOld": "B5D8778ABEF732B4297BBC1A573FCB8EF124AF9ED0074D3FE13992099C2B0C46CE4E4221136BAF7489A53E0E8B38C58E91C905BB8EA8A8C96942F1C41716EEC91BD5E11641265F6848337628BB38D59D7AF50BBE54EFBABDD6CBE3DA9D871BB73F5218254BFD69958B869A5A9332AC522AA1119DBBEDC694707A0EB6F68FAEF6D7BEF9D269F8B728E6FD14135EF19B91A65BCE671B836932E7F8AD7C759B8DE70A320F27AF9E5720E5D42D6AF4EA10CFD6B3263ABA0763C368B0BF148F8756C2F9E460FE0A01639808D3A28CAA33BFF969D6C2E0D8CD1ABC488BAA44DBC7E7752881DE3960AC16063332B91BC7F05A36C1DCAF998D95AC258F9DE773CCB9E1F5",
    "exponent": 65537,
    "exponentOld": 65537
  },
  "storage": {
    "fs": {
      "secretString": "HD97Gwvu8Z28TVQQOgMS"
    }
  }
}

复制回去:

bash 复制代码
docker cp local.json docserver:/etc/onlyoffice/documentserver/local.json

重启:

bash 复制代码
docker restart docserver

使用Python生成Token

官方文档实例:https://api.onlyoffice.com/zh/editors/signature/#python

python 复制代码
def encode(payload):
    return jwt.encode(payload, config.DOC_SERV_JWT_SECRET, algorithm='HS256')

使用zdppy_jwt框架生成Token:

python 复制代码
import jwt

key = "zhangdapeng520"
algorithm = "HS256"
data = {"id": "1", "username": "zhangdapeng", "role": "admin"}
encoded = jwt.encode(data, key, algorithm=algorithm)
print(encoded)

得到结果:

bash 复制代码
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEiLCJ1c2VybmFtZSI6InpoYW5nZGFwZW5nIiwicm9sZSI6ImFkbWluIn0.EyayWbKV5arHahTiXkSIKb6e1wF5a2pf6NhszyjZYfE

使用之前成功的代码进行测试

后端代码:

python 复制代码
import json
import req

import api
from api import middleware
from api.zjson.response import JSONResponse


async def doc_callback(request):
    data = await api.req.get_json(request)
    print("callback === ", data)
    # status == 2 文档准备好被保存
    if data.get("status") == 2:
        req.download(data.get("url"), "data/test.docx")
    if data.get("status") == 6:
        req.download(data.get("url"), "data/test.docx")
    # status == 6 文档编辑会话关闭
    data = {"error": 0}
    # return JSONResponse('{"error":0}')
    return JSONResponse(json.dumps(data))


app = api.Api(
    routes=[
        api.resp.dir_route("/dist", "data"),
        api.resp.post("/callback", doc_callback),
    ],
    middleware=[middleware.cors()],
)

if __name__ == "__main__":
    app.run(port=18888)

前端代码:

html 复制代码
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite + Vue</title>
  </head>
  <body>
    <div id="app"></div>
    <script type="text/javascript" src="http://127.0.0.1:18889/web-apps/apps/api/documents/api.js"></script>
    <script type="module" src="/src/main.js"></script>
  </body>
</html>

但是,现在当我们点击加载的时候,会报如下错误:

错误原因是因为我们之前的docserver是禁用了JWT的,但是现在我们启用了,而代码还是原来的代码,没有做任何修改。

实际上,我们应该加上JWT Token相关的信息配置。

添加JWT Token

注意:token的payload必须是文档config的完整信息:https://api.onlyoffice.com/zh/editors/signature/browser

改完后token生成代码如下:

python 复制代码
import jwt

key = "zhangdapeng520"
algorithm = "HS256"
data = {
    "document": {
      "fileType": "docx",
      "key": "Khirz6zTPdfd7",
      "title": "Example Document Title.docx",
      "url": "http://127.0.0.1:18888/dist/test.docx"
    },
    "editorConfig": {
      "callbackUrl": "http://127.0.0.1:18888/callback",
      "lang": "zh-CN",
      "user": {
        "id": 1,
        "name": "zdp"
      },
      "customization": {
        "forcesave": True,
      },
      "coEditing": {
        "mode": "fast",
        "change": True
      },
    },
    "documentType": "word",
    "height": '500px',
    "width": '100%',
  }
encoded = jwt.encode(data, key, algorithm=algorithm)
print(encoded)

改完后前端代码如下:

html 复制代码
<script setup>
import {message} from "ant-design-vue";

const onLoadDocumentClick = () => {
  message.success("load document")
  // isDocument.value = true
  new DocsAPI.DocEditor("doc", {
    "document": {
      "fileType": "docx",
      "key": `Khirz6zTPdfd7`,
      "title": "Example Document Title.docx",
      "url": "http://127.0.0.1:18888/dist/test.docx"
    },
    "editorConfig": {
      "callbackUrl": "http://127.0.0.1:18888/callback",
      "lang": "zh-CN",
      "user": {
        "id": 1,
        "name": `zdp`
      },
      "customization": {
        "forcesave": true,
      },
      "coEditing": {
        "mode": "fast",
        "change": true
      },
    },
    "documentType": "word",
    height: '500px',
    width: '100%',
    token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb2N1bWVudCI6eyJmaWxlVHlwZSI6ImRvY3giLCJrZXkiOiJLaGlyejZ6VFBkZmQ3IiwidGl0bGUiOiJFeGFtcGxlIERvY3VtZW50IFRpdGxlLmRvY3giLCJ1cmwiOiJodHRwOi8vMTI3LjAuMC4xOjE4ODg4L2Rpc3QvdGVzdC5kb2N4In0sImVkaXRvckNvbmZpZyI6eyJjYWxsYmFja1VybCI6Imh0dHA6Ly8xMjcuMC4wLjE6MTg4ODgvY2FsbGJhY2siLCJsYW5nIjoiemgtQ04iLCJ1c2VyIjp7ImlkIjoxLCJuYW1lIjoiemRwIn0sImN1c3RvbWl6YXRpb24iOnsiZm9yY2VzYXZlIjp0cnVlfSwiY29FZGl0aW5nIjp7Im1vZGUiOiJmYXN0IiwiY2hhbmdlIjp0cnVlfX0sImRvY3VtZW50VHlwZSI6IndvcmQiLCJoZWlnaHQiOiI1MDBweCIsIndpZHRoIjoiMTAwJSJ9.AolS4haisOZc9IxQcd99ovD5FixcfswrhWOnbSRaCJw",
  });
}
</script>
<template>
  <div class="bg-indigo-50 p-8 min-h-screen">
    <div class="bg-amber-200 p-8">
      <a-button type="primary" @click="onLoadDocumentClick">Load Document</a-button>
    </div>
    <div class="bg-amber-400 p-8 min-h-96">
      <div id="doc">doc</div>
    </div>
  </div>
</template>

此时token错误的问题已经解决了,但是出现了下载失败的问题:

解决下载失败的问题

把配置文件复制出来:

bash 复制代码
docker cp docserver:/etc/onlyoffice/documentserver/default.json ~/Downloads/default.json

修改:之前

bash 复制代码
"allowPrivateIPAddress": false,
"allowMetaIPAddress": false

修改:之后

bash 复制代码
"allowPrivateIPAddress": true,
"allowMetaIPAddress": true

把文件复制进去:

bash 复制代码
docker cp default.json docserver:/etc/onlyoffice/documentserver/default.json 

进入容器:

bash 复制代码
docker exec -it docserver bash

重启相关服务:

bash 复制代码
supervisorctl restart all

或者一次性执行:

bash 复制代码
docker exec docserver supervisorctl restart all

最终解决方案

生成token:

python 复制代码
import jwt

key = "zhangdapeng520"
algorithm = "HS256"
data = {
    "document": {
      "fileType": "docx",
      "key": "Khirz6zTPdfd7",
      "title": "Example Document Title.docx",
      "url": "http://192.168.234.138:18888/dist/test.docx"
    },
    "editorConfig": {
      "callbackUrl": "http://192.168.234.138:18888/callback",
      "lang": "zh-CN",
      "user": {
        "id": "1",
        "name": "zdp"
      },
      "customization": {
        "forcesave": True,
      },
      "coEditing": {
        "mode": "fast",
        "change": True
      },
    },
    "documentType": "word",
    "height": '500px',
    "width": '100%',
  }
encoded = jwt.encode(data, key, algorithm=algorithm)
print(encoded)

前端代码:

html 复制代码
<script setup>
import {message} from "ant-design-vue";

const onLoadDocumentClick = () => {
  message.success("load document")
  // isDocument.value = true
  new DocsAPI.DocEditor("doc", {
    "document": {
      "fileType": "docx",
      "key": `Khirz6zTPdfd7`,
      "title": "Example Document Title.docx",
      "url": "http://192.168.234.138:18888/dist/test.docx"
    },
    "editorConfig": {
      "callbackUrl": "http://192.168.234.138:18888/callback",
      "lang": "zh-CN",
      "user": {
        "id": "1",
        "name": "zdp"
      },
      "customization": {
        "forcesave": true,
      },
      "coEditing": {
        "mode": "fast",
        "change": true
      },
    },
    "documentType": "word",
    height: '500px',
    width: '100%',
    token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb2N1bWVudCI6eyJmaWxlVHlwZSI6ImRvY3giLCJrZXkiOiJLaGlyejZ6VFBkZmQ3IiwidGl0bGUiOiJFeGFtcGxlIERvY3VtZW50IFRpdGxlLmRvY3giLCJ1cmwiOiJodHRwOi8vMTkyLjE2OC4yMzQuMTM4OjE4ODg4L2Rpc3QvdGVzdC5kb2N4In0sImVkaXRvckNvbmZpZyI6eyJjYWxsYmFja1VybCI6Imh0dHA6Ly8xOTIuMTY4LjIzNC4xMzg6MTg4ODgvY2FsbGJhY2siLCJsYW5nIjoiemgtQ04iLCJ1c2VyIjp7ImlkIjoiMSIsIm5hbWUiOiJ6ZHAifSwiY3VzdG9taXphdGlvbiI6eyJmb3JjZXNhdmUiOnRydWV9LCJjb0VkaXRpbmciOnsibW9kZSI6ImZhc3QiLCJjaGFuZ2UiOnRydWV9fSwiZG9jdW1lbnRUeXBlIjoid29yZCIsImhlaWdodCI6IjUwMHB4Iiwid2lkdGgiOiIxMDAlIn0.vnSYKU22-IsBmuoSn5Po-Ukz1RrnM-G3ch3-JuJd1xg",
  });
}
</script>
<template>
  <div class="bg-indigo-50 p-8 min-h-screen">
    <div class="bg-amber-200 p-8">
      <a-button type="primary" @click="onLoadDocumentClick">Load Document</a-button>
    </div>
    <div class="bg-amber-400 p-8 min-h-96">
      <div id="doc">doc</div>
    </div>
  </div>
</template>

总结

虽然上面的方案解决了Token校验的问题,但是,我们一开始的目标并没有实现。 那就是我们想要解决文件下载失败和保存文件失败的问题。

这两个错误实际上都是警告,不是严格错误。而且,我们的文档最终也都被成功的加载或者是保存了,实际上并没有发生错误,而我docserver文档服务发出的警告。

如果要提高用户的体验,我们还是得想办法就这两个警告给禁止掉或者解决掉。

后面继续研究。

相关推荐
荒Huang4 分钟前
Linux挖矿病毒(kswapd0进程使cpu爆满)
linux·运维·服务器
海阔天空_201314 分钟前
Python pyautogui库:自动化操作的强大工具
运维·开发语言·python·青少年编程·自动化
桥田智能17 分钟前
气爪在自动化装配线中是如何应用的?
运维·自动化
MonkeyKing_sunyuhua19 分钟前
ubuntu22.04 docker-compose安装postgresql数据库
数据库·docker·postgresql
追风林1 小时前
mac m1 docker本地部署canal 监听mysql的binglog日志
java·docker·mac
€☞扫地僧☜€2 小时前
docker 拉取MySQL8.0镜像以及安装
运维·数据库·docker·容器
茶馆大橘2 小时前
微服务系列六:分布式事务与seata
分布式·docker·微服务·nacos·seata·springcloud
其乐无涯2 小时前
服务器技术(一)--Linux基础入门
linux·运维·服务器
Diamond技术流2 小时前
从0开始学习Linux——网络配置
linux·运维·网络·学习·安全·centos
写bug的小屁孩2 小时前
前后端交互接口(三)
运维·服务器·数据库·windows·用户界面·qt6.3