liusheng
2024-08-28 bedd0a1cef215538df64470df6b8d4a022189136
ruoyi-framework/src/main/java/com/ruoyi/framework/config/ServerConfig.java
@@ -1,6 +1,8 @@
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;
@@ -10,23 +12,40 @@
 * @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;
    }
}