系统集成 onlyoffice

This commit is contained in:
cwchen 2025-11-04 16:59:58 +08:00
parent 35245b1a5d
commit 026712b9b7
3 changed files with 65 additions and 7 deletions

View File

@ -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

View File

@ -38,4 +38,6 @@ public class OnlyOfficeJwtService {
return false;
}
}
}

View File

@ -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<String, Object> getConfigWithToken(String fileKey, String fileName, String mode) throws Exception {
Map<String, Object> 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<String, Object> editorConfig = (Map<String, Object>) 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<String, Object> 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<String, Object> config = new HashMap<>();
// 文档配置
Map<String, Object> 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<String, Object> 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 TokenOnlyOffice 专用格式
*/
private String generateJwtToken(Map<String, Object> config) {
try {
// OnlyOffice 需要的 payload 结构
Map<String, Object> 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;
}
}
}