更新时间:2024-12-17 GMT+08:00
导出证书
导出私有证书,包含证书体与证书链。可根据具体需求导出对应格式的证书。
相关参数详情请参见导出证书参数说明。
import com.huaweicloud.sdk.ccm.v1.CcmClient;
import com.huaweicloud.sdk.ccm.v1.model.ExportCertificateRequest;
import com.huaweicloud.sdk.ccm.v1.model.ExportCertificateRequestBody;
import com.huaweicloud.sdk.ccm.v1.model.ExportCertificateResponse;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
/**
* 导出私有证书,包含证书体与证书链。可根据具体需求导出对应格式的证书
*/
public class ExportCertificateExample {
/**
* 基础认证信息:
* - ACCESS_KEY: 华为云账号Access Key
* - SECRET_ACCESS_KEY: 华为云账号Secret Access Key
* - DOMAIN_ID: 华为云账号ID
* - CCM_ENDPOINT: 华为云CCM服务(PCA属于CCM下的微服务)访问终端地址
* 认证使用的ak和sk硬编到代码中或明文存储存在较大安全风险,建议在配置文件或环境变量中密文存放,使用时解密,确保安全;
* 本示例ak和sk保存在环境变量中为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
*/
private static final String ACCESS_KEY = System.getenv("HUAWEICLOUD_SDK_AK");
private static final String SECRET_ACCESS_KEY = System.getenv("HUAWEICLOUD_SDK_SK");
private static final String DOMAIN_ID = "<DomainID>";
private static final String CCM_ENDPOINT = "<CcmEndpoint>";
public static void main(String[] args) {
// 1.准备访问华为云的认证信息,PCA为全局服务
final GlobalCredentials auth = new GlobalCredentials()
.withAk(ACCESS_KEY)
.withSk(SECRET_ACCESS_KEY)
.withDomainId(DOMAIN_ID);
// 2.初始化SDK,传入认证信息及CCM服务的访问终端地址
final CcmClient ccmClient = CcmClient.newBuilder()
.withCredential(auth)
.withEndpoint(CCM_ENDPOINT).build();
// 3、构造请求参数
// (1)需要导出的终端实体证书的ID
String certId = "5554a381-af92-4336-a943-811396c87616";
/*
(2)定义导出格式(SDK仅支持非压缩下的)
- isCompressed: 是否压缩,类型为String,可选值true、false,SDK仅支持false
- type:导出形式,SDK调用目前仅支持以下形式
APACHE : apache服务器推荐使用此参数;
NGINX : nginx服务器推荐使用此参数;
OTHER : 下载PEM格式证书,推荐使用此参数。
*/
ExportCertificateRequestBody requestBody = new ExportCertificateRequestBody();
requestBody.setType("NGINX");
requestBody.setIsCompressed("false");
// 4、构造请求体
ExportCertificateRequest request = new ExportCertificateRequest()
.withCertificateId(certId)
.withBody(requestBody);
// 5、开始发起请求
ExportCertificateResponse response;
try {
response = ccmClient.exportCertificate(request);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
// 6、获取响应消息
//(1)获取证书体,pem格式
String certificate = response.getCertificate();
//(2)获取证书链,pem格式
String certificateChain = response.getCertificateChain();
System.out.println(response);
}
}
父主题: 私有证书管理代码示例