https://github.com/thewaterfall/ip4j
Ip4J is a small, simple and zero dependencies Java 11+ library for retrieving a request IP address from an HttpServletRequest. It supports various headers commonly used in web applications to forward client IP addresses.
https://github.com/thewaterfall/ip4j
client-ip ip ip-address-lookup java
Last synced: 12 months ago
JSON representation
Ip4J is a small, simple and zero dependencies Java 11+ library for retrieving a request IP address from an HttpServletRequest. It supports various headers commonly used in web applications to forward client IP addresses.
- Host: GitHub
- URL: https://github.com/thewaterfall/ip4j
- Owner: thewaterfall
- License: mit
- Created: 2025-02-01T11:57:11.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-07T09:19:58.000Z (over 1 year ago)
- Last Synced: 2025-03-28T06:31:25.493Z (about 1 year ago)
- Topics: client-ip, ip, ip-address-lookup, java
- Language: Java
- Homepage:
- Size: 72.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ip4j
[](https://jitpack.io/#thewaterfall/ip4j)
Ip4j is a small, simple and zero dependencies Java 11+ library for retrieving a request IP address from an
`HttpServletRequest`. It supports various headers commonly used in web applications to forward client IP addresses.
## Features
Extracts IP address from multiple sources by the following order (higher to lower priority):
- `Custom user headers` (optional user-provided headers)
- `X-Client-IP header`
- `X-Forwarded-For header` (if multiple comma-separated IPs found - first one is returned)
- `CF-Connecting-IP header` (Cloudflare)
- `Fastly-Client-Ip header` (Fastly CDN)
- `True-Client-Ip header` (Akamai, Cloudflare)
- `X-Real-IP header` (Nginx, FastCGI)
- `X-Cluster-Client-IP header` (Rackspace, Riverbed Stingray)
- `X-Forwarded header` (if multiple comma-separated IPs found - first one is returned)
- `Forwarded-For header` (if multiple comma-separated IPs found - first one is returned)
- `Forwarded header` (first "for" directive is used, if multiple comma-separated IPs found - first one is returned)
- `appengine-user-ip header` (Google App Engine)
- `Cf-Pseudo-IPv4 header` (Cloudflare fallback)
- `request.getRemoteAddr()`
## Installation
Ip4j can be easily installed using JitPack, see Gradle and Maven examples below.
### Gradle
Add the following to your build.gradle file:
```
repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}
dependencies {
implementation 'com.github.thewaterfall:ip4j:1.0.0'
}
```
### Maven
Add the following to your pom.xml file:
```
jitpack.io
https://jitpack.io
com.github.thewaterfall
ip4j
1.0.0
```
## Usage
```
String ip = Ip4j.getIp(request);
// With custom headers
String ip = Ip4j.getIp(request, "X-Ip");
String ip = Ip4j.getIp(request, "X-Ip", "My-Ip", "IPs");
```