private String sign(final String payload, final boolean encoded) throws Exception {
        val jws = new JsonWebSignature();
        if (encoded) {
            jws.setEncodedPayload(payload);
        } else {
            jws.setPayload(payload);
        }
        jws.setAlgorithmHeaderValue(this.algorithm);
        jws.setAlgorithmConstraints(getAlgorithmConstraints());
        jws.setHeader("typ", mediaType);

        if (this.key instanceof final IdentifiableKey idk) {
            jws.setKey(idk.getKey());
            jws.setKeyIdHeaderValue(idk.getId());
        } else {
            jws.setKey(key);
            FunctionUtils.doIfNotNull(this.keyId, jws::setKeyIdHeaderValue);
        }
        headers.forEach((header, value) -> jws.setHeader(header, value.toString()));
        LOGGER.trace("Signing ID token with key id header value [{}] and algorithm header value [{}]",
            jws.getKeyIdHeaderValue(), jws.getAlgorithmHeaderValue());
        return jws.getCompactSerialization();
    }
Sign claims.

@param claims the claims
@return the string
core/cas-server-core-util-api/src/main/java/org/apereo/cas/util/jwt/JsonWebTokenSigner.java