Class JWT.Builder

java.lang.Object
com.bastiaanjansen.jwt.JWT.Builder
Enclosing class:
JWT

public static class JWT.Builder
extends Object
  • Constructor Details

    • Builder

      public Builder​(Algorithm algorithm)
      Creates a new JWT Builder instance
      Parameters:
      algorithm - algorithm to use when signing JWT
  • Method Details

    • 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 claim
      value - 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 claim
      value - 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