spring mvc 獲取nginx下用(yòng)戶真實ip
如果經過多(duō)台服務(wù)器Nginx轉發,獲取的ip内容類似這樣218.5.70.170, 192.168.1.1,所以過濾掉空格取第1個就是。
private String getRemortIP(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();
}
ip = ip.replace(" ", "").trim();
if (StringUtils.isNotBlank(ip)) {
String[] ips = ip.split(",");
ip = ips[0];
}
logService.log(request.getRequestURL().toString(), request.getMethod(), ip, 0);
return ip;
}
- 版權所有(yǒu):奇站網絡 轉載請注明出處
- 廈門奇站網絡科(kē)技(jì )有(yǒu)限公(gōng)司,專業提供網站建設,響應式網站建設,小(xiǎo)程序開發,系統定制開發。
- 軟件開發咨詢熱線(xiàn):吳小(xiǎo)姐 13313868605