From d8eb9f12f3f80baae00e41cd3dab1722d209039f Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Thu, 28 Nov 2024 16:37:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(X-Pack):=20=E8=AE=BE=E7=BD=AE=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E8=AE=BF=E9=97=AE=E8=B7=AF=E5=BE=84=E5=90=8E=E5=AF=BC?= =?UTF-8?q?=E5=87=BApdf=E5=A4=B1=E8=B4=A5=20#12945?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/dataease/commons/utils/ServletUtils.java | 17 +++++++++++++++-- .../io/dataease/controller/IndexController.java | 4 ++-- .../service/panel/PanelLinkService.java | 4 ++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/core/backend/src/main/java/io/dataease/commons/utils/ServletUtils.java b/core/backend/src/main/java/io/dataease/commons/utils/ServletUtils.java index 7becef9dd6..ba0874c473 100644 --- a/core/backend/src/main/java/io/dataease/commons/utils/ServletUtils.java +++ b/core/backend/src/main/java/io/dataease/commons/utils/ServletUtils.java @@ -2,7 +2,11 @@ package io.dataease.commons.utils; import io.dataease.commons.constants.AuthConstants; import io.dataease.plugins.common.util.SpringContextUtil; +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; @@ -10,8 +14,18 @@ import javax.servlet.http.HttpServletResponse; import java.net.InetAddress; import java.net.UnknownHostException; +@Component public class ServletUtils { + + @Getter + private static String contextPath; + + @Value("${server.servlet.context-path:#{null}}") + public void setContextPath(String contextPath) { + ServletUtils.contextPath = contextPath; + } + public static HttpServletRequest request() { ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = servletRequestAttributes.getRequest(); @@ -48,8 +62,7 @@ public class ServletUtils { } Environment environment = SpringContextUtil.getBean(Environment.class); Integer port = environment.getProperty("server.port", Integer.class); - return "http://" + hostAddress + ":" + port; + return "http://" + hostAddress + ":" + port + (StringUtils.isBlank(contextPath) ? "" : contextPath); } - } diff --git a/core/backend/src/main/java/io/dataease/controller/IndexController.java b/core/backend/src/main/java/io/dataease/controller/IndexController.java index 73ed1d8858..e01ae194e1 100644 --- a/core/backend/src/main/java/io/dataease/controller/IndexController.java +++ b/core/backend/src/main/java/io/dataease/controller/IndexController.java @@ -30,8 +30,6 @@ public class IndexController { @Resource private PanelLinkService panelLinkService; - @Value("${server.servlet.context-path:#{null}}") - private String contextPath; @GetMapping(value = "/") public String index() { @@ -56,6 +54,7 @@ public class IndexController { } else { url = panelLinkService.getUrlByUuid(index); } + String contextPath = ServletUtils.getContextPath(); if (StringUtils.isNotBlank(contextPath)) { url = contextPath + url; } @@ -85,6 +84,7 @@ public class IndexController { @GetMapping("/tempMobileLink/{id}/{token}") public void tempMobileLink(@PathVariable("id") String id, @PathVariable("token") String token) { String url = "/#preview/" + id; + String contextPath = ServletUtils.getContextPath(); if (StringUtils.isNotBlank(contextPath)) { url = contextPath + url; } diff --git a/core/backend/src/main/java/io/dataease/service/panel/PanelLinkService.java b/core/backend/src/main/java/io/dataease/service/panel/PanelLinkService.java index 566aaa6415..115dcd05b1 100644 --- a/core/backend/src/main/java/io/dataease/service/panel/PanelLinkService.java +++ b/core/backend/src/main/java/io/dataease/service/panel/PanelLinkService.java @@ -36,8 +36,7 @@ public class PanelLinkService { private static final String USERPARAM = "&user="; private static final String SHORT_URL_PREFIX = "/link/"; - @Value("${server.servlet.context-path:#{null}}") - private String contextPath; + @Resource private PanelLinkMapper mapper; @@ -281,6 +280,7 @@ public class PanelLinkService { List mappings = panelLinkMappingMapper.selectByExample(example); PanelLinkMapping mapping = mappings.get(0); String uuid = mapping.getUuid(); + String contextPath = ServletUtils.getContextPath(); return (StringUtils.isNotBlank(contextPath) ? contextPath : "") + SHORT_URL_PREFIX + (StringUtils.isBlank(uuid) ? mapping.getId() : uuid); }