Description
The way I understand it, if you have a gateway component with a @EnableZuulProxy
and @EnableOAuth2Sso
annotation it is able to proxy requests to Oauth2 protected backends by
- looking into the http session for an Oauth2Authentication (result of the OAuth2 flow / authentication)
- Extract the access token from that OAuth2Authentication object
- Pass that access token as a bearer token to the underlying backend via the Authorization header
So with the following zuul rules
zuul:
routes:
ui:
path: /ui/**
url: http://localhost:8080/ui
backend:
path: /backend/**
url: http://localhost:8800/api
backend2:
path: /backend2/**
url: http://localhost:8083/api
server:
port: 8888
I can access (@EnableResourceServer) backends directly (outside of Zuul) with a valid access token :
curl -v -H "Authorization: Bearer 8cbb47bb-6596-445e-8c0e-b930065d2137" http://localhost:8083/api/
And I can access the same backend through Zuul providing I have a valid JSESSION ID that Spring can use to extract the OAuth2Authentication / Access token
curl -v -H "Cookie:JSESSIONID=88850010D7C53051FEDC201579A6C7FD; XSRF-TOKEN=c7e400ca-74be-4b1a-aeec-d34b6f8a541d" http://localhost:8888/backend2/
Now suppose I have the following flow
- The /ui (Angular app) does a REST call to /backend (using a valid JSESSION)
- /backend calls /backend2 via a REST template (problem .... how does the RestTemplate authenticate the request? )
The server-side code in /backend can call /backend2 via Zuul but it needs to know the JSESSIONID.
By default RestTemplate doesn't send cookies so /backend2 gets called without a valid JESSIONID and it is unable to authenticate the request.
What would be the proper way to solve this ? The /ui would be able to call /backend2 just fine via javascript / REST (as it has a valid session in the browser). But how would a backend component call /backend2 ? I don't want to rely on too many hacks to extract the JSessionID from the original request and sending it as a cookie / header value. Perhaps using RestTemplate isn't the way to go ?