Original link: https://www.zjhuiwan.cn/info/20200330/4006602464505049.html
Recently I met a demand, a website can only access the network, ip need to limit access. Ip is in the part of the article to the site only in the white list is opened. Because the site is static, all articles are static html page. So first thought is to get visitors directly js ip and then determine whether or not in the white list, not in the whitelist, the page does not have permission.
But JS get the network Ip is quite troublesome, finally found a few ways tried not work.
Several methods and methods found at last achieved the record.
JS method for obtaining external network ip:
// The easiest way to get outside the network ip. It can be used directly, but with nothing ..
JS obtain network Ip method: // Some browsers acquired encryption ip of a problem, so when its time
Use WebRTC get in the real network Ip, WebRTC support is a web browser for real-time voice conversations or video conversation API
Since WebRTC in establishing the connection, the local address will be sent to the other party SDP, can be obtained by visiting the visitor's IP SDP
But some browsers impossible, so in this way or give up.
Finally, still feel relatively well implemented in Java, the front page article write ajax, each entry article first determine whether the articles need to restrict access to IP, if you need to request at the back-end, back-end acquisition Ip determine whether the whitelist. Note ajax use synchronization.
Visitors Ip get Java method:
The complete code
The method of the above are from the network, the effective pro-test, recording thereto.
I am just a porter