@Bean
    @RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
    @ConditionalOnMissingBean(name = "delegatedSaml2ClientMongoDbMetadataCustomizer")
    public DelegatedClientFactoryCustomizer delegatedSaml2ClientMongoDbMetadataCustomizer(
        final CasConfigurationProperties casProperties,
        @Qualifier(CasSSLContext.BEAN_NAME) final CasSSLContext casSslContext) {
        return client -> {
            if (client instanceof final SAML2Client saml2Client) {
                val configuration = saml2Client.getConfiguration();
                casProperties.getAuthn().getPac4j().getSaml()
                    .stream()
                    .map(saml -> saml.getMetadata().getServiceProvider().getMongo())
                    .filter(saml -> StringUtils.isNotBlank(saml.getCollection()))
                    .forEach(mongo -> {
                        val factory = new MongoDbConnectionFactory(casSslContext.getSslContext());
                        val mongoClient = factory.buildMongoDbClient(mongo);
                        val mongoTemplate = factory.buildMongoTemplate(mongoClient, mongo);
                        MongoDbConnectionFactory.createCollection(mongoTemplate, mongo.getCollection(), mongo.isDropCollection());
                        val metadataGenerator = new SAML2MongoMetadataGenerator(mongoClient, configuration.getServiceProviderEntityId());
                        metadataGenerator.setMetadataCollection(mongo.getCollection());
                        metadataGenerator.setMetadataDatabase(mongo.getDatabaseName());
                        configuration.setServiceProviderMetadataResource(ResourceUtils.NULL_RESOURCE);
                        configuration.setMetadataGenerator(metadataGenerator);
                    });
            }
        };
    }
This is {@link DelegatedAuthenticationSaml2MongoDbConfiguration}.

@author Misagh Moayyed
@since 7.1.0
support/cas-server-support-pac4j-saml/src/main/java/org/apereo/cas/config/DelegatedAuthenticationSaml2MongoDbConfiguration.java