12345678910111213141516171819202122232425262728293031323334 |
- package com.iamberry.wechat.tools;
- import javax.servlet.http.HttpServletRequest;
- /**
- * @author:何秀刚
- * @description: ip地址工具类
- * @createDate:2016年5月26日
- */
- public class IpAddressUtil {
- /**
- * 获取ip地址, 若多级代理,第一个IP为客户端真实IP,多个IP按照','分割
- * @param request
- * @return
- */
- public static String getIpAddr(HttpServletRequest request) {
- String ip = request.getHeader("x-forwarded-for");
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("Proxy-Client-IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("WL-Proxy-Client-IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getRemoteAddr();
- }
- if (ip != null && ip.indexOf(",") != -1) {
- ip = ip.split(",")[0];
- }
- return ip;
- }
- }
|