欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

request获得访问路径,端口,ip等

程序员文章站 2024-02-03 11:25:52
...
 System.out.println(request.getLocalAddr());

 System.out.println(request.getServerName());

 System.out.println(request.getRemoteAddr());//获得访问该服务的客户端的ip地址,大多数时候是真实的,但是当使用了代理,则不正确

 System.out.println(request.getRequestURI());

 System.out.println(request.getRequestURL());

 System.out.println(request.getServerPort());



127.0.0.1

127.0.0.1

127.0.0.1

/certification/ifCert

http://127.0.0.1:8089/certification/ifCert

8089

获得客户端真实ip

//获得客户端真是ip
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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}

 

相关标签: request