使用Java访问Azure活动目录保护的Web服务

这里是背景:

    使用Tomcat和Springboot的
  • App Service A提供Web服务端点。例如https://xxx.azurewebsites.net/appA/order/1111
  • webapp A受AAD身份验证保护(express config)。应用程序(Web应用程序)已在AAD中注册。 Web应用程序的客户端ID为 [client-id]
  • 在网络应用中创建了客户端机密 [client-secret]
  • AAD目录ID为 [tenant-id]
  • 使用浏览器打开上面的URL。显示AD​​登录页面,输入凭据后,我可以得到预期的json文件。

现在,我有另一个控制台应用程序,该应用程序试图从webapp A获取订单信息。这样的代码:

String AUTHORITY = "https://login.microsoftonline.com/" + [tenant-id];
String ORDER_URL = "https://xxx.azurewebsites.net/appA/order/1111"

ExecutorService service = Executors.newFixedThreadPool(1);
ClientCredential credentials = new ClientCredential([client-id],[client-secret]);

AuthenticationContext context = new AuthenticationContext(AUTHORITY,false,service);

Future<AuthenticationResult> future = context.acquireToken("https://graph.microsoft.com",credentials,null);
AuthenticationResult result = future.get();

String token = result.getaccessToken();
System.out.println(token);

HttpURLConnection conn = (HttpURLConnection) new URL(ORDER_URL).openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type","application/json; charset=UTF-8");
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization","Bearer " + token);
conn.connect();

int responseCode = conn.getResponseCode();
System.out.println(responseCode);

InputStream is = conn.getInputStream();
...

我可以获取令牌,但可以获取401的responseCode,并且错误消息为“您无权查看此目录或页面”。

那么我是否需要分配其他API权限才能使其正常工作?或缺少其他一些设置。

nikoliu1 回答:使用Java访问Azure活动目录保护的Web服务

我可以重现您的问题,resource应该是您要访问的应用的client-idApplication ID URI,而不是MS图https://graph.microsoft.com

enter image description here

本文链接:https://www.f2er.com/3140124.html

大家都在问