Java SDK
The Akeyless Java SDK makes it easy for you to integrate your Java applications, libraries, or scripts with the Akeyless Vault platform. The following Java code examples show a typical sequence of how to integrate secrets into your Java applications.
Installation
Maven Users
If your project uses Maven, add the following repository to your Maven configuration file
(by default, it is located at ~/.m2/settings.xml
):
<repository>
<id>central</id>
<url>https://akeyless.jfrog.io/artifactory/akeyless-java</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
Then, add the following dependency to your project's pom.xml
file:
<dependency>
<groupId>io.akeyless</groupId>
<artifactId>akeyless-java</artifactId>
<version>Specify the SDK version here</version>
<scope>compile</scope>
</dependency>
Don't forget to modify the value of the
<version>
element in thepom.xml
file to specify the dependency version you want to include.
Build from Source
Clone the SDK Repository and execute:
mvn clean package
Then, manually install the JAR files under the target
folder.
Configuration
The example below uses the following imported packages:
import io.akeyless.client.ApiClient;
import io.akeyless.client.ApiException;
import io.akeyless.client.Configuration;
import io.akeyless.client.model.Configure;
import io.akeyless.client.model.ConfigureOutput;
import io.akeyless.client.model.ListItems;
import io.akeyless.client.model.ListItemsInPathOutput;
To begin configuring the Java SDK, create and configure an instance of Akeyless Client:
ApiClient client = Configuration.getDefaultApiClient();
client.setBasePath("https://api.akeyless.io");
V2Api api = new V2Api(client);
The Akeyless Gateway URL should be one of the following:
https://api.akeyless.io
: A public instance of the Akeyless Gateway. It is shared between all accounts and does not support Akeyless Zero-Knowledge Encryption.https://akeyless.example.com:8081
: A private Akeyless Gateway deployment. Note that it uses port:8081
.https://akeyless.example.com:8080/v2
: A private Akeyless Gateway deployment that uses the legacy API port with the/v2
path prefix.
Authentication
The Akeyless Java SDK supports multiple authentication methods. The following example uses an API Key for authentication:
Configure body = new Configure();
body.accessId("p-1234").accessKey("<redacted>");
ConfigureOutput out;
out = api.configure(body);
String token = out.getToken();
Using cloud ID
Add this dependency to your project's POM:
<dependency>
<groupId>io.akeyless</groupId>
<artifactId>cloudid</artifactId>
<version>Specify the CloudId package version here</version>
</dependency>
Import the following to your project:
import io.akeyless.client.ApiException;
import io.akeyless.cloudid.CloudProviderFactory;
import io.akeyless.cloudid.CloudIdProvider;
Authenticate using cloud ID
// Use azure_ad/aws_iam/gcp, according to your cloud provider
CloudIdProvider idProvider = CloudProviderFactory.getCloudIdProvider(accessType);
try {
String cloudId = idProvider.getCloudId();
V2Api api = new V2Api(client);
Auth auth = new Auth();
auth.accessId("<Your auth method access id>");
auth.accessType(accessType);
auth.cloudId(cloudId);
AuthOutput out = api.auth(auth);
String token = out.getToken();
} catch (ApiException e) {
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
} catch (Exception e) {
System.err.println("Reason: " + e.getMessage());
e.printStackTrace();
}
Usage Examples
List Items
ListItems listBody = new ListItems();
listBody.setToken(token); // set the token received during authentication
ListItemsInPathOutput listOut = api.listItems(listBody);
System.out.println(listOut.getItems().size());
API Reference
For a detailed API reference, see here.
Updated 4 months ago