获取真实Ip地址
/**
* @author zhoulongqin
* @see 获取客户端ip
* @param
* @return 客户端ip(String) webserve ip不一定获取的到
*/
public String getRealIp() {
MessageContext mc = MessageContext.getCurrentContext();
HttpServletRequest request = (HttpServletRequest) mc.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST); String ipAddress = null;
// ipAddress = request.getRemoteAddr();
ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
} } // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
return ipAddress;
}
获取真实Ip地址的更多相关文章
- 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址;
package com.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.htt ...
- JSP 获取真实IP地址的代码
[转载]JSP 获取真实IP地址的代码 JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的. ...
- 【转载】JSP 获取真实IP地址的代码
JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的. 但是在通过了 Apache,Squid ...
- java 获取真实ip地址
/** * 获取真实ip地址 * @param request * @return */ public static String getIpAddress(HttpServletRequest re ...
- C#获取真实IP地址实现方法
通常来说,大家获取用户IP地址常用的方法是: string IpAddress = ""; if((HttpContext.Current.Request.ServerVariab ...
- java 客户端获取真实ip地址
在开发工作中,我们常常需要获取客户端的IP.一般获取客户端的IP地址的方法是:request.getRemoteAddr();但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实 ...
- C#如何获取真实IP地址
大家获取用户IP地址常用的方法是 C# 代码 复制 string IpAddress = ""; if((HttpContext.Current.Request.Serve ...
- php获取真实ip地址(转)
REMOTE_ADDR只能获取访问者本地连接中设置的IP,如中南民族大学校园网中自己设置的10.X.XXX.XXX系列IP,而这个函数获取的是局域网网关出口的IP地址, 如果访问者使用代理服务器,将不 ...
- Nginx反向代理 Laravel获取真实IP地址(PHP)
使用VUE前后端分离开发 后端使用Laravel 想要获取到用户的真实IP地址 因为分离开发不同源跨域问题 所以只能进行前端Nginx反向代理 location /api { rewrite ^/a ...
- 获取客户机MAC地址 根据IP地址 获取机器的MAC地址 / 获取真实Ip地址
[DllImport("Iphlpapi.dll")] private static extern int SendARP(Int32 dest, Int32 host, ref ...
随机推荐
- 无法识别的属性“targetFramework
出现这个错误的原因是NET Framework 版本版本号不对应,iis和网站使用的一致版本就可以了.
- jython安装与配置
安装jython 0. 计算机中要安装jdk 1. 在官网www.jython.org上找到下载页面,然后下载jython-installe 2. 在cmd.exe中运行java -jar jytho ...
- 1.UI初认识
前节:app是什么? app英文全称:application 应用程序,简称应用.也就是手机应用的简写 出处:http://www.cnblogs.com/mcj-coding/p/5098254.h ...
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds -----》myeclipse2015
错误:Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 错误提示就是我们限定了部署的时间导致的错 ...
- JS中多种方式创建对象
1.内置对象创建 var girl=new Object(); girl.name='hxl'; console.log(typeof girl); 2.工厂模式,寄生构造函数模式 function ...
- 51. N-Queens
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- sql对于between和时间
在写时间条件 ,比如 把2014/3/1 到2014/3/31这个时间段做为条件 的话,很多人都会写成这样 select date from table where date between '201 ...
- ajax完整格式
$.ajax({ url: encodeURI( "@Url.Action("NavList", "Home")" ...
- js获取?后面具体参数的值
function getURLParam(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' ...
- .NET 二维码生成(ThoughtWorks.QRCode)
引用ThoughtWorks.QRCode.dll (源代码里有) 1.简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string n ...