From 026712b9b77678061a09057a372c358e30f86093 Mon Sep 17 00:00:00 2001 From: cwchen <1048842385@qq.com> Date: Tue, 4 Nov 2025 16:59:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E9=9B=86=E6=88=90=20onlyoffi?= =?UTF-8?q?ce?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application-file.yml | 4 +- .../file/service/OnlyOfficeJwtService.java | 2 + .../bonus/file/service/OnlyOfficeService.java | 66 +++++++++++++++++-- 3 files changed, 65 insertions(+), 7 deletions(-) diff --git a/bonus-admin/src/main/resources/application-file.yml b/bonus-admin/src/main/resources/application-file.yml index ba3ee82..7588c7a 100644 --- a/bonus-admin/src/main/resources/application-file.yml +++ b/bonus-admin/src/main/resources/application-file.yml @@ -9,8 +9,8 @@ minio: # onlyoffice配置 onlyoffice: document-server: http://192.168.0.14:19840/ - secret: N9yleoAAnWNo4VY4Dpe0ihH02LpQOigz - jwt-enabled: false + secret: IF8oJPkqTQPnrnYGk0115PA10sKCE05Z + jwt-enabled: true #minio: # url: http://127.0.0.1:9005 diff --git a/bonus-file/src/main/java/com/bonus/file/service/OnlyOfficeJwtService.java b/bonus-file/src/main/java/com/bonus/file/service/OnlyOfficeJwtService.java index 0f29b57..a7878b1 100644 --- a/bonus-file/src/main/java/com/bonus/file/service/OnlyOfficeJwtService.java +++ b/bonus-file/src/main/java/com/bonus/file/service/OnlyOfficeJwtService.java @@ -38,4 +38,6 @@ public class OnlyOfficeJwtService { return false; } } + + } diff --git a/bonus-file/src/main/java/com/bonus/file/service/OnlyOfficeService.java b/bonus-file/src/main/java/com/bonus/file/service/OnlyOfficeService.java index 2bd8dd1..15a6020 100644 --- a/bonus-file/src/main/java/com/bonus/file/service/OnlyOfficeService.java +++ b/bonus-file/src/main/java/com/bonus/file/service/OnlyOfficeService.java @@ -4,6 +4,7 @@ import com.bonus.common.core.domain.model.LoginUser; import com.bonus.common.domain.file.vo.OnlyOfficeCallback; import com.bonus.common.utils.FileUtil; import com.bonus.common.utils.SecurityUtils; +import com.bonus.common.utils.ip.IpUtils; import com.bonus.file.config.OnlyOfficeConfig; import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.RedisTemplate; @@ -14,6 +15,7 @@ import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; import java.io.ByteArrayInputStream; +import java.net.URLEncoder; import java.time.Duration; import java.util.HashMap; import java.util.Map; @@ -47,19 +49,32 @@ public class OnlyOfficeService { public Map getConfigWithToken(String fileKey, String fileName, String mode) throws Exception { Map map = buildEditorConfig(fileKey, fileName, mode); - String token = onlyOfficeJwtService.generateToken(map); + log.info("map:{}",map); + String token = generateJwtToken(map); + boolean b = onlyOfficeJwtService.verifyToken(token); + log.info("b:{}",b); + @SuppressWarnings("unchecked") + Map editorConfig = (Map) map.getOrDefault("editorConfig", new HashMap<>()); + log.info("token:{}",token); + // 添加token + editorConfig.put("token", token); + // 确保editorConfig被放回map中 + map.put("editorConfig", editorConfig); map.put("token", token); return map; } public Map buildEditorConfig(String fileKey, String fileName, String mode) throws Exception { - String fileUrl = fileUploadService.getFile(fileName).getUrl(); + // 清理文件名,只保留最终文件名 + String cleanFileName = extractFileName(fileName); + String fileUrl = fileUploadService.getFile(fileName).getUrl(); +// fileUrl = URLEncoder.encode(fileUrl, "UTF-8").replaceAll("\\+", "%20"); Map config = new HashMap<>(); // 文档配置 Map document = new HashMap<>(); - document.put("title", fileName); + document.put("title", cleanFileName); document.put("url", fileUrl); document.put("fileType", getFileExtension(fileName)); document.put("key", fileKey); @@ -80,7 +95,10 @@ public class OnlyOfficeService { // 自定义配置 Map customization = new HashMap<>(); - customization.put("forcesave", true); + customization.put("forcesave", false); + customization.put("about", false); + customization.put("feedback", false); + customization.put("hideRightMenu", false); customization.put("compactToolbar", false); editorConfig.put("customization", customization); @@ -158,9 +176,10 @@ public class OnlyOfficeService { } private String buildCallbackUrl() { - return "http://192.168.0.39:" + onlyOfficeConfig.getServerPort() + "/smartBid/documents/callback"; + return "http://" + IpUtils.getHostIp() +":" + onlyOfficeConfig.getServerPort() + "/smartBid/documents/callback"; } + private String getFileExtension(String fileName) { return fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); } @@ -214,4 +233,41 @@ public class OnlyOfficeService { .map(LoginUser::getUsername) .orElse(null); } + + // 提取文件名(去掉路径) + private String extractFileName(String filePath) { + if (filePath == null) return "document"; + int lastSlash = filePath.lastIndexOf("/"); + int lastBackslash = filePath.lastIndexOf("\\"); + int lastIndex = Math.max(lastSlash, lastBackslash); + return lastIndex >= 0 ? filePath.substring(lastIndex + 1) : filePath; + } + /** + * 生成 JWT Token(OnlyOffice 专用格式) + */ + private String generateJwtToken(Map config) { + try { + // OnlyOffice 需要的 payload 结构 + Map payload = new HashMap<>(); + // 复制整个配置到 payload + payload.put("document", config.get("document")); + payload.put("editorConfig", config.get("editorConfig")); + payload.put("documentType", config.get("documentType")); + payload.put("type", config.get("type")); + payload.put("width", config.get("width")); + payload.put("height", config.get("height")); + + // 使用 JWTUtil 生成 token + String token = onlyOfficeJwtService.generateToken(payload); + if (token == null) { + log.error("生成JWT Token失败"); + return null; + } + log.debug("生成的JWT Token: {}", token); + return token; + } catch (Exception e) { + log.error("生成JWT Token异常", e); + return null; + } + } }