An HTTP redirect is a new request target. A client carrying credentials or a request body needs an explicit rule for which redirect destinations are acceptable.
Java HttpClient redirects: check the next origin before resending
Operational contract
HttpClient can follow redirects automatically, but a controlled client can configure NEVER and inspect the Location header after a 3xx response. The code resolves a relative target and only accepts the same HTTPS scheme, host, and effective port. It returns a URI; the caller must still decide whether the response status permits replaying the original method and body. In particular, 301 and 302 automatic handling may turn POST into GET. Pin a narrower path or allowlist when the application requires it.
Failure case
A shipment API returns a redirect while the caller holds an authorization token. Replaying the request against another host would cross the credential boundary. This gate rejects that move, including a downgrade to plain HTTP. A 307 or 308 response still needs a replayable request body and an idempotency decision before sending again.
Java code
import java.net.URI;
import java.net.http.HttpClient;
import java.util.Locale;
public class ShipmentRedirectGate {
public static HttpClient client() {
return HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NEVER).build();
}
public static URI sameOrigin(URI current, URI location) {
URI next = current.resolve(location);
String oldHost = current.getHost();
String newHost = next.getHost();
int oldPort = current.getPort() == -1 ? 443 : current.getPort();
int newPort = next.getPort() == -1 ? 443 : next.getPort();
if (!"https".equalsIgnoreCase(current.getScheme())
|| !"https".equalsIgnoreCase(next.getScheme())
|| oldHost == null || newHost == null
|| !oldHost.toLowerCase(Locale.ROOT).equals(newHost.toLowerCase(Locale.ROOT))
|| oldPort != newPort) {
throw new IllegalArgumentException("Redirect crosses the configured origin");
}
return next;
}
}Performance and ownership cost
URI resolution and comparison take time proportional to URI length and allocate one URI. Redirect chains add network round trips; cap their count in the caller. A same-origin check does not prove that the destination path is authorized for the same token.
Common Mistakes
- Do not carry credentials to an unapproved origin.
- Do not assume an automatic 301 or 302 preserves POST.
- Do not follow an unlimited chain or replay a non-repeatable body.
Connected lessons
- Java HttpClient: request policy, deadlines, and response size
- Java HTTP retry project: bounded attempts and one wait budget
- Java HTTP query values: encode data without changing URI structure
- Java HttpClient deadlines: separate connection and request timeouts
- Java streaming HTTP bodies: close the stream and cap retained bytes
- Java Retry-After: parse a bounded server delay without inventing a retry
- Java HttpClient cookies: make session storage an owned policy
- Java HTTP and fork/join decisions quiz
- Advanced Java
