我有一个与 Tomcat 一起运行的 Azure Web 应用程序(应用程序服务)。我部署了 2 个 war 应用程序。 WAR-1 提供了使用 Springboot 返回 json 文件的 Web 服务调用。 WAR-2 是一个 Web 应用程序,它调用 WAR-1 中的 Web 服务。此 Web 应用程序具有系统分配的托管身份(或 MSI)。此外,此 Web 应用程序使用 Express 配置通过 AAD 进行身份验证。
通过 AAD 进行身份验证后,我可以访问 WAR-2 中的静态页面。现在我需要从 WAR-1 中获取数据。我有一个 servlet,其中包含如下代码:
String subscriptionId = "xxxx";
String testURL = "https://yyy.azurewebsites.net/war1/person/100";
String resourceId = "https://management.azure.com/";
AppServiceMSICredentials credentials = new AppServiceMSICredentials(AzureEnvironment.AZURE);
Azure azure = Azure.configure()
.withLogLevel(LogLevel.BODY_AND_HEADERS)
.authenticate(credentials)
.withSubscription(subscriptionId);
String token = credentials.getToken(resourceId);
HttpURLConnection conn = (HttpURLConnection) new URL(testURL).openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Bearer " + token);
int responseCode = conn.getResponseCode();
OutputStream os = conn.getOutputStream();
....
我确实能够获取 token ,但当我进行 GET 调用时,响应代码为 500。
所以我的问题是......这是进行此通话的正确方法吗?我确实找到了一篇文章https://dotnetdevlife.wordpress.com/2018/10/22/call-azure-ad-protected-website-using-managed-service-identity-msi/与这种情况类似,但它使用.Net。我找不到任何与此等效的 Java 语言。
最佳答案
我在我身边进行了测试,这是我的步骤:
1。一个 Azure Web 应用程序中的两个应用程序。
应用程序1:https://jackdemoapp1.azurewebsites.net/app1/
应用程序2:https://jackdemoapp1.azurewebsites.net/app2/
2。在 Azure 门户上配置身份验证/授权。
您可以通过点击详细信息获取客户端 ID,记下它,我们将在 app2 中使用它:
3。在 Azure 门户上配置托管标识
为了简化测试,app1 将只返回一个“Hello”字符串。
4。 app2中的代码
@ResponseBody
@RequestMapping("/")
public String index() {
JSONObject json = new JSONObject();
try {
AppServiceMSICredentials credential = new AppServiceMSICredentials(AzureEnvironment.AZURE);
// As we want to get token for accessing the aad-protected app, change the
// resource to the client ID you get in step 2
String token = credential.getToken("ac07d701-6f7d-462e-8b67-5dffa1df955f");
json.put("token", token);
// The URL for app1 API
String app1 = "https://jackdemoapp1.azurewebsites.net/app1/";
HttpURLConnection conn = (HttpURLConnection) new URL(app1).openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Bearer " + token);
conn.setDoOutput(true);
conn.setDoInput(true);
// Open the connection
conn.connect();
int code = conn.getResponseCode();
if (code >= 200 && code <= 300) {
try (InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
String response = stringBuilder.toString();
json.put("response", response);
}
} else {
json.put("Error", "Response Code" + conn.getResponseCode());
}
conn.disconnect();
} catch (Exception e) {
json.put("Exception", e.getStackTrace());
}
return json.toString();
}
<小时/>
结果
关于java - 使用 MSI 通过 AAD 访问 Azure Web 应用程序 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58697914/