LocalhostFilter extends JsonSecurityRequestFilterBase { @Override protected Optional<ErrorResponse> filter(DiscFilterRequest request) { InetAddress remoteAddr = InetAddresses.forString(request.getRemoteAddr()); if (!remoteAddr.isLoopbackAddress() && !request.getRemoteAddr().equals(request.getLocalAddr())) { return Optional.of(new ErrorResponse( Response.Status.UNAUTHORIZED, String.format("%s %s denied for %s: Unauthorized host", request.getMethod(), request.getUri().getPath(), request.getRemoteAddr()))); } return Optional.empty(); }    }
@Test public void filter() { assertUnauthorized(createRequest("1.2.3.4", null)); assertSuccess(createRequest("127.0.0.1", null)); assertSuccess(createRequest("127.127.0.1", null)); assertSuccess(createRequest("0:0:0:0:0:0:0:1", null)); assertSuccess(createRequest("1.3.3.7", "1.3.3.7")); }