I adapted the following OAauth2 Spring Cloud samples:
The only change I made, was using JPA on the Authserver side to check the credentials from a database. Everything works well, except deploying it behind an nginx proxy. As used in the sample apps above, Spring Boot and embedded Tomcat is used. I also properly configured proxy headers:
server.tomcat.protocol-header=X-Forwarded-Proto
server.tomcat.remote-ip-header=X-Real-IP
Proxying HTTP is working:
accessTokenUri: http://uaa.sample.com/oauth/token
userAuthorizationUri: http://uaa.sample.com/oauth/authorize
So far so good, but I need to use SSL (obviously):
accessTokenUri: https://uaa.sample.com/oauth/token
userAuthorizationUri: https://uaa.sample.com/oauth/authorize
If I switch to SSL, I get a 401 from my client application after the auth server is redirecting back from authorize. I captured the HTTP traffic and everything seems to work:
- GET request to client application
- Client app redirects to /login
- /login redirects to https://uaa.sample.com/oauth/authorize?client_id=reprisk&redirect_uri=http://test.sample.com/login&response_type=code&state=9prwi2
- Auth server redirects to https://uaa.sample.com/login
- After login, authorize is called again and the server finally redirects to http://test.sample.com/login?code=212eRK&state=9prwi2
The HTTP traffic for HTTP and HTTPS is exactly the same, except that for HTTP a proper referer is set for the last request (AFAIK, the referer isn't checked during OAuth authentication, right?):
HTTP:
GET /login?code=212eRK&state=9prwi2 HTTP/1.1
Host: test.sample.com
...
Referer: http://uaa.sample.com/login
Cookie: JSESSIONID=401EB8D1D1F4297160D518EC253A0CB5; XSRF-TOKEN=95a00a0d-3362-4e9b-b7eb-45addf2d10b4
...
---
HTTP/1.1 302 Found
HTTPS:
GET /login?code=212eRK&state=9prwi2 HTTP/1.1
Host: test.sample.com
...
Cookie: JSESSIONID=401EB8D1D1F4297160D518EC253A0CB5; XSRF-TOKEN=95a00a0d-3362-4e9b-b7eb-45addf2d10b4
...
---
HTTP/1.1 401 Unauthorized
Corresponding log message from client application:
Authentication request failed: org.springframework.security.authentication.BadCredentialsException: Could not obtain access token.
Any ideas why using a proxy and SSL isn't working? I'm happy to share more code and/or log output!
Thanks!!!
1. OAuth2ClientAuthenticationProcessingFilter - attemptAuthentication() method's log hiding the actual error's cause(407 error) & displaying error "BadCredentialsException: Could not obtain access token"
from the msg it's very difficult to under stand the cause(issue with basic auth or proxy or some other issue)
2. DefaultClientAuthenticationHandler - authenticateTokenRequest() method is only setting the basic authentication header & not setting any proxy header – Karthikeyan Jun 15 '16 at 10:46