title: How do I automatically pick the configured SAML Identity provider in a multi-tenant environment to do SSO using Spring SAML
body: <p>I am using Spring SAML in a multi-tenant application to provide SSO. Different tenants use different urls to access the application, and each has a separate Identity Provider configured. How do I automatically assign the correct Identity Provider given the url used to access the application?</p>
  
  <p>Example:</p>
  
  <p>Tenant 1: <a href="http://tenant1.myapp.com">http://tenant1.myapp.com</a></p>
  
  <p>Tenant 2: <a href="http://tenant2.myapp.com">http://tenant2.myapp.com</a></p>
  
  <p>I saw that I can add a parameter idp to the url (<a href="http://tenant1.myapp.com?idp=my.idp.entityid.com">http://tenant1.myapp.com?idp=my.idp.entityid.com</a>) and the SAMLContextProvider will pick the identity provider with that entity id. I developed a database-backed MetadataProvider that takes the tenant hostname as initialisation parameter to fetch the metadata for that tenant form the database linked to that hostname. Now I think I need some way to iterate over the metadata providers to link entityId of the metadata to the hostname. I don't see how I can fetch the entityId of the metadata, though. That would solve my problem.</p>