public class JsonPathFilterQuery
extends java.lang.Object
{
"aud": ["uma_authorization", "kafka"],
"iss": "https://auth-server/sso",
"iat": 0,
"exp": 600,
"sub": "username",
"custom": "custom-value",
"roles": {
"client-roles": {
"kafka": ["kafka-user"]
}
},
"custom-level": 9
}
Some examples of valid queries are:
@.custom == 'custom-value'
@.sub && @.custom == 'custom-value'
@.custom == 'custom-value' || 'kafka' in @.aud
@.custom == 'custom-value' && @.roles.client-roles.kafka
@.['custom'] == 'custom-value'
@.custom in ['custom-value','custom-value2','custom-value3']
@.custom == 'custom-value' || @.custom == 'custom-value2' || @.custom == 'custom-value2'
@.custom-level && @.custom-level nin [1,2,3]
"kafka-user" in @.['roles'].['client-roles'].['kafka']
@.roles.client-roles.kafka && "kafka-admin" nin @.roles.client-roles.kafka
@.custom =~ /^CUSTOM-.+$/i
@.custom == 'custom-value' && ('kafka' in @.aud || 'kafka-user' in @.roles.client-roles.kafka)
@.custom =~ /^custom-.+/
@.iss =~ /^https:\/\/auth-server\/.+/
! @.internal_id
See Jayway JsonPath project for full syntax.
This class takes a JSONPath filter expression and rewrites it so it can be applied to the parsed JWT token as a filtering selector.
The query is rewritten into JSONPath as:
$[*][?(QUERY)]For example: '$[*][?(@.custom == 'custom value')]' The JWT token is wrapped into another JSON object as follows:
{
"token": {
"sub": "username",
"iss": "https://auth-server/sso",
"custom": "custom value",
...
}
}
Usage:
JsonPathFilterQuery query = new JsonPathFilterQuery("@.custom == 'value'");
boolean match = query.matches(jsonObject);
Query is parsed in the first line and any error during parsing results
in JsonPathQueryException.
Matching is thread safe. The normal usage pattern is to initialise the JsonPathFilterQuery object once,
and query it many times concurrently against json objects.
In addition to filtering this helper can also be used to apply JsonPath query to extract a result containing the matching keys.| Modifier and Type | Method and Description |
|---|---|
boolean |
matches(com.fasterxml.jackson.databind.JsonNode jsonObject)
Match the json objects against the filter query.
|
static JsonPathFilterQuery |
parse(java.lang.String query)
Construct a new JsonPathFilterQuery
|
java.lang.String |
toString() |
public static JsonPathFilterQuery parse(java.lang.String query)
query - The query using the JSONPath filter syntaxpublic boolean matches(com.fasterxml.jackson.databind.JsonNode jsonObject)
jsonObject - Jackson DataBind objectpublic java.lang.String toString()
toString in class java.lang.ObjectCopyright © 2024. All rights reserved.