3. CredHubOperations 简介
接口和实现是 Spring CredHub 中的中心类。 提供对模拟完整 CredHub API 的其他操作接口的访问:org.springframework.credhub.core.CredHubOperations
org.springframework.credhub.core.CredHubTemplate
CredHubOperations
/**
* Get the operations for saving, retrieving, and deleting credentials.
*/
CredHubCredentialOperations credentials();
/**
* Get the operations for adding, retrieving, and deleting credential permissions.
*/
CredHubPermissionOperations permissions();
/**
* Get the operations for adding, retrieving, and deleting credential permissions.
*/
CredHubPermissionV2Operations permissionsV2();
/**
* Get the operations for retrieving, regenerating, and updating certificates.
*/
CredHubCertificateOperations certificates();
/**
* Get the operations for interpolating service binding credentials.
*/
CredHubInterpolationOperations interpolation();
/**
* Get the operations for retrieving CredHub server information.
*/
CredHubInfoOperations info();
3.1. 映射到 CredHub API
接口的每个方法都直接映射到 CredHub HTTP API 的一个端点。
下表显示了 CredHub API 和相应的 Spring CredHub 接口之间的映射。Operations
Operations
CredHub 权限 API (v1) |
|
CredHub 权限 API (v2) |
|
3.2. CredHubOperations 自动配置
正确配置应用程序属性后,将使用 Spring Boot 自动配置创建 Spring Bean。
应用程序类可以自动装配此 bean 的实例以与 CredHub 服务器进行交互。CredHubOperations
/*
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.credhub;
@Component
public class CredHubService {
private final CredHubOperations credHubOperations;
private final SimpleCredentialName credentialName;
public CredHubService(CredHubOperations credHubOperations) {
this.credHubOperations = credHubOperations;
this.credentialName = new SimpleCredentialName("example", "password");
}
public String generatePassword() {
PasswordParameters parameters = PasswordParameters.builder().length(12).excludeLower(false).excludeUpper(false)
.excludeNumber(false).includeSpecial(true).build();
CredentialDetails<PasswordCredential> password = this.credHubOperations.credentials()
.generate(PasswordParametersRequest.builder().name(this.credentialName).parameters(parameters).build());
return password.getValue().getPassword();
}
public String getPassword() {
CredentialDetails<PasswordCredential> password = this.credHubOperations.credentials()
.getByName(this.credentialName, PasswordCredential.class);
return password.getValue().getPassword();
}
}