Java服务器获取客户端的ip详解编程语言

[Java]代码    

	/** 
	 * 获取登录用户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.equals("0:0:0:0:0:0:0:1")) { 
			ip = "本地"; 
		} 
		return ip; 
	}

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10384.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论