IPv4AccessControl { public void validate(String clientIP) { try { InetAddress address = InetAddress.getByName(clientIP); if (isLocal(address)) { logger.debug("allow site local client address"); return; } if (!allow(address.getAddress())) { throw new ForbiddenException("access denied", "IP_ACCESS_DENIED"); } } catch (UnknownHostException e) { throw new Error(e); } }  void validate(String clientIP); public IPv4Ranges allow; public IPv4Ranges deny; }
@Test void validateWithAllowedIP() { accessControl.allow = new IPv4Ranges(List.of("100.100.100.100/32")); accessControl.validate("100.100.100.100"); }
@Test void validateWithLocalIP() { accessControl.validate("127.0.0.1"); accessControl.validate("192.168.0.1"); accessControl.validate("10.0.0.1"); accessControl.validate("::1"); }
@Test void validateWithNotAllowedIP() { accessControl.allow = new IPv4Ranges(List.of("100.100.100.100/32")); assertThatThrownBy(() -> accessControl.validate("100.100.100.1")) .isInstanceOf(ForbiddenException.class) .hasMessageContaining("access denied"); }