1.相关nginx配置(通过header将客户端ip,host等信息传入)
location ~ .*.do$
{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass_header Set-Cookie;
proxy_pass_header P3P;
proxy_pass http://127.0.0.1:8090;
}
2.获取ip, 主要通过分析request中的header信息
public static String getClientIP(HttpServletRequest request) {
String ip = request.getHeader("X-Real-IP");
if (isBlank(ip) || isIPUnknown(ip)) {
ip = request.getHeader("X-Forwarded-For");
}
if (isBlank(ip) || isIPUnknown(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (isBlank(ip) || isIPUnknown(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP"); //WebLogic
}
if (isBlank(ip) || isIPUnknown(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
private static boolean isIPUnknown(String ip) {
return "unknown".equalsIgnoreCase(ip);
}
public static boolean isBlank(String s) {
return s == null || s.trim().length() == 0;
}