Package com.bastiaanjansen.jwt
Class JWT.Builder
- java.lang.Object
-
- com.bastiaanjansen.jwt.JWT.Builder
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JWTbuild()Create a new JWT instanceStringsign()Creates a new JWTStringsign(Map<String,?> payload)Creates a new JWT with additional payloadJWT.BuilderwithAudience(String... audience)Add an audience (aud) claim to payloadJWT.BuilderwithClaim(String name, Object value)Add a claim to the payloadJWT.BuilderwithClaim(Map<String,?> claims)Add multiple claims to payloadJWT.BuilderwithContentType(String contentType)Add a content type (cty) claim to headerJWT.BuilderwithExpirationTime(long timeSinceEpoch)Add an expiration time (exp) to payloadJWT.BuilderwithExpirationTime(Date expirationTime)Add an expiration time (exp) to payloadJWT.BuilderwithHeader(Header header)Set the header.JWT.BuilderwithHeader(String name, String value)Add a header claimJWT.BuilderwithID(String id)Add a key ID (kid) to the header claimsJWT.BuilderwithIssuedAt(long timeSinceEpoch)Add an issued at (iat) claim to payloadJWT.BuilderwithIssuedAt(Date issuedAt)Add an issued at (iat) claim to payloadJWT.BuilderwithIssuer(String issuer)Add an issuer (iss) claim to payloadJWT.BuilderwithNotBefore(long timeSinceEpoch)Add a not-before (nbf) claim to payloadJWT.BuilderwithNotBefore(Date notBefore)Add a not-before (nbf) claim to payloadJWT.BuilderwithPayload(Payload payload)Set the payload.JWT.BuilderwithSubject(String subject)Add a subject (sub) claim to payloadJWT.BuilderwithType(String type)Add type (typ) claim to header
-
-
-
Constructor Detail
-
Builder
public Builder(Algorithm algorithm)
Creates a new JWT Builder instance- Parameters:
algorithm- algorithm to use when signing JWT
-
-
Method Detail
-
withType
public JWT.Builder withType(String type)
Add type (typ) claim to header- Parameters:
type- type header- Returns:
- the same builder instance
-
withContentType
public JWT.Builder withContentType(String contentType)
Add a content type (cty) claim to header- Parameters:
contentType- content type value- Returns:
- the same builder instance
-
withIssuer
public JWT.Builder withIssuer(String issuer)
Add an issuer (iss) claim to payload- Parameters:
issuer- issuer value- Returns:
- the same builder instance
-
withSubject
public JWT.Builder withSubject(String subject)
Add a subject (sub) claim to payload- Parameters:
subject- subject value- Returns:
- the same builder instance
-
withAudience
public JWT.Builder withAudience(String... audience)
Add an audience (aud) claim to payload- Parameters:
audience- audience value- Returns:
- the same builder instance
-
withExpirationTime
public JWT.Builder withExpirationTime(Date expirationTime)
Add an expiration time (exp) to payload- Parameters:
expirationTime- expiration time as date- Returns:
- the same builder instance
-
withExpirationTime
public JWT.Builder withExpirationTime(long timeSinceEpoch)
Add an expiration time (exp) to payload- Parameters:
timeSinceEpoch- Milliseconds since January 1, 1970- Returns:
- the same builder instance
-
withNotBefore
public JWT.Builder withNotBefore(Date notBefore)
Add a not-before (nbf) claim to payload- Parameters:
notBefore- not before date object- Returns:
- the same builder instance
-
withNotBefore
public JWT.Builder withNotBefore(long timeSinceEpoch)
Add a not-before (nbf) claim to payload- Parameters:
timeSinceEpoch- Milliseconds since January 1, 1970- Returns:
- the same builder instance
-
withIssuedAt
public JWT.Builder withIssuedAt(Date issuedAt)
Add an issued at (iat) claim to payload- Parameters:
issuedAt- issued at date- Returns:
- the same builder instance
-
withIssuedAt
public JWT.Builder withIssuedAt(long timeSinceEpoch)
Add an issued at (iat) claim to payload- Parameters:
timeSinceEpoch- Milliseconds since January 1, 1970- Returns:
- the same builder instance
-
withID
public JWT.Builder withID(String id)
Add a key ID (kid) to the header claims- Parameters:
id- the key ID- Returns:
- the same builder instance
-
withHeader
public JWT.Builder withHeader(String name, String value)
Add a header claim- Parameters:
name- name of header claimvalue- value of header claim
-
withHeader
public JWT.Builder withHeader(Header header)
Set the header. This will replace the current header- Parameters:
header- Header data- Returns:
- the same builder instance
-
withPayload
public JWT.Builder withPayload(Payload payload)
Set the payload. This will replace the current payload- Parameters:
payload- Payload data- Returns:
- the same builder instance
-
withClaim
public JWT.Builder withClaim(String name, Object value)
Add a claim to the payload- Parameters:
name- name of payload claimvalue- value of payload claim- Returns:
- the same builder instance
-
withClaim
public JWT.Builder withClaim(Map<String,?> claims)
Add multiple claims to payload- Parameters:
claims- the values used in the payload
-
sign
public String sign() throws JWTCreationException
Creates a new JWT- Returns:
- a new JWT
- Throws:
JWTCreationException- when the JWT could not be created
-
sign
public String sign(Map<String,?> payload) throws JWTCreationException
Creates a new JWT with additional payload- Parameters:
payload- map of additional payload- Returns:
- A new JWT
- Throws:
JWTCreationException- when the JWT could not be created
-
build
public JWT build() throws JWTCreationException
Create a new JWT instance- Returns:
- New JWT instance
- Throws:
JWTCreationException
-
-