{"id":1409,"date":"2023-03-19T11:35:16","date_gmt":"2023-03-19T03:35:16","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1409"},"modified":"2023-04-28T21:07:59","modified_gmt":"2023-04-28T13:07:59","slug":"java-determines-whether-the-ip-address-is-within-cidr-range","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/19\/java-determines-whether-the-ip-address-is-within-cidr-range\/","title":{"rendered":"Java\u5224\u65adIP\u5730\u5740\u662f\u5426\u5728CIDR\u8303\u56f4\u5185"},"content":{"rendered":"<h2>Java\u539f\u751f\u7b97\u6cd5<\/h2>\n<pre><code class=\"language-java\">\/\/\u6838\u5fc3\u4ee3\u7801\uff0c\u68c0\u7d22IP\u6240\u5c5e\u7f51\u6bb5\npublic boolean isInRange(String ip, String cidr) {\n    String[] ips = ip.split(&quot;\\\\.&quot;);\n    long ipAddr = (Long.parseInt(ips[0]) &lt;&lt; 24)\n            | (Long.parseInt(ips[1]) &lt;&lt; 16)\n            | (Long.parseInt(ips[2]) &lt;&lt; 8)\n            | Long.parseInt(ips[3]);\n    long type = Long.parseInt(cidr.replaceAll(&quot;.*\/&quot;, &quot;&quot;));\n    long mask = 0xFFFFFFFF &lt;&lt; (32 - type);\n    String cidrIp = cidr.replaceAll(&quot;\/.*&quot;, &quot;&quot;);\n    String[] cidrIps = cidrIp.split(&quot;\\\\.&quot;);\n    long networkIpAddr = (Long.parseInt(cidrIps[0]) &lt;&lt; 24)\n            | (Long.parseInt(cidrIps[1]) &lt;&lt; 16)\n            | (Long.parseInt(cidrIps[2]) &lt;&lt; 8)\n            | Long.parseInt(cidrIps[3]);\n    return (ipAddr &amp; mask) == (networkIpAddr &amp; mask);\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<pre><code>System.out.println(isInRange(&quot;192.168.1.129&quot;, &quot;192.168.1.132\/25&quot;));<\/code><\/pre>\n<h2>Apache\u5e93<\/h2>\n<pre><code class=\"language-java\">\/\/\u4e5f\u53ef\u4ee5\u4f7f\u7528apache\u7684net\u5de5\u5177\u7c7b\npublic boolean isInRange(String ip, String network) {\n    SubnetInfo subnetInfo = new SubnetUtils(network).getInfo();\n    return subnetInfo.isInRange(ip);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Java\u539f\u751f\u7b97\u6cd5 \/\/\u6838\u5fc3\u4ee3\u7801\uff0c\u68c0\u7d22IP\u6240\u5c5e\u7f51\u6bb5 public boolean isInRange(Strin [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[236],"class_list":["post-1409","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-ip"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1409"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1409\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}