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表示禁用