From 193d78d7fb43cf596cd52c16ba288b7a88ba251d Mon Sep 17 00:00:00 2001 From: RuoYi Date: Thu, 17 Jan 2019 15:05:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=AE=8C=E6=95=B4=E7=9A=84?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/framework/config/ServerConfig.java | 35 ++++++++----------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ServerConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ServerConfig.java index bed31e11..cc4045b1 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ServerConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ServerConfig.java @@ -1,10 +1,8 @@ package com.ruoyi.framework.config; -import org.springframework.boot.web.context.WebServerInitializedEvent; -import org.springframework.context.ApplicationListener; +import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Component; -import java.net.InetAddress; -import java.net.UnknownHostException; +import com.ruoyi.framework.util.ServletUtils; /** * 服务相关配置 @@ -13,28 +11,23 @@ import java.net.UnknownHostException; * */ @Component -public class ServerConfig implements ApplicationListener +public class ServerConfig { - private int serverPort; - + /** + * 获取完整的请求路径,包括:域名,端口,上下文访问路径 + * + * @return 服务地址 + */ public String getUrl() { - InetAddress address = null; - try - { - address = InetAddress.getLocalHost(); - } - catch (UnknownHostException e) - { - e.printStackTrace(); - } - return "http://" + address.getHostAddress() + ":" + this.serverPort; + HttpServletRequest request = ServletUtils.getRequest(); + return getDomain(request); } - @Override - public void onApplicationEvent(WebServerInitializedEvent event) + public static String getDomain(HttpServletRequest request) { - this.serverPort = event.getWebServer().getPort(); + StringBuffer url = request.getRequestURL(); + String contextPath = request.getServletContext().getContextPath(); + return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString(); } - }