Use Warrant in server-side Java projects.
implementation group: 'dev.warrant', name: 'warrant-java', version: '4.2.0'<dependency>
<groupId>dev.warrant</groupId>
<artifactId>warrant-java</artifactId>
<version>4.2.0</version>
</dependency>publicstaticvoidmain(String[] args) throwsWarrantException, IOException {
StringapiKey = "YOUR_KEY";
WarrantClientclient = newWarrantClient(WarrantConfig.withApiKey(apiKey));
// Create users, roles, permissionsUseruser = client.createUser();
RoleadminRole = client.createRole(newRole("admin", "Admin", "Admin role"));
PermissioncreatePermission = client.createPermission(newPermission("create-report", "Create Report""Permission to create reports"));
// RBAC exampleclient.assignPermissionToRole(createPermission, adminRole);
client.assignRoleToUser(adminRole, user);
client.checkUserHasPermission(user, "create-report"); // returns true
}By default, the SDK makes requests to api.warrant.dev. You can override this endpoint, as well as a check endpoint (if using Warrant Edge) via a WarrantConfig object passed to the client:
publicstaticvoidmain(String[] args) throwsWarrantException, IOException {
StringapiKey = "YOUR_KEY";
// Initialize api endpoint to https://api.warrant.dev and check endpoint to "http://localhost:3000" (local Edge instance)WarrantClientclient = newWarrantClient(newWarrantConfig(apiKey, "https://api.warrant.dev", "http://localhost:3000"));
}We’ve used a random API key in these code examples. Replace it with your actual API keys to test this code through your own Warrant account.
For more information on how to use the Warrant API, please refer to the Warrant API reference.
Note that we may release new minor and patch versions of this library with small but backwards-incompatible fixes to the type declarations. These changes will not affect Warrant itself.
Build and run all checks & tests:
./gradlew build