Class JwtHeader

  • Direct Known Subclasses:
    JwtSecureHeader

    public abstract class JwtHeader
    extends JWObject
    A base implementation class for JWT Headers.

    Provides methods to set header parameters for all types of JWT Headers.

    Since:
    2.0.0
    See Also:
    JwsHeader, JweHeader
    • Constructor Detail

      • JwtHeader

        public JwtHeader()
        Constructs a new JwtHeader, with the "typ" parameter set to "JWT".
      • JwtHeader

        public JwtHeader​(Map<String,​Object> headers)
        Constructs a new JwtHeader, with its parameters set to the contents of the given Map.
        Parameters:
        headers - A Map containing the parameters to be set in the header.
    • Method Detail

      • setType

        public void setType​(JwtType jwtType)
        Sets the type of JWT this header represents.

        For non-nested JWTs then the "JWT" type is RECOMMENDED to be used but it is OPTIONAL to set the "typ" property. For nested signed or encrypted JWTs the JWT type MUST be "JWS" and "JWE" respectively and the "typ" property MUST be set.

        Parameters:
        jwtType - The JwtType.
        See Also:
        JwtType
      • getType

        public JwtType getType()
        Gets the type of JWT this header represents.
        Returns:
        The JwtType.
      • setAlgorithm

        public void setAlgorithm​(Algorithm algorithm)
        Sets the algorithm used to perform cryptographic signing and/or encryption on the JWT.
        Parameters:
        algorithm - The Algorithm.
      • getAlgorithm

        public abstract Algorithm getAlgorithm()
        Gets the Algorithm set in the JWT header.
        Returns:
        The Algorithm.
      • getAlgorithmString

        protected String getAlgorithmString()
        Gets the string representation of the Algorithm set in the JWT header.
        Returns:
        The algorithm as a String.
      • setParameter

        public void setParameter​(String key,
                                 Object value)
        Sets a header parameter with the specified key and value.

        If the key matches one of the reserved header parameter names, then the relevant set method is called to set that header parameter with the specified value.

        Parameters:
        key - The key of the header parameter.
        value - The value of the header parameter.
      • setParameters

        public void setParameters​(Map<String,​Object> headers)
        Sets header parameters using the values contained in the specified map.
        Parameters:
        headers - The Map to use to set header parameters.
        See Also:
        setParameter(String, Object)
      • getParameter

        public Object getParameter​(String key)
        Gets a header parameter for the specified key.

        If the key matches one of the reserved header parameter names, then the relevant get method is called to get that header parameter.

        Parameters:
        key - The header parameter key.
        Returns:
        The value stored against the header parameter key.
      • getParameter

        public <T> T getParameter​(String key,
                                  Class<T> clazz)
        Gets a header parameter for the specified key and then casts it to the specified type.
        Type Parameters:
        T - The required type for the header parameter value.
        Parameters:
        key - The header parameter key.
        clazz - The class of the required type.
        Returns:
        The value stored against the header parameter key.
        See Also:
        getParameter(String)
      • getParameters

        public Map<String,​Object> getParameters()
        Returns this JwtHeader's parameters.
        Returns:
        Map of this JwtHeader's parameters.
      • build

        public String build()
        Builds the JWT's header into a String representation of a JSON object.
        Returns:
        A JSON string.