java服务器获取远端ip地址
public 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.getRemoteAddr();
}
System.out.println("本次登录的用户IP地址:"+ ip);
return ip;
}
这里面的参数request在play!里面怎么传进去,或者play!有它本身获得ip地址的api吗?
传说中的q
11 years, 9 months ago
Answers
Play Framework
中没有
HttpServletRequest
,也不需要
你的代码 可以改成
public String getIpAddr() {
Request req = request();
String ip = req.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getHeader("proxy-client-ip");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getHeader("wl-proxy-client-ip");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.remoteAddress();
}
System.out.println("本次登录的用户IP地址:"+ ip);
return ip;
}
东方八云紫
answered 11 years, 9 months ago