title: How to integrate keycloak in Spring Boot with a different context root and reverse proxy
body: <p>We are currently developing a microservice application using Spring Boot 1.4 and Keycloak 2.5.0 (configured as openid-connect service) using the Keycloak Spring Adapter (not the Spring Boot adapter). </p>
  
  <p>All of our microservices are put behind a load balancer and an additional reverse proxy as the application will be hosted on an existing domain behind a context root (so the root of our application is <a href="http://foo.bar/foobar/" rel="nofollow noreferrer">http://foo.bar/foobar/</a> and the rest services are <a href="http://foo.bar/foobar/rest/" rel="nofollow noreferrer">http://foo.bar/foobar/rest/</a>).</p>
  
  <p>We are facing a couple of problems with Keycloak in this given scenario:</p>
  
  <ol>
  <li>Keycloak forward to /sso/login if a sign-in is needed. This is in our case unwanted behaviour because <a href="http://foo.bar/sso/login" rel="nofollow noreferrer">http://foo.bar/sso/login</a> will not exist. I have found a way to change the forward but there is no way to make Keycloak listen to the same url; we end up with a 404 in this case.</li>
  <li>After signing in, Keycloak redirects back to the /sso/login url with the correct tokens, but if this is not the same server, the request fails and it redirects us to <a href="http://foo.bar/" rel="nofollow noreferrer">http://foo.bar/</a>. Since every microservice exposes /sso/login, this can be in fact a completely different server.</li>
  <li>If keycloak is hosted on the same domain, we end up in a redirect loop. We would also like to have Keycloak hosted on the same domain and on the context root <a href="http://foo.bar/foobar/auth/" rel="nofollow noreferrer">http://foo.bar/foobar/auth/</a> .</li>
  </ol>
  
  <p>We've already tried using the <code>"token-store": "cookie"</code> but this did not resolve the problem.</p>
  
  <p>Is there a way to resolve these problems or is Keycloak maybe not the correct solution for our use-case ?  </p>
  
  <hr>
  
  <p><strong>Update 05/05/2017</strong>: 
  Move my answer from here to an answer</p>