Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A JAX-RS service behind a load balancer generates absolute URLs with http:// and the wrong host, and its session cookie is sometimes not sent by browsers in cross-site iframes. What is going on and how do you fix both?

Both symptoms come from what the service sees versus what the browser sees. Behind the load balancer, UriInfo builds URLs from the internal request, plain HTTP to an internal host, unless the container or a @PreMatching filter honours X-Forwarded-Proto and X-Forwarded-Host (or Forwarded) from trusted proxies only. And browsers treat cookies without a SameSite attribute as Lax, so a session cookie inside a cross-site iframe needs SameSite=None; Secure, with its CSRF and privacy costs, and third-party cookie blocking may still stop it, which often makes a token-based design better for embedded use.

Why this question is asked

These two problems tend to show up together the first time a service moves behind a load balancer and gets embedded in a partner's page. Neither is a bug in our code exactly; both come from the gap between the request our service receives and the request the browser made. Interviewers ask this scenario to see whether we can reason about proxies and browser cookie rules instead of guessing. In this answer, we will recap what a proxy changes, explain the wrong URLs and fix them safely, then explain the missing cookie, fix it, discuss why the fix may not be enough, and finish with how to test it properly.

1A Quick Recap of What the Load Balancer Changes

A load balancer or reverse proxy accepts the browser's connection and opens its own connection to our service. Often it ends TLS itself, so the browser speaks https to shop.example.com while our service receives plain http from the proxy at an address such as 10.0.3.17:8080. To tell the service what the original request looked like, proxies add headers: X-Forwarded-Proto: https, X-Forwarded-Host: shop.example.com and X-Forwarded-For with the client IP, or the standardised Forwarded header (RFC 7239), which carries all three.

2Why the Absolute URLs Are Wrong

JAX-RS builds absolute URLs from UriInfo, the object that describes the current request's base and request URIs. For example, a POST that creates an order returns a Location header built like this.

Java
@POST
public Response create(OrderRequest req, @Context UriInfo uriInfo) {
Order o = orders.create(req);
URI location = uriInfo.getAbsolutePathBuilder().path(String.valueOf(o.id())).build();
return Response.created(location).build();
}
// Browser called: https://shop.example.com/api/orders
// Service received: http://10.0.3.17:8080/api/orders
// Location: http://10.0.3.17:8080/api/orders/42 <- wrong scheme and host

UriInfo has no way to know the original scheme and host unless something tells it. The container builds the request URI from the connection it accepted and the Host header it received, and the forwarded headers are just headers that nobody reads by default. So the fix is to make the container, or our own early filter, apply them.

3Fixing the URLs, but Only for Trusted Proxies

The best place is the container, because then everything, including redirects and servlet code, sees the corrected values. Tomcat has RemoteIpValve (with protocolHeader and hostHeader settings), Jetty has ForwardedRequestCustomizer, and Undertow in WildFly has proxy-address-forwarding. Spring Boot exposes server.forward-headers-strategy. Exact options differ, so check your version. If the container cannot do it, a @PreMatching request filter can rewrite the URIs before resource matching. @PreMatching matters here because it is the only kind of filter allowed to change the request URI.

Java
@Provider
@PreMatching
@Priority(Priorities.AUTHENTICATION - 100)
public class ForwardedHeadersFilter implements ContainerRequestFilter {
private static final Set<String> TRUSTED = Set.of("10.0.1.10", "10.0.1.11"); // the load balancers
@Context HttpServletRequest servletRequest;
@Override
public void filter(ContainerRequestContext ctx) {
if (!TRUSTED.contains(servletRequest.getRemoteAddr())) return; // ignore headers from anyone else
String proto = ctx.getHeaderString("X-Forwarded-Proto"); // "https"
String host = ctx.getHeaderString("X-Forwarded-Host"); // "shop.example.com"
if (proto == null && host == null) return;
UriInfo u = ctx.getUriInfo();
UriBuilder b = u.getBaseUriBuilder();
if (proto != null) b.scheme(proto);
if (host != null) b.host(host).port(-1); // drop the internal :8080
URI base = b.build();
URI request = UriBuilder.fromUri(u.getRequestUri())
.scheme(base.getScheme()).host(base.getHost()).port(-1).build();
ctx.setRequestUri(base, request);
}
}
// Location: https://shop.example.com/api/orders/42

The trusted-proxy check is the important line. If any client can send X-Forwarded-Host and we believe it, an attacker can make our service generate links to their own domain, for example in a password reset email, which is called host header injection. So forwarded headers are honoured only when the connection comes from our own proxies, and the proxy itself should overwrite these headers rather than append to what the client sent.

4Why the Session Cookie Disappears in the Iframe

Now the cookie. Our page is embedded in an iframe on partner.example.org, so requests from the iframe to our service are cross-site. Browsers treat a cookie with no SameSite attribute as SameSite=Lax, and Lax cookies are not sent on cross-site subrequests, which includes requests made inside a cross-site iframe. That explains sometimes: it works when the user opens our site directly and fails when it is embedded.

The cookie only travels in that context if it says SameSite=None, and browsers require None to be combined with Secure, so it must be served over HTTPS.

Will the browser send the session cookie?
Cookie settingDirect visitCross-site iframe
no SameSite (treated as Lax)yesno
SameSite=Laxyesno
SameSite=Strictyesno
SameSite=None; Secureyesyes, unless third-party cookies are blocked
SameSite=None without Securerejected by the browserrejected by the browser
Only None with Secure travels into a cross-site iframe, and even that is not guaranteed.

5The Cost of SameSite=None and the Better Design

Setting SameSite=None; Secure on the session cookie fixes the iframe for many users, but it has a price. Lax was protecting us against cross-site request forgery, where another site makes the browser send a request with our cookie. With None, we must add our own CSRF protection, such as a per-request token or a check of the Origin header. It also makes the cookie a third-party cookie in the iframe, which has privacy implications and, more practically, is blocked by Safari and Firefox by default and increasingly restricted in Chrome; partitioned cookies (the CHIPS Partitioned attribute) can help, but support varies, so check current browser behaviour.

For an embedded use case, the sturdier design is not to depend on a third-party cookie at all. The iframe app obtains a short-lived access token, for example through an OAuth flow or a token passed by the partner's backend, keeps it in memory, and sends it in an Authorization header. Headers are not subject to cookie rules, so the browser's privacy settings no longer decide whether our app works.

6Testing With the Real Proxy Chain and Browsers

Both bugs passed testing because tests called the service directly. The test plan must reproduce the real path: a staging environment with the same load balancer configuration, an automated check that a POST through the load balancer returns a Location starting with https://shop.example.com, a check that a direct call with a forged X-Forwarded-Host is ignored, and browser tests (Playwright, for example) of the embedded page in Chrome, Firefox and Safari with default privacy settings.

7Follow-up questions

Why not just hard-code the public base URL in configuration?

It works for a service with one public name, and it is safe from header spoofing. It breaks when the same service is reached through several hostnames or both internally and externally, and it means every environment needs the right value. Honouring forwarded headers from trusted proxies adapts to each request, so I prefer it, with a configured value as a fallback.

Should we return relative URLs instead?

For Location headers, relative URIs are allowed by the current HTTP specification and avoid the problem entirely. But links in emails, in API responses consumed by other systems, and OAuth redirect URIs must be absolute, so we still need correct forwarded header handling.

8Conclusion

In this answer, we saw that the wrong URLs come from UriInfo building them from the internal request the proxy made, and that the fix is to honour X-Forwarded-Proto and X-Forwarded-Host, or Forwarded, in the container or a @PreMatching filter, but only from trusted proxies. The missing cookie comes from browsers defaulting to SameSite Lax, which blocks cookies in cross-site iframes; SameSite=None; Secure fixes that at the cost of CSRF protection and privacy, and third-party cookie blocking may still stop it, so a token-based design is safer for embedded use. Finally, both bugs need tests through the real proxy and real browsers.

What to say in the interviewAbout 80 seconds aloud

Open with

There are two separate problems, and both come from the difference between what the browser did and what our service sees. For the URLs: the load balancer terminates TLS and calls us over plain HTTP on an internal host, so UriInfo, which builds absolute URIs from the request it receives, produces http and the internal host.

Then, if they want more

The fix is to process X-Forwarded-Proto and X-Forwarded-Host, or the Forwarded header, ideally in the container with something like Tomcat's RemoteIpValve, or in a PreMatching request filter that rewrites the base and request URIs. Crucially, only from trusted proxy addresses, otherwise anyone can inject a host into our links.

For the cookie: browsers treat cookies without SameSite as Lax, and Lax cookies are not sent from a cross-site iframe. To work there the cookie needs SameSite None plus Secure, which means I need explicit CSRF protection, and it becomes a third-party cookie, which Safari and Firefox block by default anyway.

So for embedded use I would rather move to a short-lived token in an Authorization header. And I would test both through the real load balancer and in real browsers, because direct calls hide both bugs.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Jakarta EE specifications
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A JAX-RS service behind a load balancer generates absolute URLs with http:// and the...": https://scrivio-interview-library.netlify.app/java/jax-rs/0914bfae4f/