class="java">/** * 获取当前登录用户的IP地址 * * @param request * @return * @throws Exception */ public static String getIpAddr(final HttpServletRequest request) throws Exception { String ip = request.getHeader("X-Real-IP"); if (!StringUtil.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { return ip; } ip = request.getHeader("X-Forwarded-For"); if (!StringUtil.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) { // 多次反向代理后会有多个IP值,第一个为真实IP。 int index = ip.indexOf(','); if (index != -1) { return ip.substring(0, index); } else { return ip; } } else { return request.getRemoteAddr(); } }