@PostMapping(value = "/logout/post", produces = MediaType.TEXT_HTML_VALUE)
    @Operation(summary = "Produce SAML2 logout request for the given SAML2 SP",
               parameters = @Parameter(name = SamlProtocolConstants.PARAMETER_ENTITY_ID, required = true, description = "The entity id"))
    public ResponseEntity<Object> produceLogoutRequestPost(
        @RequestParam(SamlProtocolConstants.PARAMETER_ENTITY_ID) final String entityId,
        final HttpServletResponse response) throws Exception {
        val selectedService = serviceFactory.createService(entityId);
        val registeredService = servicesManager.findServiceBy(selectedService, SamlRegisteredService.class);
        RegisteredServiceAccessStrategyUtils.ensureServiceAccessIsAllowed(selectedService, registeredService);

        val logoutRequest = saml20ObjectBuilder.newSamlObject(LogoutRequest.class);
        logoutRequest.setID(RandomUtils.randomAlphabetic(4));
        val issuer = saml20ObjectBuilder.newSamlObject(Issuer.class);
        issuer.setValue(entityId);

        val criteriaSet = new CriteriaSet();
        criteriaSet.add(new EntityIdCriterion(casProperties.getAuthn().getSamlIdp().getCore().getEntityId()));
        criteriaSet.add(new EntityRoleCriterion(SPSSODescriptor.DEFAULT_ELEMENT_NAME));
        criteriaSet.add(new BindingCriterion(List.of(SAMLConstants.SAML2_POST_BINDING_URI)));

        val result = samlIdPMetadataResolver.resolveSingle(criteriaSet);
        val sloEndpointDestination = result.getIDPSSODescriptor(SAMLConstants.SAML20P_NS)
            .getEndpoints(SingleLogoutService.DEFAULT_ELEMENT_NAME)
            .stream()
            .filter(endpoint -> SAMLConstants.SAML2_POST_BINDING_URI.equals(endpoint.getBinding()))
            .findFirst()
            .orElseThrow()
            .getLocation();

        val nameId = saml20ObjectBuilder.newSamlObject(NameID.class);
        nameId.setValue(UUID.randomUUID().toString());
        logoutRequest.setNameID(nameId);

        logoutRequest.setIssuer(issuer);
        logoutRequest.setDestination(sloEndpointDestination);
        logoutRequest.setIssueInstant(Instant.now(Clock.systemUTC()).minusSeconds(10));

        val encoder = new HTTPPostEncoder();
        encoder.setVelocityEngine(saml20ObjectBuilder.getOpenSamlConfigBean().getVelocityEngine());
        encoder.setHttpServletResponseSupplier(() -> response);
        val messageContext = new MessageContext();
        SAMLBindingSupport.setRelayState(messageContext, UUID.randomUUID().toString());
        val peerEntityContext = messageContext.ensureSubcontext(SAMLPeerEntityContext.class);
        val endpointContext = peerEntityContext.ensureSubcontext(SAMLEndpointContext.class);

        val endpoint = saml20ObjectBuilder.newSamlObject(SingleSignOnService.class);
        endpoint.setLocation(sloEndpointDestination);
        endpointContext.setEndpoint(endpoint);

        val encodedRequest = EncodingUtils.encodeBase64(SamlUtils.transformSamlObject(
            saml20ObjectBuilder.getOpenSamlConfigBean(), logoutRequest, true).toString());
        response.setHeader("LogoutRequest", encodedRequest);
        
        messageContext.setMessage(logoutRequest);
        encoder.setMessageContext(messageContext);
        encoder.initialize();
        encoder.encode();

        return ResponseEntity.ok().build();
    }
Produce logout request post.

@param entityId the entity id
@param response the response
@return the response entity
@throws Exception the exception
support/cas-server-support-saml-idp-web/src/main/java/org/apereo/cas/support/saml/web/idp/profile/sso/SSOSamlIdPPostProfileHandlerEndpoint.java