JAVA Web開發過程中,很多場景下需要獲取訪問終端的IP,對應方法getRemoteAddr。例如調試過程中本地回環ip地址是127.0.0.1,忽然有一天返回
0:0:0:0:0:0:0:1,這個是IPv6地址,是不是會讓人蒙圈。當前互聯網環境下仍以ipv4為主,大家習慣接受的ipv4地址。
具體原因是因為開發者使用了高版本的操作系統,Win7/Win10等啟用了ipv6,大家需要手工禁止,或者通過參數控制。
常用方法
方法1:修改server.xml
<Connector port="8083" protocol="org.apache.coyote.http11.Http11NioProtocol" address="0.0.0.0" connectionTimeout="20000" redirectPort="8443" />
其中address="0.0.0.0"為后添加部分
方法2(沒詳細驗證): windows
netsh interface teredo set state disabled netsh interface ipv6 6to4 set state state=disabled undoonstop=disabled netsh interface ipv6 isatap set state state=disabled netsh interface IPV6 set global randomizeidentifier=disabled netsh interface IPV6 set privacy state=disable netsh interface ipv6 6to4 set state state=disabled netsh interface ipv6 isatap set state state=disabled netsh interface ipv6 set teredo=disabled
方法3:
在jvm命令行添加以下參數
-Djava.net.preferIPv4Stack=true
linux下禁用ipv6的方法
打開配置文件/etc/sysctl.d/99-sysctl.conf,在下面添加
net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv6.conf.lo.disable_ipv6 = 1
執行 sysctl -p 使配置生效
查看是否生效
cat /proc/sys/net/ipv6/conf/all/disable_ipv6
返回結果如果是1表示禁用