question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to add custom field to payload ?

See original GitHub issue

Hi all .

I want to add a custom field(UserType) to my jwt payload. look like that , how can i do that ?

String compactJws = Jwts.builder()
                                 .setId(uid)
                                 .setCustomField("userType",userType)
                                 .setExpiration(getExp())
                                 .signWith(SignatureAlgorithm.HS512,key)
                                 .compact();

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pkalwarcommented, Aug 24, 2017

@bakulinav You can have a custom class like:

public static final class MyJwtToken extends DefaultClaims {
	private static final String USER_ID = "userId";
	// ... your other fields
	public MyJwtToken() {
		super();
	}
	private MyJwtToken(Claims claim) {
		super(claim);
	}
	public String getUserid() {
		return (String) get(USER_ID);
	}
	public void setUserid(String userId) {
		super.setValue(USER_ID, userId);
	}
	//.... other custom getters/setters
}

and use this class like:

MyJwtToken myToken = new MyJwtToken(Jwts.parser().setSigningKey(tokenSigningKey).parseClaimsJws(tokenStr).getBody());

now you can call:

myToken.getUserid();
0reactions
lhazlewoodcommented, Nov 14, 2017

I’d really recommend not subclassing the Claims implementation - you’re tightly coupling your implementation to JJWT’s. Instead, it’s better to use composition over inheritance. In other words, save your custom data as a claim, e.g. .claim("foo", myData). Once you acquire your custom data, you can convert it or inspect it however you wish.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add custom field in webhook custom payload - Alerts
new relic how to add custom violation description in webhook custom payload.
Read more >
How to add additional custom field in streaming event payload?
You can derive it from your subscriber's connection. For some events, you can also use LoginUrl and/or NetworkId . – identigral. May 21...
Read more >
How to include custom field values in Webhook custom payload
Solved: Hi there We have a requirement to pass custom field values to an external application and our plan is to use the...
Read more >
How to add custom fields to WooCommerce webhooks?
In this post, I described how you can add custom fields to they $payload object by using the woocommerce_webhook_payload hook. You can use...
Read more >
Updating custom fields' values - Getting started
With the. PUT request, you need to pass along the key (field API key) as a parameter and add a new value to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found