Im using spring oauth2 client against Azure AD - I have it working successfully on my localhost:8080
When I deploy this working OAuth2 Client code against Azure AD on remote server with https enabled https://serverhost.corp.com , I get an Auth Code during url redirection, but the access_token request throws
org.springframework.security.oauth2.core.OAuth2AuthenticationException: [invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: I/O error on POST request for "https://login.microsoftonline.com/<tenantId>/oauth2/v2.0/token": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
The only difference in localhost:8080 and https://serverhost.corp.com is the port from 8080 to 443 and SSL Certificate
I set the proxy to be sure, but this isnt a proxy issue. I also see the url redirection happens and I get the Auth Code
Redirecting to 'https://login.microsoftonline.com/<Tenant>/oauth2/v2.0/authorize?response_type=code&client_id=<ClientId>&scope=openid%20profile%20User.Read&state=<state>&redirect_uri=https://serverhost.corp.com/login/oauth2/code/azure'
I get the Auth Code too
/login/oauth2/code/azure?code=<CODE_HERE>
http
.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2Login()
app.properties are
server.port: 443
server.ssl.key-store: myapp-prod.jks
server.ssl.key-store-password: MyPassword
server.ssl.keyStoreType: JKS
server.ssl.keyAlias: server
org.springframework.security.oauth2.core.OAuth2AuthenticationException: [invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: I/O error on POST request for "https://login.microsoftonline.com/<tenant>/oauth2/v2.0/token": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
at org.springframework.security.oauth2.client.authentication.OAuth2LoginAuthenticationProvider.authenticate(OAuth2LoginAuthenticationProvider.java:110) ~[spring-security-oauth2-client-5.1.5.RELEASE.jar!/:5.1.5.RELEASE]