Browse Source

测试获取请求域名端口

dev_shibei_match
wxz 5 years ago
parent
commit
c9e729b7ab
  1. 8
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpContextUtils.java
  2. 5
      epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/test/TestController.java

8
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/utils/HttpContextUtils.java

@ -74,6 +74,12 @@ public class HttpContextUtils {
if (StringUtils.isBlank(scheme)) {
scheme = "http";
}
return String.format("%s://%s:%s", scheme, request.getServerName(), request.getServerPort());
String hostAndPort = request.getHeader("Host-And-Port");
if (StringUtils.isBlank(hostAndPort)) {
hostAndPort = request.getServerName().concat(":").concat(String.valueOf(request.getServerPort()));
}
return String.format("%s://%s", scheme, hostAndPort);
}
}

5
epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/test/TestController.java

@ -1,5 +1,6 @@
package com.epmet.modules.test;
import com.epmet.commons.tools.utils.HttpContextUtils;
import com.epmet.commons.tools.utils.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@ -13,8 +14,8 @@ public class TestController {
@PostMapping("get-req-info")
public Result testDomain(HttpServletRequest request) {
String domain = String.format("%s://%s:%s", request.getHeader("X-Forwarded-Scheme"), request.getServerName(), request.getServerPort());
return new Result().ok(domain);
String requestServerNameAndPort = HttpContextUtils.getRequestServerNameAndPort();
return new Result().ok(requestServerNameAndPort);
}
}

Loading…
Cancel
Save