Class RedirectSecurity
Authorization
or Cookie) should be forwarded or stripped when a REST call is transparently replayed
against a redirect target (an HTTP 3xx response's Location).
This class contains only the decision — it does not itself intercept, follow, or rewrite any request. Each transport binding (classic, HttpClient-4.5/5.x, Jetty, OkHttp, the JDK client, etc.) is responsible for calling into this class from its own redirect hook and acting on the result.
The policy is intentionally simple and conservative:
- Credentials are forwarded only when the redirect target is the exact same origin
as the original request — same scheme, same host (case-insensitive), and same port
(after applying the standard default port for
http/https). - Credentials are stripped on any other origin change (different scheme, host, or port).
- Credentials are stripped on an
https→httpscheme downgrade, even in the (impossible under the same-origin rule above) case that host and port otherwise match.
Both from and to must be absolute URIs (non-null scheme and host);
callers are responsible for resolving a relative Location header against the original
request URI before calling into this class.
Example:
- Since:
- 10.0.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe outcome of a credential-forwarding decision for a single redirect hop. -
Method Summary
Modifier and TypeMethodDescriptionDecides whether credentials should be forwarded or stripped for a single redirect hop.Returns the lower-cased names of the headers that may be replayed against another origin.static booleanisDowngrade(URI from, URI to) Returnstrueif the redirect represents anhttps→httpscheme downgrade.static booleanisForwardedCrossOrigin(String name) Returns whether a header may be replayed against another origin.static booleanmustStrip(String name, Collection<String> extraForward) Returns whether a header must be removed from a request replayed against another origin.static booleansameOrigin(URI from, URI to) Returnstrueiffromandtoshare the same origin — scheme, host (case-insensitive), and port (after default-port normalization forhttp/https).static booleanshouldStripCredentials(URI from, URI to) Convenience boolean form ofdecide(URI, URI).Returns the well-known credential header names, used to mark them sensitive (for example to keep them out of logs).
-
Method Details
-
sameOrigin
Returnstrueiffromandtoshare the same origin — scheme, host (case-insensitive), and port (after default-port normalization forhttp/https).- Parameters:
from- The original request URI. Must be absolute (non-nullscheme and host).to- The redirect target URI. Must be absolute (non-nullscheme and host).- Returns:
trueif both URIs resolve to the same origin.- Throws:
IllegalArgumentException- If either argument isnullor not an absolute URI.
-
isDowngrade
Returnstrueif the redirect represents anhttps→httpscheme downgrade.- Parameters:
from- The original request URI. Must be absolute (non-nullscheme and host).to- The redirect target URI. Must be absolute (non-nullscheme and host).- Returns:
trueiffromishttpsandtoishttp.- Throws:
IllegalArgumentException- If either argument isnullor not an absolute URI.
-
decide
Decides whether credentials should be forwarded or stripped for a single redirect hop.- Parameters:
from- The original request URI. Must be absolute (non-nullscheme and host).to- The redirect target URI. Must be absolute (non-nullscheme and host).- Returns:
RedirectSecurity.CredentialDecision.STRIPunlesstois the exact same origin asfrom(and not a scheme downgrade), in which caseRedirectSecurity.CredentialDecision.FORWARD.- Throws:
IllegalArgumentException- If either argument isnullor not an absolute URI.
-
shouldStripCredentials
Convenience boolean form ofdecide(URI, URI).- Parameters:
from- The original request URI. Must be absolute (non-nullscheme and host).to- The redirect target URI. Must be absolute (non-nullscheme and host).- Returns:
trueif credentials must be stripped before replaying the request againstto.- Throws:
IllegalArgumentException- If either argument isnullor not an absolute URI.
-
stripOnCrossOrigin
Returns the well-known credential header names, used to mark them sensitive (for example to keep them out of logs).This is not the strip list: a cross-origin redirect removes every header that is not on
forwardOnCrossOrigin().- Returns:
- The set of credential header names.
-
forwardOnCrossOrigin
Returns the lower-cased names of the headers that may be replayed against another origin.Every other caller-set header is removed from a request replayed cross-origin (or across an
https→httpdowngrade). Additional names can be forwarded per client withRestClient.Builder.redirectForwardHeaders(String...).- Returns:
- An unmodifiable set of lower-cased header names.
-
isForwardedCrossOrigin
Returns whether a header may be replayed against another origin.- Parameters:
name- The header name. Compared case-insensitively.- Returns:
true if the header is on the built-in forward list.
-
mustStrip
Returns whether a header must be removed from a request replayed against another origin.- Parameters:
name- The header name.extraForward- Additional header names the caller allows to be forwarded. May benull .- Returns:
true if the header is on neither the built-in nor the caller-supplied forward list.
-