Spring 获取客户端IP地址

 HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

            po.setIp(IpUtil.clientIp(req));

IpUtil.java

 

Java代码 

 

  1. public class IpUtil {  
  2.     private static final Logger logger = LoggerFactory.getLogger(IpUtil.class);  
  3.   
  4.     /** 
  5.      * 获取的是本地的IP地址 
  6.      * 
  7.      * @return 
  8.      */  
  9.     public static String serviceIp() {  
  10.         String result = "";  
  11.         try {  
  12.             InetAddress address = InetAddress.getLocalHost();  
  13.             result = address.getHostAddress();  
  14.         } catch (UnknownHostException e) {  
  15.             logger.error("------>调用 IpUtil.servicerIp,错误信息如下");  
  16.             logger.error(e.getMessage(), e);  
  17.         }  
  18.         return result;  
  19.     }  
  20.   
  21.     /** 
  22.      * 获取的是该网站的ip地址,比如我们所有的请求都通过nginx的,所以这里获取到的其实是nginx服务器的IP地址 
  23.      * 
  24.      * @param domain 
  25.      * @return 
  26.      */  
  27.     public static String serviceIp(String domain) {  
  28.         String result = "";  
  29.         if (ValidatorUtil.isNotNullOrEmpty(domain)) {  
  30.             try {  
  31.                 InetAddress inetAddress = InetAddress.getByName(domain);  
  32.                 result = inetAddress.getHostAddress();  
  33.             } catch (UnknownHostException e) {  
  34.                 logger.error("------>调用 IpUtil.servicerIp,错误信息如下");  
  35.                 logger.error(e.getMessage(), e);  
  36.             }  
  37.         }  
  38.         return result;  
  39.     }  
  40.   
  41.     /** 
  42.      * 根据主机名返回其可能的所有Ip地址 
  43.      * 
  44.      * @param domain 
  45.      * @return 
  46.      */  
  47.     public static List<String> originalServiceIp(String domain) {  
  48.         List<String> result = new ArrayList<>();  
  49.         if (ValidatorUtil.isNotNullOrEmpty(domain)) {  
  50.             try {  
  51.                 InetAddress[] addresses = InetAddress.getAllByName(domain);  
  52.                 for (InetAddress addr : addresses) {  
  53.                     result.add(addr.getHostAddress());  
  54.                 }  
  55.             } catch (UnknownHostException e) {  
  56.                 logger.error("------>调用 IpUtil.originalServicerIp,错误信息如下");  
  57.                 logger.error(e.getMessage(), e);  
  58.             }  
  59.         }  
  60.         return result;  
  61.     }  
  62.   
  63.     /** 
  64.      * @param request 
  65.      * @return 
  66.      * @description: 如果通过了多级反向代理的话, 
  67.      * X-Forwarded-For的值并不止一个, 而是一串IP值, 
  68.      * 究竟哪个才是真正的用户端的真实IP呢? 
  69.      * 答案是取 X-Forwarded-For中第一个非unknown的有效IP字符串。 
  70.      */  
  71.     public static String clientIp(HttpServletRequest request) {  
  72.         String ip = request.getHeader("x-forwarded-for");  
  73.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  74.             ip = request.getHeader("Proxy-Client-IP");  
  75.         }  
  76.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  77.             ip = request.getHeader("WL-Proxy-Client-IP");  
  78.         }  
  79.         if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
  80.             ip = request.getRemoteAddr();  
  81.         }  
  82.         return ip;  
  83.     }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值