java获取登录内网ip地址
Posted 未来.....
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java获取登录内网ip地址相关的知识,希望对你有一定的参考价值。
java获取登录内网ip地址
最近好多小伙伴在和我说怎么java怎么获取登录的ip地址,今天就分享一下我的方法了。
1、直接获取ip地址
//获取request请求
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//获取IP地址
String ipaddr = request.getRemoteAddr();
if(ipaddr == "0:0:0:0:0:0:0:1")
ipaddr ="127.0.0.1";
System.out.println(ipaddr);
2、第二种封装成util工具类
public class IPUtil
/**
* 获取ip
*/
public static String getIp(HttpServletRequest request)
if (request == null)
return "";
String ip = request.getHeader("X-Requested-For");
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip))
ip = request.getHeader("X-Forwarded-For");
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip))
ip = request.getHeader("Proxy-Client-IP");
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip))
ip = request.getHeader("WL-Proxy-Client-IP");
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip))
ip = request.getHeader("HTTP_CLIENT_IP");
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip))
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
if (StringUtils.isEmpty(ip) || "unknown".equalsIgnoreCase(ip))
ip = request.getRemoteAddr();
if ("0:0:0:0:0:0:0:1".equals(ip))
return "127.0.0.1";
return ip;
public static boolean isLocalHost(String ip)
return "127.0.0.1".equals(ip) || "localhost".equals(ip);
如果这篇文章帮助了你,记得点个赞哦!!!
以上是关于java获取登录内网ip地址的主要内容,如果未能解决你的问题,请参考以下文章