先にジャンプ
VMware は、あなたの進歩を加速させるトレーニングと認定を提供します。
さらに学習したい方に (英語)Spring Cloud Azure は、Spring アプリケーション開発に固有の従来の表現と構成を使用して、Spring アプリケーションと Azure サービス間の統合を提供するオープンソースプロジェクトです。
Spring Cloud Azure を使用すると、Spring アプリケーションで次のタスクを簡単に実行できます。
次の図は、これらの機能の概要を示しています。
次のセクションでは、Spring Cloud Azure を使用する利点を示します。このセクションでは、Azure キー Vault に格納されているシークレットの取得を例として使用します。このセクションでは、Spring Cloud Azure を使用する場合と使用しない場合の Spring Boot アプリケーションの開発の違いを比較します。
Spring Cloud Azure を使用せずに、Azure キー Vault に保存されているシークレットを取得する場合は、次の手順を実行する必要があります。
次の依存関係を pom.xml ファイルに追加します。
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-security-keyvault-secrets</artifactId>
<version>4.5.2</version>
</dependency>
次の例のようなコードを使用して、SecretClient
クラスインスタンスを構築します。
public class DemoClass {
public static void main(String... args) {
SecretClient client = new SecretClientBuilder()
.vaultUrl("vaultUrl")
.credential(new ClientSecretCredentialBuilder()
.tenantId("tenantId")
.clientId("clientId")
.clientSecret("clientSecret")
.build())
.buildClient();
}
}
次の例に示すように、これらのプロパティを構成可能にすることで、client-id
や client-secret
などの情報のハードコーディングを回避します。
@ConfigurationProperties("azure.keyvault")
public class KeyVaultProperties {
private String vaultUrl;
private String tenantId;
private String clientId;
private String clientSecret;
public KeyVaultProperties(String vaultUrl, String tenantId, String clientId, String clientSecret) {
this.vaultUrl = vaultUrl;
this.tenantId = tenantId;
this.clientId = clientId;
this.clientSecret = clientSecret;
}
public String getVaultUrl() {
return vaultUrl;
}
public void setVaultUrl(String vaultUrl) {
this.vaultUrl = vaultUrl;
}
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
}
次の例に示すように、アプリケーションコードを更新します。
@SpringBootApplication
@EnableConfigurationProperties(KeyVaultProperties.class)
public class SecretClientApplication implements CommandLineRunner {
private KeyVaultProperties properties;
public SecretClientApplication(KeyVaultProperties properties) {
this.properties = properties;
}
public static void main(String[] args) {
SpringApplication.run(SecretClientApplication.class, args);
}
@Override
public void run(String... args) {
SecretClient client = new SecretClientBuilder()
.vaultUrl(properties.getVaultUrl())
.credential(new ClientSecretCredentialBuilder()
.tenantId(properties.getTenantId())
.clientId(properties.getClientId())
.clientSecret(properties.getClientSecret())
.build())
.buildClient();
System.out.println("sampleProperty: " + client.getSecret("sampleProperty").getValue());
}
}
次の例に示すように、必要なプロパティを application.yml ファイルに追加します。
azure:
keyvault:
vault-url:
tenant-id:
client-id:
client-secret:
複数の場所で SecretClient
を使用する必要がある場合は、SecretClient
Bean を定義します。次に、関連する場所で SecretClient
をオートワイヤーします。
Spring Cloud Azure を使用して、Azure キー Vault に保存されているシークレットを取得する場合、次の手順に示すように、要件はより単純です。
次の依存関係を pom.xml ファイルに追加します。
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-keyvault-secrets</artifactId>
</dependency>
</dependencies>
次の例に示すように、部品表 (BOM) を使用して Spring Cloud Azure バージョンを管理します。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-dependencies</artifactId>
<version>5.18.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
次のプロパティを application.yml ファイルに追加します。
spring:
cloud:
azure:
keyvault:
secret:
endpoint:
次のコマンドを使用して、Azure CLI (英語) でサインインします。認証情報は Azure CLI によって提供されるため、client-id
や client-secret
などの他の認証情報を追加する必要はありません。
az login
次の例に示すように、関連する場所で SecretClient
をオートワイヤーします。
@SpringBootApplication
public class SecretClientApplication implements CommandLineRunner {
private final SecretClient secretClient;
public SecretClientApplication(SecretClient secretClient) {
this.secretClient = secretClient;
}
public static void main(String[] args) {
SpringApplication.run(SecretClientApplication.class, args);
}
@Override
public void run(String... args) {
System.out.println("sampleProperty: " + secretClient.getSecret("sampleProperty").getValue());
}
}
Spring Cloud Azure は、自動構成された SecretClient
以外にもいくつかの機能を提供します。例: 次の例に示すように、@Value
を使用してシークレット値を取得できます。
@SpringBootApplication
public class PropertySourceApplication implements CommandLineRunner {
@Value("${sampleProperty1}")
private String sampleProperty1;
public static void main(String[] args) {
SpringApplication.run(PropertySourceApplication.class, args);
}
public void run(String[] args) {
System.out.println("sampleProperty1: " + sampleProperty1);
}
}
サービスバス、ストレージ、Active Directory などの Azure サービスの自動構成サポートを提供します。
認証用の Azure Active Directory を使用した Spring Security の統合サポートを提供します。詳細については、Spring Cloud Azure 開発者ガイド (英語) の Spring セキュリティサポート (英語) セクションを参照してください。
Azure キー Vault シークレットとの統合のための Spring @Value
アノテーションサポートを提供します。詳細については、Spring Cloud Azure 開発者ガイド (英語) の秘密管理 (英語) セクションを参照してください。
Azure ストレージサービスの Spring Boot サポートを提供します。詳細については、Spring Cloud Azure 開発者ガイド (英語) のリソース処理 (英語) セクションを参照してください。
Spring Cloud Azure のサポートが必要な場合は、次の方法で支援を求めることができます。