You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Get started with the Azure Key Vault Certificate client library for Java. Follow the steps below to install the package and try out example code for basic tasks.
- An Azure subscription - [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
26
28
-[Java Development Kit (JDK)](/java/azure/jdk/) version 8 or above
27
29
-[Apache Maven](https://maven.apache.org)
@@ -30,23 +32,26 @@ Additional resources:
30
32
This quickstart assumes you are running [Azure CLI](/cli/azure/install-azure-cli) and [Apache Maven](https://maven.apache.org) in a Linux terminal window.
31
33
32
34
## Setting up
35
+
33
36
This quickstart is using the Azure Identity library with Azure CLI to authenticate user to Azure Services. Developers can also use Visual Studio or Visual Studio Code to authenticate their calls, for more information, see [Authenticate the client with Azure Identity client library](/java/api/overview/azure/identity-readme).
34
37
35
38
### Sign in to Azure
39
+
36
40
1. Run the `login` command.
37
41
38
-
```azurecli-interactive
39
-
az login
40
-
```
42
+
```azurecli-interactive
43
+
az login
44
+
```
41
45
42
46
If the CLI can open your default browser, it will do so and load an Azure sign-in page.
43
47
44
48
Otherwise, open a browser page at [https://aka.ms/devicelogin](https://aka.ms/devicelogin) and enter the
45
49
authorization code displayed in your terminal.
46
50
47
-
2. Sign in with your account credentials in the browser.
51
+
1. Sign in with your account credentials in the browser.
48
52
49
53
### Create a new Java console app
54
+
50
55
In a console window, use the `mvn` command to create a new Java console app with the name `akv-certificates-java`.
51
56
52
57
```console
@@ -88,6 +93,7 @@ cd akv-certificates-java
88
93
```
89
94
90
95
### Install the package
96
+
91
97
Open the *pom.xml* file in your text editor. Add the following dependency elements to the group of dependencies.
92
98
93
99
```xml
@@ -105,39 +111,49 @@ Open the *pom.xml* file in your text editor. Add the following dependency elemen
105
111
```
106
112
107
113
### Create a resource group and key vault
114
+
108
115
[!INCLUDE [Create a resource group and key vault](../../../includes/key-vault-rg-kv-creation.md)]
109
116
110
117
#### Grant access to your key vault
118
+
111
119
Create an access policy for your key vault that grants certificate permissions to your user account.
112
120
113
121
```azurecli
114
122
az keyvault set-policy --name <your-key-vault-name> --upn [email protected] --certificate-permissions delete get list create purge
115
123
```
116
124
117
125
#### Set environment variables
126
+
118
127
This application is using your key vault name as an environment variable called `KEY_VAULT_NAME`.
119
128
120
129
Windows
130
+
121
131
```cmd
122
132
set KEY_VAULT_NAME=<your-key-vault-name>
123
133
````
134
+
124
135
Windows PowerShell
136
+
125
137
```powershell
126
138
$Env:KEY_VAULT_NAME="<your-key-vault-name>"
127
139
```
128
140
129
141
macOS or Linux
142
+
130
143
```cmd
131
144
export KEY_VAULT_NAME=<your-key-vault-name>
132
145
```
133
146
134
147
## Object model
148
+
135
149
The Azure Key Vault Certificate client library for Java allows you to manage certificates. The [Code examples](#code-examples) section shows how to create a client, create a certificate, retrieve a certificate, and delete a certificate.
136
150
137
151
The entire console app is [below](#sample-code).
138
152
139
153
## Code examples
154
+
140
155
### Add directives
156
+
141
157
Add the following directives to the top of your code:
In this quickstart, a logged in user is used to authenticate to Key Vault, which is preferred method for local development. For applications deployed to Azure, a Managed Identity should be assigned to an App Service or Virtual Machine. For more information, see [Managed Identity Overview](../../active-directory/managed-identities-azure-resources/overview.md).
158
173
159
-
In the example below, the name of your key vault is expanded to the key vault URI, in the format "https://\<your-key-vault-name\>.vault.azure.net". This example is using the ['DefaultAzureCredential()'](/java/api/com.azure.identity.defaultazurecredential) class, which allows to use the same code across different environments with different options to provide identity. For more information, see [Default Azure Credential Authentication](/java/api/overview/azure/identity-readme).
174
+
Application requests to most Azure services must be authorized. Using the [DefaultAzureCredential](/java/api/com.azure.identity.defaultazurecredential) is the recommended approach for implementing passwordless connections to Azure services in your code. `DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code.
175
+
176
+
In this quickstart, `DefaultAzureCredential` authenticates to key vault using the credentials of the local development user logged into the Azure CLI. When the application is deployed to Azure, the same `DefaultAzureCredential` code can automatically discover and use a managed identity that is assigned to an App Service, Virtual Machine, or other services. For more information, see [Managed Identity Overview](/azure/active-directory/managed-identities-azure-resources/overview).
177
+
178
+
In this example, the name of your key vault is expanded to the key vault URI, in the format `https://<your-key-vault-name>.vault.azure.net`. For more information about authenticating to key vault, see [Developer's Guide](/azure/key-vault/general/developers-guide#authenticate-to-key-vault-in-code).
@@ -169,6 +188,7 @@ CertificateClient certificateClient = new CertificateClientBuilder()
169
188
```
170
189
171
190
### Save a secret
191
+
172
192
Now that your application is authenticated, you can create a certificate in your key vault using the `certificateClient.beginCreateCertificate` method. This requires a name for the certificate and a certificate policy -- we've assigned the value "myCertificate" to the `certificateName` variable in this sample and use a default policy.
173
193
174
194
Certificate creation is a long running operation, for which you can poll its progress or wait for it to complete.
You can now access the details of the retrieved certificate with operations like `retrievedCertificate.getName`, `retrievedCertificate.getProperties`, etc. As well as its contents `retrievedCertificate.getCer`.
196
217
197
218
### Delete a certificate
219
+
198
220
Finally, let's delete the certificate from your key vault with the `certificateClient.beginDeleteCertificate` method, which is also a long running operation.
In this quickstart you created a key vault, created a certificate, retrieved it, and then deleted it. To learn more about Key Vault and how to integrate it with your applications, continue on to the articles below.
269
294
270
295
- Read an [Overview of Azure Key Vault](../general/overview.md)
- An Azure subscription - [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
27
28
-[Java Development Kit (JDK)](/java/azure/jdk/) version 8 or above
28
29
-[Apache Maven](https://maven.apache.org)
@@ -31,22 +32,25 @@ Additional resources:
31
32
This quickstart assumes you're running [Azure CLI](/cli/azure/install-azure-cli) and [Apache Maven](https://maven.apache.org) in a Linux terminal window.
32
33
33
34
## Setting up
35
+
34
36
This quickstart is using the Azure Identity library with Azure CLI to authenticate user to Azure Services. Developers can also use Visual Studio or Visual Studio Code to authenticate their calls, for more information, see [Authenticate the client with Azure Identity client library](/java/api/overview/azure/identity-readme).
35
37
36
38
### Sign in to Azure
39
+
37
40
1. Run the `login` command.
38
41
39
-
```azurecli-interactive
40
-
az login
41
-
```
42
+
```azurecli-interactive
43
+
az login
44
+
```
42
45
43
46
If the CLI can open your default browser, it will do so and load an Azure sign-in page.
44
47
45
48
Otherwise, open a browser page at [https://aka.ms/devicelogin](https://aka.ms/devicelogin) and enter the authorization code displayed in your terminal.
46
49
47
-
2. Sign in with your account credentials in the browser.
50
+
1. Sign in with your account credentials in the browser.
48
51
49
52
### Create a new Java console app
53
+
50
54
In a console window, use the `mvn` command to create a new Java console app with the name `akv-keys-java`.
51
55
52
56
```console
@@ -88,6 +92,7 @@ cd akv-keys-java
88
92
```
89
93
90
94
### Install the package
95
+
91
96
Open the *pom.xml* file in your text editor. Add the following dependency elements to the group of dependencies.
92
97
93
98
```xml
@@ -105,39 +110,49 @@ Open the *pom.xml* file in your text editor. Add the following dependency elemen
105
110
```
106
111
107
112
### Create a resource group and key vault
113
+
108
114
[!INCLUDE [Create a resource group and key vault](../../../includes/key-vault-rg-kv-creation.md)]
109
115
110
116
#### Grant access to your key vault
117
+
111
118
Create an access policy for your key vault that grants key permissions to your user account.
112
119
113
120
```azurecli
114
121
az keyvault set-policy --name <your-key-vault-name> --upn [email protected] --key-permissions delete get list create purge
115
122
```
116
123
117
124
#### Set environment variables
125
+
118
126
This application is using your key vault name as an environment variable called `KEY_VAULT_NAME`.
119
127
120
128
Windows
129
+
121
130
```cmd
122
131
set KEY_VAULT_NAME=<your-key-vault-name>
123
132
````
133
+
124
134
Windows PowerShell
135
+
125
136
```powershell
126
137
$Env:KEY_VAULT_NAME="<your-key-vault-name>"
127
138
```
128
139
129
140
macOS or Linux
141
+
130
142
```cmd
131
143
export KEY_VAULT_NAME=<your-key-vault-name>
132
144
```
133
145
134
146
## Object model
147
+
135
148
The Azure Key Vault Key client library for Java allows you to manage keys. The [Code examples](#code-examples) section shows how to create a client, create a key, retrieve a key, and delete a key.
136
149
137
150
The entire console app is supplied in [Sample code](#sample-code).
138
151
139
152
## Code examples
153
+
140
154
### Add directives
155
+
141
156
Add the following directives to the top of your code:
In this quickstart, a logged in user is used to authenticate to Key Vault, which is preferred method for local development. For applications deployed to Azure, a Managed Identity should be assigned to an App Service or Virtual Machine. For more information, see [Managed Identity Overview](../../active-directory/managed-identities-azure-resources/overview.md).
171
+
Application requests to most Azure services must be authorized. Using the [DefaultAzureCredential](/java/api/com.azure.identity.defaultazurecredential) class is the recommended approach for implementing passwordless connections to Azure services in your code. `DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code.
172
+
173
+
In this quickstart, `DefaultAzureCredential` authenticates to key vault using the credentials of the local development user logged into the Azure CLI. When the application is deployed to Azure, the same `DefaultAzureCredential` code can automatically discover and use a managed identity that is assigned to an App Service, Virtual Machine, or other services. For more information, see [Managed Identity Overview](/azure/active-directory/managed-identities-azure-resources/overview).
157
174
158
-
In this example, the name of your key vault is expanded to the key vault URI, in the format `https://<your-key-vault-name>.vault.azure.net`. This example is using the ['DefaultAzureCredential()'](/java/api/com.azure.identity.defaultazurecredential) class, which allows to use the same code across different environments with different options to provide identity. For more information, see [Default Azure Credential Authentication](/java/api/overview/azure/identity-readme).
175
+
In this example, the name of your key vault is expanded to the key vault URI, in the format `https://<your-key-vault-name>.vault.azure.net`. For more information about authenticating to key vault, see [Developer's Guide](/azure/key-vault/general/developers-guide#authenticate-to-key-vault-in-code).
@@ -168,6 +185,7 @@ KeyClient keyClient = new KeyClientBuilder()
168
185
```
169
186
170
187
### Create a key
188
+
171
189
Now that your application is authenticated, you can create a key in your key vault using the `keyClient.createKey` method. This requires a name for the key and a key type. We've assigned the value "myKey" to the `keyName` variable and use a an RSA `KeyType` in this sample.
172
190
173
191
```java
@@ -181,6 +199,7 @@ az keyvault key show --vault-name <your-unique-key-vault-name> --name myKey
181
199
```
182
200
183
201
### Retrieve a key
202
+
184
203
You can now retrieve the previously created key with the `keyClient.getKey` method.
In this quickstart, you created a key vault, created a key, retrieved it, and then deleted it. To learn more about Key Vault and how to integrate it with your applications, continue on to these articles.
268
291
269
292
- Read an [Overview of Azure Key Vault](../general/overview.md)
0 commit comments