我有一个Azure webapp ()和Tomcat一起运行。我部署了两个战争应用程序。WAR-1提供web服务调用,它使用Springboot返回json文件。WAR-2是一个在WAR-1中调用此web服务的web应用程序。此webapp具有系统指定的托管标识(或MSI)。此外,该webapp还使用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();
....我确实能够得到一个令牌,但是当我打get电话时,响应代码是500。
所以我的问题是..。这是打电话的正确方式吗?我确实找到了一篇类似于这种情况的文章https://dotnetdevlife.wordpress.com/2018/10/22/call-azure-ad-protected-website-using-managed-service-identity-msi/,但它使用了.Net。我找不到任何与此类似的Java。
发布于 2019-11-05 14:00:41
我在我身边测试过,下面是我的步骤:
1.一个Azure web应用程序中的两个应用程序。
App1:https://jackdemoapp1.azurewebsites.net/app1/
App2: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();
}结果

https://stackoverflow.com/questions/58697914
复制相似问题