Over a million developers have joined DZone.

Custom Data Search for Powerful Identity Management in Java

Custom Data is one of the standout features of Stormpath’s authentication and user management API. Read on to find out more.

· Java Zone

The Java Zone is brought to you by Stormpath—offering a pre-built Identity API for developers. On 11/3 at 10AM PT, join Stormpath Developer Evangelist, Micah Silverman, to dive deep into using JWTs to protect microservices from CSRF and more. Register here!

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)
    .setUsername("tk421")  // optional, defaults to email if unset

CustomData customData = account.getCustomData();
customData.put("currentAssignment", "Tatooine");


If you want to remove some previously-stored data, you can use the Remove method:

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();

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:

Custom Data Search for .NET

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:

Custom Data Search in .NET

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 = 

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!

Building Identity Management, including authentication and authorization? Try Stormpath! Our REST API and robust Java SDK support can eliminate your security risk and can be implemented in minutes. Sign up, and never build auth again!

java,stormpath,java sdk,data,spring boot,custom data,feature,search,sdk,directory

Published at DZone with permission of Micah Silverman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}