更新时间:2026-07-01 GMT+08:00
分享

使用临时安全凭证

您可以使用临时安全凭证对API请求进行签名,进而通过编程方式访问对应的华为云资源。临时安全凭证提供的权限与永久访问密钥(例如IAM用户永久AK/SK)相同,但存在如下区别:

  • 使用临时安全凭证进行调用时,必须在调用中包含随临时安全凭证一同返回的会话令牌security_token。华为云使用该会话令牌来验证临时安全凭证的有效性。
  • 临时安全凭证存在有效期。到期后,任何使用此临时安全凭证签名的调用都将失败,此时您必须生成一组新的临时安全凭证。
  • 使用临时安全凭证签名调用时,请求会话可能会包含一组标签,这些标签来自调用AssumeAgency API时传递的会话标签参数。

一般地,您可调用STS AssumeAgency API来获取临时安全凭证,然后使用这些安全凭证显式地调用其他华为云服务。如果您是外部身份提供商的用户,您也可以调用STS AssumeAgencyWithSAML API 或者STS AssumeAgencyWithOIDC API来获取临时安全凭证,然后使用这些安全凭证显式地调用其他华为云服务。

将临时安全凭证用于华为云SDK

  1. 将临时安全凭证用于华为云SDK
    要在代码中使用临时安全凭证,可以通过调用STS的AssumeAgency API,获取生成的临时安全凭证,包含临时AK/SK和会话令牌security_token。然后,您可以使用生成的临时安全凭证作为对华为云服务的后续API调用的凭证。以下示例说明了使用华为云SDK发起请求获取临时安全凭证的示例代码:
    public static void main(String[] args) {
        // 配置认证信息
        ICredential auth = new BasicCredentials()
                // 可通过环境变量等方式配置认证信息
                .withAk(System.getenv("HUAWEICLOUD_SDK_AK"))
                .withSk(System.getenv("HUAWEICLOUD_SDK_SK"))
                // 如果未填写ProjectId,SDK会自动调用IAM服务查询所在region对应的项目id,注意经过VPCEP时,必须手动填写此参数
                .withProjectId("{your projectId string}");
    
        // 创建服务客户端
        StsClient client = StsClient.newBuilder()
                .withCredential(auth)
                .withRegion(StsRegion.valueOf("{region id string}"))
                .build();
    
        // 发送请求并获取响应
        AssumeAgencyReqBody assumeAgencyReqBody = new AssumeAgencyReqBody()
                .withAgencyUrn("{your agency urn}")
                .withAgencySessionName("{agency session name}");
        AssumeAgencyRequest request = new AssumeAgencyRequest().withBody(assumeAgencyReqBody);
        try {
            AssumeAgencyResponse response = client.assumeAgency(request);
            System.out.println(response.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    您必须确保在临时安全凭证到期之前,获得一组新的临时安全凭证。

  2. AssumeAgencyWithSAML API示例

    您已经完成了身份提供商SAML协议配置,要在代码中使用临时安全凭证,可以通过调用STS的AssumeAgencyWithSAML API,获取生成的临时安全凭证,包含临时AK/SK和会话令牌security_token。然后,您可以使用生成的临时安全凭证作为对华为云服务的后续API调用的凭证。以下示例说明了使用华为云SDK发起请求获取临时安全凭证的示例代码:

    public static void main(String[] args) {
        // 创建服务客户端
        StsClient client = StsClient.newBuilder()
                .withRegion(StsRegion.valueOf("{region id string}"))
                .build();
    
    
        // 发送请求并获取响应
        AssumeAgencyWithSAMLReqBody assumeAgencyWithSAMLReqBody = new AssumeAgencyWithSAMLReqBody()
                .withProviderUrn("{your provider urn}")
                .withAgencyUrn("{your agency urn}")
                .withSamlAssertion("{saml assertion from idp}");
        AssumeAgencyWithSAMLRequest request = new AssumeAgencyWithSAMLRequest().withBody(assumeAgencyWithSAMLReqBody);
        try {
            AssumeAgencyWithSAMLResponse response = client.assumeAgencyWithSAML(request);
            System.out.println(response.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
  3. AssumeAgencyWithOIDC API示例
    您已经完成了身份提供商OIDC协议配置,要在代码中使用临时安全凭证,可以通过调用STS的AssumeAgencyWithOIDC API,获取生成的临时安全凭证,包含临时AK/SK和会话令牌security_token。然后,您可以使用生成的临时安全凭证作为对华为云服务的后续API调用的凭证。以下示例说明了使用华为云SDK发起请求获取临时安全凭证的示例代码:
    public static void main(String[] args) {
        // 创建服务客户端
        StsClient client = StsClient.newBuilder()
                .withRegion(StsRegion.valueOf("{region id string}"))
                .build();
    
    
        // 发送请求并获取响应
        AssumeAgencyWithOIDCReqBody assumeAgencyWithOIDCReqBody = new AssumeAgencyWithOIDCReqBody()
                .withProviderUrn("{your provider urn}")
                .withAgencyUrn("{your agency urn}")
                .withAgencySessionName("{agency session name}")
                .withIdToken("{id token from idp}");
        AssumeAgencyWithOIDCRequest request = new AssumeAgencyWithOIDCRequest().withBody(assumeAgencyWithOIDCReqBody);
        try {
            AssumeAgencyWithOIDCResponse response = client.assumeAgencyWithOIDC(request);
            System.out.println(response.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

直接调用API发起请求

一般地,直接向华为云发出HTTPS API请求,同样需要先通过调用STS的AssumeAgency API,获取生成的临时安全凭证。使用临时安全凭证签名调用的方式,与使用永久访问密钥进行签名调用的过程是一样的,唯一不同点是需要将临时安全凭证的会话令牌security_token添加到API请求的HTTP Header中,Header头为X-Security-Token。有关签署HTTPS API请求的更多信息,请参阅《API签名指南》

您使用联邦方式发出HTTPS API请求,则不需要使用华为云签名认证,SAML协议直接使用身份提供商签发的SAML断言调用STS AssumeAgencyWithSAML API,OIDC协议直接使用身份提供商签发的OIDC令牌调用STS AssumeAgencyWithOIDC API,获取临时安全凭证。

相关文档