| | |
| | | package com.ruoyi.framework.config; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | import com.ruoyi.common.utils.ServletUtils; |
| | | |
| | | /** |
| | | * 服务相关配置 |
| | | * |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @Component |
| | | public class ServerConfig |
| | | { |
| | | public class ServerConfig { |
| | | @Value("${server.port}") |
| | | private String port; |
| | | |
| | | /** |
| | | * 获取完整的请求路径,包括:域名,端口,上下文访问路径 |
| | | * |
| | | * |
| | | * @return 服务地址 |
| | | */ |
| | | public String getUrl() |
| | | { |
| | | public String getUrl() { |
| | | HttpServletRequest request = ServletUtils.getRequest(); |
| | | return getDomain(request); |
| | | String domain = getDomain(request); |
| | | //修改一个端口 |
| | | domain = replaceAfterSecondColon(domain, port); |
| | | return domain; |
| | | } |
| | | |
| | | public static String getDomain(HttpServletRequest request) |
| | | { |
| | | public static String getDomain(HttpServletRequest request) { |
| | | StringBuffer url = request.getRequestURL(); |
| | | String contextPath = request.getServletContext().getContextPath(); |
| | | return url.delete(url.length() - request.getRequestURI().length(), url.length()).append(contextPath).toString(); |
| | | } |
| | | |
| | | public static String replaceAfterSecondColon(String str, String replacement) { |
| | | int firstColonIndex = str.indexOf(":"); |
| | | if (firstColonIndex != -1) { |
| | | int secondColonIndex = str.indexOf(":", firstColonIndex + 1); |
| | | if (secondColonIndex != -1 && secondColonIndex + 1 < str.length()) { |
| | | // 获取第二个“:”前的部分 |
| | | String beforeSecondColon = str.substring(0, secondColonIndex + 1); |
| | | // 返回替换后的字符串 |
| | | return beforeSecondColon + replacement; |
| | | } |
| | | } |
| | | return str; |
| | | } |
| | | } |