title: Spring OAuth Authorization Server behind Spring Cloud Zuul Proxy
body: <p>I am currently developing a application based on a micro service architecture. We use a API-Gateway implemented using Spring Cloud Netfix's Zuul Server to route the requests to our micro services.</p>
  
  <p>To realize single sign on for all our services I am currently working on an OAuth2 server set up using Spring Cloud Security. The server is basically just copy and past of the implementation in Dave Syer's Repo: <a href="https://github.com/dsyer/spring-security-angular/tree/master/oauth2/authserver" rel="noreferrer">https://github.com/dsyer/spring-security-angular/tree/master/oauth2/authserver</a></p>
  
  <p>The main difference is that I want to route the requests to my OAuth server through the Zuul Proxy. This way I will not have to directly expose my OAuth Server and can add and remove Login Server dynamically. </p>
  
  <p>The problem is I do not seam to understand how to correctly configure this setup. When I try to access a protected resource on the OAuth server I am forwarded to the login page. This of course is as expected. But I can not figure out how to set the hostname and port used when forwarding. What I want to happen is the server to forward to an endpoint on the Zuul server that will get proxied back to the OAuth server. (The Zuul API-Gateway should be the only server the client ever talks to. Everything else will be hidden.)</p>
  
  <p>As it is the host and port are read from the <code>HttpServletRequest</code> in <code>LoginUrlAuthenticationEntryPoint</code>. But the request the server sees is the request send by the Zuul proxy. So I am forwarded to an internal IP not an endpoint on the proxy.</p>
  
  <p>I tried to set the URL of the login page in <code>WebSecurityConfigurerAdapter.configure(HttpSecurity)</code> to the absolut URL of my Zuul Proxy. But this just caused my application to complain about too many redirects. (Might have caused a loop there.)</p>
  
  <p>What would be the best way to set this up? </p>
  
  <ul>
  <li>Do I have to implement some kind of own forwarding strategy by overriding a bean?</li>
  <li>Is there a configuration option I am missing?</li>
  <li>Is my idea itself wrong? (In his answer to <a href="https://stackoverflow.com/questions/29417005/how-to-avoid-redirect-to-another-host-with-zuul?answertab=votes#tab-top">How to avoid redirect to another host with Zuul?</a> Dave Syer says you would not normally proxy this but does not explain why.)</li>
  </ul>