一、通过HttpServletRequest获取IP地址
在Java Web开发中,我们通常会调用HttpServletRequest对象的getRemoteAddr()方法获取客户端的IP地址,具体操作如下:
HttpServletRequest request = ServletActionContext.getRequest();
String ip = request.getRemoteAddr();
但是,在某些情况下,该方法获取的IP地址并不准确。原因是该方法的实现方式是通过获取Http请求的报头信息中的“X-Forwarded-For”字段,如果该字段不存在,则获取“Remote-Addr”字段。而有些代理服务器、CDN等会在报头信息中加入自己的IP地址,因此会影响该方法获取客户端真实IP地址的准确性。
二、通过Servlet过滤器获取IP地址
为了解决HttpServletRequest获取IP地址的不准确性问题,我们可以使用Servlet过滤器,在请求到达Controller之前获取客户端真实IP地址。具体操作如下:
1.编写一个过滤器类IpFilter实现javax.servlet.Filter接口。
public class IpFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
// 获取客户端IP地址
String ip = getIpAddr(req);
// 将客户端IP地址设置到请求属性中
request.setAttribute("clientIp", ip);
// 执行过滤器链
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
/**
* 获取客户端IP地址
* @param request Http请求
* @return 客户端IP地址
*/
private String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
String[] ips = ip.split(",");
for (String tmpIp : ips) {
if (tmpIp.trim().length() != 0 && !"unknown".equalsIgnoreCase(tmpIp)) {
ip = tmpIp.trim();
break;
}
}
}
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();
}
return ip;
}
}
2.在web.xml中配置IpFilter过滤器。
<filter>
<filter-name>IpFilter</filter-name>
<filter-class>com.example.filter.IpFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>IpFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.在Controller中获取客户端IP地址。
public String execute(HttpServletRequest request) {
String ip = (String) request.getAttribute("clientIp");
// 处理业务逻辑
return SUCCESS;
}
三、通过Socket获取IP地址
如果我们是在服务端编程,需要获取客户端的IP地址,也可以通过Socket来获取。
1.服务端代码:
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
Socket socket = serverSocket.accept();
// 获取客户端IP地址
String ip = socket.getInetAddress().getHostAddress();
// 处理请求
}
}
}
2.客户端代码:
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("127.0.0.1", 8080);
// 发送请求到服务端
}
}
四、小结
在Java Web开发中,我们通常会使用HttpServletRequest对象的getRemoteAddr()方法获取客户端IP地址。但是该方法获取的IP地址并不准确,为了解决这个问题,我们可以使用Servlet过滤器,在请求到达Controller之前获取客户端真实IP地址。另外,如果我们是在服务端编程,需要获取客户端的IP地址,也可以通过Socket来获取。