I have configured Azure AD credentials in my Spring Boot project’s application.propertiesfile using the dependency spring-cloud-azure-starter-active-directory. Here is the configuration:
spring.cloud.azure.active-directory.enabled=true
spring.cloud.azure.active-directory.profile.tenant-id=${TENANT_ID_ENV_VAR}
spring.cloud.azure.active-directory.credential.client-id=${CLIENT_ID_ENV_VAR}
spring.cloud.azure.active-directory.credential.client-secret=${CLIENT_SECRET_ENV_VAR}
Application Code:
@RestController
public class HelloController {
@GetMapping("/user")
public String getUserInfo(
@AuthenticationPrincipal OAuth2User principal) {
// Retrieve the email or username from the principal
String userName = principal.getAttribute("preferred_username");
// Return a response with the user's email
return "Hello: " + userName;
}
}
Currently, I am encountering an error on a particular Azure Web App (ASP) server: [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/0d993ad3-fa73-421a-b129-1fe5590103f3/oauth2/v2.0/token": login.microsoftonline.com: Temporary failure in name resolution.
I have cross-checked the AD credentials and I am getting a successful response in the local environment and a different web app as well. After referring to some Google documents and links mentioned below, I have made the necessary adjustments, but I am still getting the same error on that web app.
Spring Boot Version:
<properties>
<java.version>17</java.version>
<spring-cloud-azure.version>5.5.0</spring-cloud-azure.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-active-directory</artifactId>
</dependency>
</dependencies>
Could u please guide me how to fix/resolve this issue and and what are the posssible reasons for this issue.
2
Answers
To resolve the [invalid_token_response] error in Azure Web App, change the DNS zone option in the networking section from 'Custom' to 'Default' (Azure provided).
I successfully retrieved the
username
andaccess token
both locally and in the Azure Web App.HelloController.java :
I have modified the
HelloController
as below to retrieve the Access Token.Below is the complete
HelloController
class.SecurityConfig.java :
I added the signin logic in the
SecurityConfig
class.application.properties :
pom.xml :
I have added the redirect URI for both local and Azure Web Apps in the service principal under
Authentication
, as shown below.Local Output :
I successfully retrieved the
userName
andAccess Token
in the browser.Note
: Before deploying, make sure to update the redirect URI in theapplication.properties
file to the web app URL as shown below.Azure Web App Output :
I successfully retrieved the
userName
andAccess Token
in the Azure Web apps.