Java
Check and report with java.net.http, no extra dependencies.
Java 11 and later ship an HTTP client, so no extra dependency is needed to call the API. The examples parse JSON with Jackson, which most applications already have.
Check and report
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import static java.nio.charset.StandardCharsets.UTF_8;
public final class IsIPsafe {
private static final String BASE = "https://isipsafe.com/api/v1";
private static final HttpClient HTTP = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(2)).build();
private static final ObjectMapper JSON = new ObjectMapper();
private final String key;
public IsIPsafe(String key) { this.key = key; }
/** Abuse confidence from 0 to 100; 0 if the lookup fails. */
public int score(String ip) {
try {
HttpRequest req = HttpRequest.newBuilder(URI.create(BASE + "/check?ip=" + URLEncoder.encode(ip, UTF_8)))
.timeout(Duration.ofSeconds(3))
.header("Authorization", "Bearer " + key)
.GET().build();
HttpResponse<String> res = HTTP.send(req, HttpResponse.BodyHandlers.ofString());
if (res.statusCode() != 200) return 0;
return JSON.readTree(res.body()).path("data").path("abuseConfidenceScore").asInt(0);
} catch (Exception e) {
return 0; // fail open
}
}
public void report(String ip, String categories, String comment) throws Exception {
String form = "ip=" + URLEncoder.encode(ip, UTF_8)
+ "&categories=" + URLEncoder.encode(categories, UTF_8)
+ "&comment=" + URLEncoder.encode(comment, UTF_8);
HttpRequest req = HttpRequest.newBuilder(URI.create(BASE + "/report"))
.timeout(Duration.ofSeconds(5))
.header("Authorization", "Bearer " + key)
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString(form)).build();
HTTP.send(req, HttpResponse.BodyHandlers.discarding()); // 429: already reported in the last 15 minutes
}
}
In Spring
Register the class as a bean, cache score with @Cacheable (an hour is plenty), and check it in a HandlerInterceptor or a Spring Security AuthenticationFailureHandler on the login endpoint. Report from the failure handler once an address has failed a number of times.
Need a hand?
If something here does not work with your version of Java, or you have improved it, tell us. We update these pages with what people send.
Java and its logo are trademarks of their respective owners. isipsafe is not affiliated with or endorsed by them.