title: Spring: Redirect with Authorization Header
body: <p>I'm currently writing an application that issues a JWT token on demand.
  When the token is issued, the user should be redirected to a webpage. This works like a charm - but I need to set an authorization header for that redirect. </p>
  
  <p>The user enters his credentials on Webpage A. Webpage A sends a POST Request to Server B. Server B checks the credentials and offers a token. Now the user should be redirected to Webpage C. </p>
  
  <p>I tried the following:</p>
  
  <pre><code>@RequestMapping(value = "/token", method = RequestMethod.POST, produces = "application/json", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
  public ResponseEntity&lt;Object&gt; token(
          @RequestParam("user") String _username, 
          @RequestParam("secret") String _secret
          ) throws Exception
  {       
      headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
  
      MultiValueMap&lt;String, String&gt; map = new LinkedMultiValueMap&lt;String, String&gt;();
      map.add("user", _username);
      map.add("secret", _secret);
  
      HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; entity = new HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt;(map, headers);
  
      HttpStatus statusCode = HttpStatus.FOUND;
      HttpHeaders httpHeaders = new HttpHeaders();
  
      try {
          ResponseEntity&lt;String&gt; request = restTemplate.exchange(_url, HttpMethod.POST, entity, String.class);
      } catch (Exception ex) {
          ex.printStackTrance();
      }
  
      String response = request.getBody();
  
      JSONObject _tokenObject = new JSONObject(response);
      String _token = _tokenObject.getString("access_token");
  
      httpHeaders.add("Authorization", "Bearer: " + _token);
  
      URI _redirectUri = new URI("http://foo.example.com/webpageC");
      httpHeaders.setLocation(_redirectUri);
  
      return new ResponseEntity&lt;&gt;(httpHeaders, HttpStatus.FOUND);
  
  }
  </code></pre>
  
  <p>The redirect works, but only <code>/token</code> gets the Authorization Header as Response Header, right before the redirect happens.</p>
  
  <p>How can I achieve that the header is sent to Webpage C?</p>
  
  <p>Thanks.</p>
  
  <p><strong>Update</strong></p>
  
  <p>A <code>forward:</code> is not possible, as Webpage C is on another URL and not in the same Controller.</p>
  
  <p>Anyone has an Idea how to solve? </p>