当前位置: 首页>>代码示例>>Java>>正文


Java AsymmetricKeyCredential类代码示例

本文整理汇总了Java中com.microsoft.aad.adal4j.AsymmetricKeyCredential的典型用法代码示例。如果您正苦于以下问题:Java AsymmetricKeyCredential类的具体用法?Java AsymmetricKeyCredential怎么用?Java AsymmetricKeyCredential使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


AsymmetricKeyCredential类属于com.microsoft.aad.adal4j包,在下文中一共展示了AsymmetricKeyCredential类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: acquireAccessToken

import com.microsoft.aad.adal4j.AsymmetricKeyCredential; //导入依赖的package包/类
private AuthenticationResult acquireAccessToken(String resource) throws IOException {
    String authorityUrl = this.environment().activeDirectoryEndpoint() + this.domain();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    AuthenticationContext context = new AuthenticationContext(authorityUrl, false, executor);
    if (proxy() != null) {
        context.setProxy(proxy());
    }
    try {
        if (clientSecret != null) {
            return context.acquireToken(
                    resource,
                    new ClientCredential(this.clientId(), clientSecret),
                    null).get();
        } else if (clientCertificate != null && clientCertificatePassword != null) {
            return context.acquireToken(
                    resource,
                    AsymmetricKeyCredential.create(clientId, new ByteArrayInputStream(clientCertificate), clientCertificatePassword),
                    null).get();
        } else if (clientCertificate != null) {
            return context.acquireToken(
                    resource,
                    AsymmetricKeyCredential.create(clientId(), privateKeyFromPem(new String(clientCertificate)), publicKeyFromPem(new String(clientCertificate))),
                    null).get();
        }
        throw new AuthenticationException("Please provide either a non-null secret or a non-null certificate.");
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    } finally {
        executor.shutdown();
    }
}
 
开发者ID:Azure,项目名称:autorest-clientruntime-for-java,代码行数:32,代码来源:ApplicationTokenCredentials.java

示例2: redeemDaemon

import com.microsoft.aad.adal4j.AsymmetricKeyCredential; //导入依赖的package包/类
/**
 * Redeem daemon.
 *
 * @param redeemDaemonRequest the redeem daemon request
 * @return the Access Token redeemed it
 * @throws AuthenticationException the authentication exception
 */
public static String redeemDaemon(RedeemDaemonRequest redeemDaemonRequest) throws AuthenticationException{
	ExecutorService service = Executors.newCachedThreadPool();
	AuthenticationResult authenticationResult = null;
	String authority = String.format(ApiEnviroment.tokenDaemonBaseUrl.getValue(), redeemDaemonRequest.getTenantId());
	logger.debug("Trying to get App Only token for {}", redeemDaemonRequest);
	try {
		AuthenticationContext authenticationContext =  new AuthenticationContext(authority, false, service);
		String filePkcs12 = ApiEnviroment.fileUrlPkcs12Certificate.getValue();
		if(StringUtils.isNotEmpty(redeemDaemonRequest.getFileUrlPkcs12Certificate())){
			filePkcs12 = redeemDaemonRequest.getFileUrlPkcs12Certificate(); 
		}
		
		String filePkcs12Secret = ApiEnviroment.pkcs12CertificateSecret.getValue();
		if(StringUtils.isNotEmpty(redeemDaemonRequest.getCertificateSecret())){
			filePkcs12Secret = redeemDaemonRequest.getCertificateSecret(); 
		}
		
		Validate.notEmpty(filePkcs12, "Pkcs12 Key file path must be provided or configured. You can set it on environment var 'ONEDRIVE_DAEMON_PKCS12_FILE_URL' or through Java System Property 'onedrive.daemon.pkcs12.file.url'");
		Validate.notEmpty(filePkcs12Secret, "Pkcs12 Secret Key file must be provided or configured. You can set it on environment var 'ONEDRIVE_DAEMON_PKCS12_FILE_SECRET' or through Java System Property 'onedrive.daemon.pkcs12.file.secret'");
		
		InputStream pkcs12Certificate = new FileInputStream(filePkcs12);
		AsymmetricKeyCredential credential = AsymmetricKeyCredential.create(redeemDaemonRequest.getClientId(), pkcs12Certificate, filePkcs12Secret);
		
		Future<AuthenticationResult> future = authenticationContext.acquireToken(redeemDaemonRequest.getResourceSharepointId(), credential, null);
		authenticationResult = future.get(10, TimeUnit.SECONDS);
		logger.debug("Token retrieved {}", ToStringBuilder.reflectionToString(authenticationResult, ToStringStyle.SHORT_PREFIX_STYLE));
		return authenticationResult.getAccessToken();
	} catch (Exception e) {
		logger.error("Error trying to get new App Only Token", e);
		throw new AuthenticationException(String.format("Error trying to get new App Only Token for tenantId %s and sharepointUri %s", redeemDaemonRequest.getTenantId(), redeemDaemonRequest.getResourceSharepointId()));
	}finally{
		service.shutdown();
	}

}
 
开发者ID:mxhero,项目名称:onedrive-api,代码行数:43,代码来源:OneDrive.java

示例3: acquireNewAccessToken

import com.microsoft.aad.adal4j.AsymmetricKeyCredential; //导入依赖的package包/类
AuthenticationResult acquireNewAccessToken(String resource) throws IOException {
    if (authorizationCode == null) {
        throw new IllegalArgumentException("You must acquire an authorization code by redirecting to the authentication URL");
    }
    String authorityUrl = this.environment().activeDirectoryEndpoint() + this.domain();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    AuthenticationContext context = new AuthenticationContext(authorityUrl, false, executor);
    if (proxy() != null) {
        context.setProxy(proxy());
    }
    try {
        if (applicationCredentials.clientSecret() != null) {
            return context.acquireTokenByAuthorizationCode(
                    authorizationCode,
                    new URI(redirectUrl),
                    new ClientCredential(applicationCredentials.clientId(), applicationCredentials.clientSecret()),
                    resource, null).get();
        } else if (applicationCredentials.clientCertificate() != null && applicationCredentials.clientCertificatePassword() != null) {
            return context.acquireTokenByAuthorizationCode(
                    authorizationCode,
                    new URI(redirectUrl),
                    AsymmetricKeyCredential.create(
                            applicationCredentials.clientId(),
                            new ByteArrayInputStream(applicationCredentials.clientCertificate()),
                            applicationCredentials.clientCertificatePassword()),
                    resource,
                    null).get();
        } else if (applicationCredentials.clientCertificate() != null) {
            return context.acquireTokenByAuthorizationCode(
                    authorizationCode,
                    new URI(redirectUrl),
                    AsymmetricKeyCredential.create(
                            clientId(),
                            ApplicationTokenCredentials.privateKeyFromPem(new String(applicationCredentials.clientCertificate())),
                            ApplicationTokenCredentials.publicKeyFromPem(new String(applicationCredentials.clientCertificate()))),
                    resource,
                    null).get();
        }
        throw new AuthenticationException("Please provide either a non-null secret or a non-null certificate.");
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    } finally {
        executor.shutdown();
    }
}
 
开发者ID:Azure,项目名称:autorest-clientruntime-for-java,代码行数:46,代码来源:DelegatedTokenCredentials.java

示例4: main

import com.microsoft.aad.adal4j.AsymmetricKeyCredential; //导入依赖的package包/类
public static void main(String[] args) {
	ExecutorService executorService = Executors.newFixedThreadPool(1);

    try {
        String tenant = "tenant.domain.com";
        String clientId = "%client_id%";
        String restApiEndpoint = "https://account.restv2.region.media.azure.net/api/";
        String pfxFilename = "%path_to_keystore.pfx%";
        String pfxPassword = "%keystore_password%";
        InputStream pfx = new FileInputStream(pfxFilename);

        // Connect to Media Services API with service principal and client certificate
        AzureAdTokenCredentials credentials = new AzureAdTokenCredentials(
                tenant,
                AsymmetricKeyCredential.create(clientId, pfx, pfxPassword),
                AzureEnvironments.AZURE_CLOUD_ENVIRONMENT);

        TokenProvider provider = new AzureAdTokenProvider(credentials, executorService);

        // create a new configuration with the new credentials
        Configuration configuration = MediaConfiguration.configureWithAzureAdTokenProvider(
                new URI(restApiEndpoint),
                provider);

        // create the media service with the new configuration
        MediaContract mediaService = MediaService.create(configuration);

        System.out.println("Listing assets");

        ListResult<AssetInfo> assets = mediaService.list(Asset.list());

        for (AssetInfo asset : assets) {
            System.out.println(asset.getId());
        }

    } catch (ServiceException se) {
        System.out.println("ServiceException encountered.");
        System.out.println(se.toString());
    } catch (Throwable e) {
        System.out.println("Exception encountered.");
        e.printStackTrace();
        System.out.println(e.toString());
    } finally {
        executorService.shutdown();
    }
}
 
开发者ID:southworkscom,项目名称:azure-sdk-for-media-services-java-samples,代码行数:47,代码来源:ServicePrincipalWithClientCertificate.java


注:本文中的com.microsoft.aad.adal4j.AsymmetricKeyCredential类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。