工具类封装实战(二)ip工具类

举报
小鲍侃java 发表于 2021/09/10 01:01:50 2021/09/10
【摘要】 private static final String UNKNOWN = "unknown"; //本地IP private static final String LOCAL_IP = "127.0.0.1"; //服务器IP private static String SERVER_IP = null; ...

  
  1. private static final String UNKNOWN = "unknown";
  2. //本地IP
  3. private static final String LOCAL_IP = "127.0.0.1";
  4. //服务器IP
  5. private static String SERVER_IP = null;
  6. /**
  7. * 获取客户单IP地址
  8. */
  9. public static String getClientIp(HttpServletRequest request) {
  10. String ip = request.getHeader("x-forwarded-for");
  11. if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
  12. ip = request.getHeader("Proxy-Client-IP");
  13. }
  14. if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
  15. ip = request.getHeader("WL-Proxy-Client-IP");
  16. }
  17. if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
  18. ip = request.getHeader("HTTP_CLIENT_IP");
  19. }
  20. if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
  21. ip = request.getHeader("HTTP_X_FORWARDED_FOR");
  22. }
  23. if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
  24. ip = request.getRemoteAddr();
  25. }
  26. if (StringUtils.equalsIgnoreCase("0:0:0:0:0:0:0:1", ip)) {
  27. ip = LOCAL_IP;
  28. }
  29. return ip;
  30. }
  31. /**
  32. * @param ip
  33. * @return
  34. */
  35. public static boolean noInternet(String ip) {
  36. return !isInternet(ip);
  37. }
  38. /**
  39. * 判定是否是内网地址
  40. *
  41. * @param ip
  42. * @return
  43. */
  44. public static boolean isInternet(String ip) {
  45. if (StringUtils.isEmpty(ip)) {
  46. return false;
  47. }
  48. if (StringUtils.equals("0:0:0:0:0:0:0:1", ip)) {
  49. return true;
  50. }
  51. Pattern reg = Pattern.compile("^(127\\.0\\.0\\.1)|(localhost)|(10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})|(172\\.((1[6-9])|(2\\d)|(3[01]))\\.\\d{1,3}\\.\\d{1,3})|(192\\.168\\.\\d{1,3}\\.\\d{1,3})$");
  52. Matcher match = reg.matcher(ip);
  53. return match.find();
  54. }
  55. /**
  56. * 获取服务器端的IP
  57. */
  58. public static String getServerIp() {
  59. if (StringUtils.isNotEmpty(SERVER_IP)) {
  60. return SERVER_IP;
  61. }
  62. try {
  63. Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
  64. while (allNetInterfaces.hasMoreElements()) {
  65. NetworkInterface netInterface = allNetInterfaces.nextElement();
  66. String name = netInterface.getName();
  67. if (!StringUtils.contains(name, "docker") && !StringUtils.contains(name, "lo")) {
  68. Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
  69. while (addresses.hasMoreElements()) {
  70. InetAddress ip = addresses.nextElement();
  71. //loopback地址即本机地址,IPv4的loopback范围是127.0.0.0 ~ 127.255.255.255
  72. if (ip != null
  73. && !ip.isLoopbackAddress()
  74. && !ip.getHostAddress().contains(":")) {
  75. SERVER_IP = ip.getHostAddress();
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. } catch (Exception e) {
  82. SERVER_IP = LOCAL_IP;
  83. }
  84. return SERVER_IP;
  85. }

文章来源: baocl.blog.csdn.net,作者:小黄鸡1992,版权归原作者所有,如需转载,请联系作者。

原文链接:baocl.blog.csdn.net/article/details/113336607

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。