protected static String getEntityIdFromRequest(final RegisteredServiceAttributeReleasePolicyContext context) {
        val selectedService = context.getService();
        val request = HttpRequestUtils.getHttpServletRequestFromRequestAttributes();
        if (request == null || selectedService == null) {
            LOGGER.debug("No http request could be identified to locate the entity id");
            return null;
        }
        LOGGER.debug("Attempting to determine entity id for service [{}]", selectedService);
        val entityIdAttribute = selectedService.getAttributeAs(SamlProtocolConstants.PARAMETER_ENTITY_ID, List.class);
        if (entityIdAttribute != null && !entityIdAttribute.isEmpty()) {
            LOGGER.debug("Found entity id [{}] as a service attribute", entityIdAttribute);
            return CollectionUtils.firstElement(entityIdAttribute).map(Object::toString).orElseThrow();
        }
        val providerIdAttribute = selectedService.getAttributeAs(SamlIdPConstants.PROVIDER_ID, List.class);
        if (providerIdAttribute != null && !providerIdAttribute.isEmpty()) {
            LOGGER.debug("Found provider entity id [{}] as a service attribute", providerIdAttribute);
            return CollectionUtils.firstElement(providerIdAttribute).map(Object::toString).orElseThrow();
        }
        val samlRequest = selectedService.getAttributeAs(SamlProtocolConstants.PARAMETER_SAML_REQUEST, List.class);
        if (samlRequest != null && !samlRequest.isEmpty()) {
            val applicationContext = context.getApplicationContext();
            val sessionStore = applicationContext.getBean("samlIdPDistributedSessionStore", SessionStore.class);

            val resolver = applicationContext.getBean(SamlRegisteredServiceCachingMetadataResolver.BEAN_NAME,
                SamlRegisteredServiceCachingMetadataResolver.class);
            val attributeValue = CollectionUtils.firstElement(samlRequest).map(Object::toString).orElseThrow();
            val openSamlConfigBean = resolver.getOpenSamlConfigBean();
            val authnRequest = SamlIdPSessionManager.of(openSamlConfigBean, sessionStore)
                .fetch(RequestAbstractType.class, attributeValue);
            openSamlConfigBean.logObject(authnRequest);
            val issuer = SamlIdPUtils.getIssuerFromSamlObject(authnRequest);
            LOGGER.debug("Found entity id [{}] from SAML request issuer", issuer);
            return issuer;
        }
        val entityId = request.getParameter(SamlProtocolConstants.PARAMETER_ENTITY_ID);
        if (StringUtils.isNotBlank(entityId)) {
            LOGGER.debug("Found entity id [{}] as a request parameter", entityId);
            return entityId;
        }
        val svcParam = request.getParameter(CasProtocolConstants.PARAMETER_SERVICE);
        return FunctionUtils.doIf(StringUtils.isNotBlank(svcParam),
            () -> FunctionUtils.doAndHandle(o -> {
                val builder = new URIBuilder(svcParam);
                return builder.getQueryParams()
                    .stream()
                    .filter(p -> p.getName().equals(SamlProtocolConstants.PARAMETER_ENTITY_ID))
                    .map(NameValuePair::getValue)
                    .findFirst()
                    .orElse(StringUtils.EMPTY);
            }, throwable -> {
                LoggingUtils.error(LOGGER, throwable);
                return null;
            })
                .apply(svcParam),
            () -> null).get();
    }
This is {@link BaseSamlRegisteredServiceAttributeReleasePolicy}.

@author Misagh Moayyed
@since 5.1.0
support/cas-server-support-saml-idp-core/src/main/java/org/apereo/cas/support/saml/services/BaseSamlRegisteredServiceAttributeReleasePolicy.java