获取客户端真实的IP地址
public String getRealIp(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
int index = ip.indexOf(",");
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
}
ip = request.getHeader("X-Real-IP");
if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
return request.getRemoteAddr();
}
获取客户端使用设备类型
public String getClientInfo(HttpServletRequest request) {
String userAgent = request.getHeader("user-agent");
String device = null;
//移动端访问
if (userAgent.contains("Android") || userAgent.contains("iPhone") || userAgent.contains("iPad")){
device = "mobileTerminal";
request.setAttribute("device", device);
}else {
//PC端访问
device = "pcTerminal";
}
return device;
}
Q.E.D.