Redirect to different URL with custom headers

47 views Asked by At

I manage two web applications: AppA, which provides a user interface, and AppB, which serves as an engine for configuring rules. Both applications use single sign-on (SSO) authentication, where the user's login credentials are handled via headers. Specifically, the presence of the "user_id" header is required for authentication.

When a user logs into AppA using their credentials and clicks a button to access AppB, the request to AppB must include the "user_id" header for authentication. However, I'm facing challenges in achieving this authentication flow.

I've explored various approaches but haven't found a solution yet. Using XMLHttpRequest in JavaScript, I observed that the request receives a response code of 200 when inspecting it in the browser's developer tools. However, despite this response, AppB fails to load in the browser.

Additionally, I attempted to use RedirectView, as shown in the code snippet below. However, this approach resulted in the request being redirected to a URL appended with "?hello=world", whereas the desired behavior is to send the URL with the header "hello: world".

    public RedirectView redirectWithHeaders(RedirectAttributes attributes) {
        // Add headers to the redirect request
        attributes.addAttribute("hello", "world");
        // Redirect to the desired URL
        return new RedirectView("http://someEngine.com/index.html");
    }

Please suggest how to achieve this.

1

There are 1 answers

1
Lajos Arpad On

You can use the setHeader method of HttpClient:

HttpRequest request = HttpRequest.newBuilder()
  .setHeader("hello", "world")
  .uri(new URI(url)).build();

and then

httpClient.send(request, HttpResponse.BodyHandlers.ofString());