Custom Data is one of the standout features of Stormpath’s authentication and user management API. It allows you to store up to ten megabytes of unstructured (JSON) data alongside any Stormpath resource. This can be any manner of application-specific user data; our clients use it for everything from custom profile fields to authorization roles, or even references to external data.
Custom Data is backed by a custom-built microservices architecture that indexes every data element you store on an Account. This makes searching that data blazingly fast. The typical processing time is less than 50 milliseconds, even under load.
Learn more about Custom Data Search with Stormpath in our upcoming webinar!
Custom Data in Java
The Stormpath Java SDK provides rich support for the Stormpath REST API, and the Custom Data feature is no exception. In this post, I’ll show you how to use the Java SDK to store and search user profile data with a few simple lines of code!
Start off by following the Java quickstart to get a sample application connected to Stormpath.
Storing and Updating Custom Data
When you create (or update) an Account, use the
CustomData property to add any data you want to the Account:
Account stormtrooper = client.instantiate(Account.class) .setGivenName("Joe") .setSurname("Quickstart_Stormtrooper") .setUsername("tk421") // optional, defaults to email if unset .setEmail("email@example.com") .setPassword("Changeme1"); CustomData customData = account.getCustomData(); customData.put("currentAssignment", "Tatooine"); application.createAccount(account);
If you want to remove some previously-stored data, you can use the
Retrieving Custom Data
Getting the Custom Data for an Account is easy. If you’ve already retrieved the Account, use the
getCustomData method to grab the Custom Data:
CustomData customData = account.getCustomData(); System.out.println(customData.get("currentAssignment"));
Custom Data Search
In order to perform queries, you’ll need to know your Stormpath Directory href. You can find this by logging into the Stormpath Admin Console and finding the Application you created in the Quickstart:
Click on Account Stores on the left side and open up the Directory that was created for the Application. The href is displayed on the page:
In your code, you can retrieve the Directory resource using the Java SDK:
Directory directory = client.getResource("/*your_href_here*/", Directory.class);
Now that you have a reference to the Directory, you can create queries that search the Accounts in that Directory. As a refresher, you can query against any of the built-in Account fields:
AccountList peopleNamedJ = directory.getAccounts(Accounts.where(Accounts.givenName().startsWithIgnoreCase("J")));
Map<String, Object> queryParams = new HashMap<String, Object>(); queryParams.put("userName", "J*"); AccountList accounts = directory.getAccounts(queryParams);
Searching against a value saved in Custom Data is similarly easy, although currently it is only supported in the map form of search:
Map<String, Object> queryParams = new HashMap<String, Object>(); queryParams.put("customData.startDate", "currentAssignment"); AccountList accounts = directory.getAccounts(queryParams);
There’s a lot more you can do with Custom Data in Stormpath! We have full feature documentation available in our Product Guide.
What About My Spring Boot Applications?
While these examples focused on the Stormpath Java SDK, the same objects and features are available in the official Stormpath Spring Boot integration packages!
You can get access to the Stormpath Client through autowiring:
@Autowired Client client to access the relevant Stormpath SDK objects from inside a controller. There are a number of examples to get you started in the Spring Boot Tutorial documentation.
Questions? Leave a comment here, or hit me up on Twitter @afitnerd!