项目中需要和第三方平台接口,加了来源IP鉴权功能,测试时发现没有问题,但是部署以后发现存在问题,一直鉴权不通过,一群人抓瞎。
我找到那块的代码,跟了一遍流程发现逻辑没有啥问题,但是最终的结果却还是鉴权不通过,实在有些诡异。其基本逻辑为先取得配置的IP列表,然后通过request.getRemoteAddr()取得客户端的IP地址,做鉴权和校验,逻辑没问题,那么肯定是request.getRemoteAddr()出了问题,google下,发现有人遇到类似的问题。
最终定位为request.getRemoteAddr()这种方法在大部分情况下都是有效的。但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了。
如果使用了反向代理软件,将http://192.168.1.110:2046/ 的URL反向代理为http://www.xxx.com/ 的URL时,用request.getRemoteAddr()方法获取的IP地址是:127.0.0.1 或 192.168.1.110,而并不是客户端的真实IP。
经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的IP,服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息用以跟踪原有的客户端IP地址和原来客户端请求的服务器地址。
原来如此,我们的项目中正好是有前置apache,将一些请求转发给后端的weblogic,看来就是这样导致的咯。
给出一份还算靠谱的代码
改进后的java代码:
class IpUtils {
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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
class IpUtils {
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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
}
PS:可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串ip值,究竟哪个才是真正的用户端的真实IP呢?
答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100,用户真实IP为: 192.168.1.110
================================下面是OSC中的=====================================
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
public final class InternetProtocol {
private static Logger log = Logger.getLogger(InternetProtocol.class);
/**
* 构造函数.
*/
private InternetProtocol() {
}
/**
* 获取客户端IP地址.<br>
* 支持多级反向代理
*
* @param request
* HttpServletRequest
* @return 客户端真实IP地址
*/
public static String getRemoteAddr(final HttpServletRequest request) {
try{
String remoteAddr = request.getHeader("X-Forwarded-For");
// 如果通过多级反向代理,X-Forwarded-For的值不止一个,而是一串用逗号分隔的IP值,此时取X-Forwarded-For中第一个非unknown的有效IP字符串
if (isEffective(remoteAddr) && (remoteAddr.indexOf(",") > -1)) {
String[] array = remoteAddr.split(",");
for (String element : array) {
if (isEffective(element)) {
remoteAddr = element;
break;
}
}
}
if (!isEffective(remoteAddr)) {
remoteAddr = request.getHeader("X-Real-IP");
}
if (!isEffective(remoteAddr)) {
remoteAddr = request.getRemoteAddr();
}
return remoteAddr;
}catch(Exception e){
log.error("get romote ip error,error message:"+e.getMessage());
return "";
}
}
/**
* 获取客户端源端口
* @param request
* @return
*/
public static Long getRemotePort(final HttpServletRequest request){
try{
String port = request.getHeader("remote-port");
if( StringUtil.isNotEmptyString(port )) {
try{
return Long.parseLong(port);
}catch(NumberFormatException ex){
log.error("convert port to long error , port: "+port);
return 0l;
}
}else{
return 0l;
}
}catch(Exception e){
log.error("get romote port error,error message:"+e.getMessage());
return 0l;
}
}
/**
* 远程地址是否有效.
*
* @param remoteAddr
* 远程地址
* @return true代表远程地址有效,false代表远程地址无效
*/
private static boolean isEffective(final String remoteAddr) {
boolean isEffective = false;
if ((null != remoteAddr) && (!"".equals(remoteAddr.trim()))
&& (!"unknown".equalsIgnoreCase(remoteAddr.trim()))) {
isEffective = true;
}
return isEffective;
}
}
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
public final class InternetProtocol {
private static Logger log = Logger.getLogger(InternetProtocol.class);
/**
* 构造函数.
*/
private InternetProtocol() {
}
/**
* 获取客户端IP地址.<br>
* 支持多级反向代理
*
* @param request
* HttpServletRequest
* @return 客户端真实IP地址
*/
public static String getRemoteAddr(final HttpServletRequest request) {
try{
String remoteAddr = request.getHeader("X-Forwarded-For");
// 如果通过多级反向代理,X-Forwarded-For的值不止一个,而是一串用逗号分隔的IP值,此时取X-Forwarded-For中第一个非unknown的有效IP字符串
if (isEffective(remoteAddr) && (remoteAddr.indexOf(",") > -1)) {
String[] array = remoteAddr.split(",");
for (String element : array) {
if (isEffective(element)) {
remoteAddr = element;
break;
}
}
}
if (!isEffective(remoteAddr)) {
remoteAddr = request.getHeader("X-Real-IP");
}
if (!isEffective(remoteAddr)) {
remoteAddr = request.getRemoteAddr();
}
return remoteAddr;
}catch(Exception e){
log.error("get romote ip error,error message:"+e.getMessage());
return "";
}
}
/**
* 获取客户端源端口
* @param request
* @return
*/
public static Long getRemotePort(final HttpServletRequest request){
try{
String port = request.getHeader("remote-port");
if( StringUtil.isNotEmptyString(port )) {
try{
return Long.parseLong(port);
}catch(NumberFormatException ex){
log.error("convert port to long error , port: "+port);
return 0l;
}
}else{
return 0l;
}
}catch(Exception e){
log.error("get romote port error,error message:"+e.getMessage());
return 0l;
}
}
/**
* 远程地址是否有效.
*
* @param remoteAddr
* 远程地址
* @return true代表远程地址有效,false代表远程地址无效
*/
private static boolean isEffective(final String remoteAddr) {
boolean isEffective = false;
if ((null != remoteAddr) && (!"".equals(remoteAddr.trim()))
&& (!"unknown".equalsIgnoreCase(remoteAddr.trim()))) {
isEffective = true;
}
return isEffective;
}
}