JAVA获取当前域名

谁借莪1个温暖的怀抱¢ 2023-06-30 02:29 34阅读 0赞

eg:前端node代理,域名:

  1. http://localhost:3000
  2. 后端接口域名
  3. http://192.168.0.123:8080

获取方法

  1. request.getScheme();//-->http
  2. request.getServerName();//-->localhost
  3. request.getServerPort();//-->3000
  4. request.getLocalAddr();//-->192.168.0.123
  5. request.getLocalPort();//-->8080

根据需求拼起来即可

获取请求IP,因为存在网关或者nginx代理 后端根据上面的代码无论谁请求获取到的都是代理服务器的IP 下面代码获取请求者IP非代理IP

  1. /**
  2. * 最终获取请求者IP
  3. */
  4. def getCurId(request){
  5. String ip = request.getHeader("x-forwarded-for");
  6. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  7. ip = request.getHeader("Proxy-Client-IP");
  8. }
  9. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  10. ip = request.getHeader("WL-Proxy-Client-IP");
  11. }
  12. if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
  13. ip = request.getRemoteAddr();
  14. }
  15. return ip;
  16. }

发表评论

表情:
评论列表 (有 0 条评论,34人围观)

还没有评论,来说两句吧...

相关阅读