从Java验证Azure API应用程序
我和这篇文章有一个类似的问题:但在我的例子中,我有一个客户的Java客户端托管在JBoss中,他需要访问我的API。该服务被保护为“公共(已验证)”,我从浏览器访问它时没有任何问题。我知道我可以在.net中创建Azure API应用程序客户端,但我找不到任何关于如何从Java进行身份验证的示例。目前这是否可行?如果可能,是否有人提供了任何有帮助的示例或建议?我查看了下面的一些文档,用Java制作了一个示例,用于从AAD认证的客户端调用Azure API应用程序 作为参考: 对于示例,我在Eclipse中创建了一个maven项目,并使用libs从Java验证Azure API应用程序,java,azure,azure-active-directory,azure-api-apps,Java,Azure,Azure Active Directory,Azure Api Apps,我和这篇文章有一个类似的问题:但在我的例子中,我有一个客户的Java客户端托管在JBoss中,他需要访问我的API。该服务被保护为“公共(已验证)”,我从浏览器访问它时没有任何问题。我知道我可以在.net中创建Azure API应用程序客户端,但我找不到任何关于如何从Java进行身份验证的示例。目前这是否可行?如果可能,是否有人提供了任何有帮助的示例或建议?我查看了下面的一些文档,用Java制作了一个示例,用于从AAD认证的客户端调用Azure API应用程序 作为参考: 对于示例,我在E
adal4j
,common io
&httpclient
。下面是pom.xml
文件中的依赖项配置
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>adal4j</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
</dependencies>
如果有任何问题,请随时告诉我。我查看了下面的一些文档,以制作一个Java示例,用于从AAD认证的客户端调用Azure API应用程序
try (CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(getSslConnectionSocketFactory(logger))
.build();
CloseableHttpResponse response = httpClient.execute(httpPost)) {
final int httpStatusCode = response.getStatusLine().getStatusCode();
} catch (IOException ex) {
logger.info(format("Error relaying to court store %s , %s", caseReference, ex));
}
private SSLConnectionSocketFactory getSslConnectionSocketFactory(final Logger logger) {
SSLConnectionSocketFactory sslConSocFactory = null;
try {
final KeyStore ks = KeyStore.getInstance("Windows-MY");
ks.load(null, null);
final Certificate certificate = ks.getCertificate(CERTIFICATE_SUBJECT);
if (certificate == null) {
logger.info(format("Certificate not found for %s", CERTIFICATE_SUBJECT));
}
final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(ks, acceptingTrustStrategy).build();
sslConSocFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException | KeyManagementException ex) {
logger.info(format("Error reading certificate : , %s", ex));
}
return sslConSocFactory;
}
作为参考:
对于示例,我在Eclipse中创建了一个maven项目,并使用libsadal4j
,common io
&httpclient
。下面是pom.xml
文件中的依赖项配置
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>adal4j</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
</dependencies>
任何问题,请随时告诉我。@peter pan msft这是一个比我所希望的更完整的答案。非常感谢。我缺少的是如何从身份验证令牌响应中提取身份验证令牌。@peter pan msft这是一个比我所希望的更完整的答案。非常感谢。我缺少的是如何从身份验证令牌响应中提取身份验证令牌。
try (CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(getSslConnectionSocketFactory(logger))
.build();
CloseableHttpResponse response = httpClient.execute(httpPost)) {
final int httpStatusCode = response.getStatusLine().getStatusCode();
} catch (IOException ex) {
logger.info(format("Error relaying to court store %s , %s", caseReference, ex));
}
private SSLConnectionSocketFactory getSslConnectionSocketFactory(final Logger logger) {
SSLConnectionSocketFactory sslConSocFactory = null;
try {
final KeyStore ks = KeyStore.getInstance("Windows-MY");
ks.load(null, null);
final Certificate certificate = ks.getCertificate(CERTIFICATE_SUBJECT);
if (certificate == null) {
logger.info(format("Certificate not found for %s", CERTIFICATE_SUBJECT));
}
final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(ks, acceptingTrustStrategy).build();
sslConSocFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException | KeyManagementException ex) {
logger.info(format("Error reading certificate : , %s", ex));
}
return sslConSocFactory;
}