获取客户端的真实ip,优化获取方式

master
kezhen0805 2 years ago
parent 373d6a9533
commit bbf243dcf7

@ -11,4 +11,7 @@
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="ProjectType">
<option name="id" value="jpab" />
</component>
</project>

@ -52,5 +52,10 @@ public class StandardSubmit {
*/
private int reportState;
/**
*
*/
private String realIP;
//后续再做封装
}

@ -4,7 +4,10 @@ import com.mashibing.api.enums.SmsCodeEnum;
import com.mashibing.api.from.SingleSendForm;
import com.mashibing.api.util.R;
import com.mashibing.api.vo.ResultVO;
import com.mashibing.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
@ -23,8 +26,23 @@ import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/sms")
@Slf4j
@RefreshScope //方便动态加载配置文件
public class SmsController {
@Value("${headers}")
private String headers;
/**
*
*/
private final String UNKNOW = "unknow";
/**
* IPIP
*/
private final String X_FORWARDED_FOR = "x-forwarded-for";
/**
* @param singleSendForm
* @return
@ -43,6 +61,7 @@ public class SmsController {
String ip = this.getRealIP(request);
System.out.println("ip = " + ip);
StandardSubmit standardSubmit = new StandardSubmit();
//===============发送到MQ交给策略模块处理==================
@ -57,27 +76,41 @@ public class SmsController {
*/
private String getRealIP(HttpServletRequest request) {
String ip;
String ips = request.getHeader("x-forwarded-for");
if (StringUtils.isEmpty(ips) || "unknow".equalsIgnoreCase(ips)) {
return ips.substring(0, ips.indexOf(","));
for (String header : headers.split(",")) {
if (!StringUtils.isEmpty(header)) {
ip = request.getHeader(header);
if (!StringUtils.isEmpty(ip) && !UNKNOW.equalsIgnoreCase(ip)) {
if (X_FORWARDED_FOR.equalsIgnoreCase(header) && ip.indexOf(",") > 0) {
ip = ip.substring(0, ip.indexOf(","));
}
return ip;
}
}
}
// 8. 基于传统方式获取IP
ip = request.getRemoteAddr();
ip = request.getHeader("x-real-ip");
if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-client-ip");
}
if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
ip = request.getHeader("wl-proxy-client-ip");
}
if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
//7. 基于其他代理服务器的方式获取请求头的IP地址
ip = request.getHeader("http_client_ip");
}
if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
//8. 基于传统方式获取IP
ip = request.getRemoteAddr();
}
// String ips = request.getHeader("x-forwarded-for");
// if (StringUtils.isEmpty(ips) || "unknow".equalsIgnoreCase(ips)) {
// return ips.substring(0, ips.indexOf(","));
// }
//
// ip = request.getHeader("x-real-ip");
// if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
// ip = request.getHeader("Proxy-client-ip");
//
// }
// if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
// ip = request.getHeader("wl-proxy-client-ip");
// }
// if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
// //7. 基于其他代理服务器的方式获取请求头的IP地址
// ip = request.getHeader("http_client_ip");
// }
// if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
// //8. 基于传统方式获取IP
// ip = request.getRemoteAddr();
// }
return ip;
}

Loading…
Cancel
Save