public String encrypt(final Serializable payload) {
        try {
            val jwe = new JsonWebEncryption();
            jwe.setPayload(payload.toString());
            jwe.setAlgorithmHeaderValue(this.algorithm);
            jwe.setEncryptionMethodHeaderParameter(this.encryptionMethod);

            jwe.setAlgorithmConstraints(getAlgorithmConstraints());
            jwe.setContentEncryptionAlgorithmConstraints(getContentEncryptionAlgorithmConstraints());
            jwe.setContentTypeHeaderValue("JWT");
            jwe.setHeader("typ", "JWT");
            if (this.key instanceof final IdentifiableKey idk) {
                jwe.setKeyIdHeaderValue(idk.getId());
                jwe.setKey(idk.getKey());
            } else {
                FunctionUtils.doIfNotNull(this.keyId, jwe::setKeyIdHeaderValue);
                jwe.setKey(this.key);
            }
            headers.forEach((name, v) -> jwe.setHeader(name, v.toString()));
            LOGGER.trace("Encrypting via [{}]", encryptionMethod);
            return jwe.getCompactSerialization();
        } catch (final Exception e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }
Encrypt object value.

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