diff --git a/src/main/java/io/jsonwebtoken/JwtBuilder.java b/src/main/java/io/jsonwebtoken/JwtBuilder.java index 998e832d..4cb012f2 100644 --- a/src/main/java/io/jsonwebtoken/JwtBuilder.java +++ b/src/main/java/io/jsonwebtoken/JwtBuilder.java @@ -101,6 +101,18 @@ public interface JwtBuilder extends ClaimsMutator { */ JwtBuilder setClaims(Map claims); + /** + * Adds all given name/value pairs to the JSON Claims in the payload. If a Claims instance does not yet exist at the + * time this method is called, one will be created automatically before applying the name/value pairs. + * + *

The payload and claims properties are mutually exclusive - only one of the two may be used.

+ * + * @param claims the JWT claims to be added to the JWT body. + * @return the builder for method chaining. + * @since 0.8 + */ + JwtBuilder addClaims(Map claims); + /** * Sets the JWT Claims * iss (issuer) value. A {@code null} value will remove the property from the Claims. diff --git a/src/main/java/io/jsonwebtoken/impl/DefaultJwtBuilder.java b/src/main/java/io/jsonwebtoken/impl/DefaultJwtBuilder.java index 76d3a2c3..25b48d62 100644 --- a/src/main/java/io/jsonwebtoken/impl/DefaultJwtBuilder.java +++ b/src/main/java/io/jsonwebtoken/impl/DefaultJwtBuilder.java @@ -17,14 +17,7 @@ package io.jsonwebtoken.impl; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import io.jsonwebtoken.Claims; -import io.jsonwebtoken.CompressionCodec; -import io.jsonwebtoken.Header; -import io.jsonwebtoken.JwsHeader; -import io.jsonwebtoken.JwtBuilder; -import io.jsonwebtoken.JwtParser; -import io.jsonwebtoken.Jwts; -import io.jsonwebtoken.SignatureAlgorithm; +import io.jsonwebtoken.*; import io.jsonwebtoken.impl.crypto.DefaultJwtSigner; import io.jsonwebtoken.impl.crypto.JwtSigner; import io.jsonwebtoken.lang.Assert; @@ -148,6 +141,12 @@ public class DefaultJwtBuilder implements JwtBuilder { return this; } + @Override + public JwtBuilder addClaims(Map claims) { + ensureClaims().putAll(claims); + return this; + } + @Override public JwtBuilder setIssuer(String iss) { if (Strings.hasText(iss)) { diff --git a/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtBuilderTest.groovy b/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtBuilderTest.groovy index e4c6cdbe..f4a87fc3 100644 --- a/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtBuilderTest.groovy +++ b/src/test/groovy/io/jsonwebtoken/impl/DefaultJwtBuilderTest.groovy @@ -74,6 +74,26 @@ class DefaultJwtBuilderTest { assertSame b.claims, c } + @Test + void testAddClaims() { + def b = new DefaultJwtBuilder() + def c = Jwts.claims([initial: 'initial']) + b.setClaims(c) + def c2 = [foo: 'bar', baz: 'buz'] + b.addClaims(c2) + assertEquals 'initial', b.claims.get('initial') + assertEquals 'bar', b.claims.get('foo') + } + + @Test + void testAddClaimsWithoutInitializing() { + def b = new DefaultJwtBuilder() + def c = [foo: 'bar', baz: 'buz'] + b.addClaims(c) + assertNotNull b.claims + assertEquals b.claims, c + } + @Test void testClaim() { def b = new DefaultJwtBuilder()