{"id":763,"date":"2020-02-18T14:41:40","date_gmt":"2020-02-18T06:41:40","guid":{"rendered":"http:\/\/prayerlaputa.com\/?p=763"},"modified":"2020-02-18T14:41:40","modified_gmt":"2020-02-18T06:41:40","slug":"spring%e6%a1%86%e6%9e%b6%e4%b8%ad%e8%8e%b7%e5%8f%96%e5%ae%a2%e6%88%b7%e7%ab%af%e7%9a%84%e7%9c%9f%e5%ae%9eip","status":"publish","type":"post","link":"http:\/\/prayerlaputa.com\/?p=763","title":{"rendered":"spring\u6846\u67b6\u4e2d\u83b7\u53d6\u5ba2\u6237\u7aef\u7684\u771f\u5b9eip"},"content":{"rendered":"<p>\u9898\u5916\u8bdd\uff0c\u524d\u7aef\u4e5f\u53ef\u4ee5\u8c03\u7528\u5df2\u6709\u7684\u63a5\u53e3\u83b7\u53d6ip\uff0c\u4f8b\u5982\u8c03\u7528\u641c\u72d0\u63a5\u53e3\u00a0<a href=\"https:\/\/pv.sohu.com\/cityjson?ie=utf-8\">https:\/\/pv.sohu.com\/cityjson?ie=utf-8<\/a><\/p>\n<p>\u5728spring\u6846\u67b6\u4e2d\uff0c\u83b7\u53d6IP\u63a5\u53e3\uff0c\u5219\u9700\u8981\u83b7\u53d6 HttpServletRequest \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u4e2d\u5305\u542b\u4e86\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<p>java\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"language-\" data-role=\"codeBlock\" data-info=\"\"><code>\/** \r\n * @Description: \u83b7\u53d6\u5ba2\u6237\u7aefIP\u5730\u5740   \r\n *\/  \r\nprivate String getIpAddr(HttpServletRequest request) {   \r\n    String ip = request.getHeader(\"x-forwarded-for\");   \r\n    if(ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {   \r\n        ip = request.getHeader(\"Proxy-Client-IP\");   \r\n    }   \r\n    if(ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {   \r\n        ip = request.getHeader(\"WL-Proxy-Client-IP\");   \r\n    }   \r\n    if(ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {   \r\n        ip = request.getRemoteAddr();   \r\n        if(ip.equals(\"127.0.0.1\")){     \r\n            \/\/\u6839\u636e\u7f51\u5361\u53d6\u672c\u673a\u914d\u7f6e\u7684IP     \r\n            InetAddress inet=null;     \r\n            try {     \r\n                inet = InetAddress.getLocalHost();     \r\n            } catch (Exception e) {     \r\n                e.printStackTrace();     \r\n            }     \r\n            ip= inet.getHostAddress();     \r\n        }  \r\n    }   \r\n    \/\/ \u591a\u4e2a\u4ee3\u7406\u7684\u60c5\u51b5\uff0c\u7b2c\u4e00\u4e2aIP\u4e3a\u5ba2\u6237\u7aef\u771f\u5b9eIP,\u591a\u4e2aIP\u6309\u7167','\u5206\u5272  \r\n    if(ip != null &amp;&amp; ip.length() &gt; 15){    \r\n        if(ip.indexOf(\",\")&gt;0){     \r\n            ip = ip.substring(0,ip.indexOf(\",\"));     \r\n        }     \r\n    }     \r\n    return ip;   \r\n}\r\n<\/code><\/pre>\n<p><!--more-->\u6ce8\u610f\uff0c\u6b64\u5904\u83b7\u53d6\u7684\u7b2c\u4e00\u4e2aHTTP header\u5b57\u6bb5<code>x-forwarded-for<\/code>\uff0c\u53ef\u80fd\u88ab\u4f2a\u9020\uff0c\u5177\u4f53\u53c2\u89c1\u8fd9\u7bc7\u6587\u7ae0\uff1a<a href=\"https:\/\/www.cnblogs.com\/diaosir\/p\/6890825.html\">HTTP \u8bf7\u6c42\u5934\u4e2d\u7684 X-Forwarded-For\uff0cX-Real-IP<\/a><\/p>\n<p>\u5bf9\u4e0a\u8ff0\u4ee3\u7801\u505a\u6539\u9020\uff0c\u5373\u53ef\u5f97\u5230\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"language-\" data-role=\"codeBlock\" data-info=\"\"><code>\/** \r\n * @Description: \u83b7\u53d6\u5ba2\u6237\u7aefIP\u5730\u5740   \r\n *\/  \r\nprivate String getIpAddr(HttpServletRequest request) {   \r\n    String ip = request.getHeader(\"x-Real-IP\");   \r\n    if(ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {   \r\n        ip = request.getHeader(\"Proxy-Client-IP\");   \r\n    }   \r\n    if(ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {   \r\n        ip = request.getHeader(\"WL-Proxy-Client-IP\");   \r\n    }   \r\n    if(ip == null || ip.length() == 0 || \"unknown\".equalsIgnoreCase(ip)) {   \r\n        ip = request.getRemoteAddr();   \r\n        if(ip.equals(\"127.0.0.1\")){     \r\n            \/\/\u6839\u636e\u7f51\u5361\u53d6\u672c\u673a\u914d\u7f6e\u7684IP     \r\n            InetAddress inet=null;     \r\n            try {     \r\n                inet = InetAddress.getLocalHost();     \r\n            } catch (Exception e) {     \r\n                e.printStackTrace();     \r\n            }     \r\n            ip= inet.getHostAddress();     \r\n        }  \r\n    }   \r\n    \/\/ \u591a\u4e2a\u4ee3\u7406\u7684\u60c5\u51b5\uff0c\u7b2c\u4e00\u4e2aIP\u4e3a\u5ba2\u6237\u7aef\u771f\u5b9eIP,\u591a\u4e2aIP\u6309\u7167','\u5206\u5272  \r\n    if(ip != null &amp;&amp; ip.length() &gt; 15){    \r\n        if(ip.indexOf(\",\")&gt;0){     \r\n            ip = ip.substring(0,ip.indexOf(\",\"));     \r\n        }     \r\n    }     \r\n    return ip;   \r\n}\r\n<\/code><\/pre>\n<h1 id=\"%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99\" class=\"mume-header\">\u53c2\u8003\u8d44\u6599<\/h1>\n<ul>\n<li><a href=\"https:\/\/www.cnblogs.com\/diaosir\/p\/6890825.html\">HTTP \u8bf7\u6c42\u5934\u4e2d\u7684 X-Forwarded-For\uff0cX-Real-IP<\/a><\/li>\n<li><a href=\"https:\/\/blog.csdn.net\/kioo_i_see\/article\/details\/71630014\">spring\u6846\u67b6\u4e2d\u83b7\u53d6\u8bbf\u95ee\u7aef\u7684ip\u5730\u5740<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u5916\u8bdd\uff0c\u524d\u7aef\u4e5f\u53ef\u4ee5\u8c03\u7528\u5df2\u6709\u7684\u63a5\u53e3\u83b7\u53d6ip\uff0c\u4f8b\u5982\u8c03\u7528\u641c\u72d0\u63a5\u53e3\u00a0https:\/\/pv.sohu.com\/cityj <a href='http:\/\/prayerlaputa.com\/?p=763' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[6,74,20],"tags":[33,37,52],"_links":{"self":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/763"}],"collection":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=763"}],"version-history":[{"count":1,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/763\/revisions"}],"predecessor-version":[{"id":764,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/763\/revisions\/764"}],"wp:attachment":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=763"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}