Blog

    Spring Security Multi Provider Authentication for Healthcare Backends

    By GauravJuly 1, 20269 mins read
    On this page

    A healthcare backend that starts with one login screen rarely keeps it. The first hospital customer wants staff to sign in through its own identity provider, the second uses SAML instead of OpenID Connect, and patients need a login of their own with a different risk profile.

    This post is for engineering leads building Spring Boot services used by hospitals and clinics. It covers how to route each login to the right provider, turn every login into one internal user, keep patient and staff identities apart, add MFA and test the setup.

    What is Spring Security multi provider authentication?

    Building software for several hospital customers?

    See how we build provider software that fits each organization's identity, workflow and access rules.

    Spring Security multi provider authentication is a setup in which one Spring Boot backend accepts logins from several identity providers, such as each hospital's OIDC or SAML service and a separate patient identity store, and maps every login to one internal user model. Agnotic Technologies designs these setups so each provider is configuration, not code.

    The goal is not a page full of login buttons. It is one set of authorization rules that behaves the same whichever provider vouched for the user.

    Why does one healthcare backend need several providers?

    A healthcare backend needs several providers because its users belong to different organizations, each with its own identity system. Hospitals want staff to use accounts they already manage, so access ends the day someone leaves.

    • Hospital staff: sign in through the hospital's workforce identity provider over OpenID Connect or SAML 2.0.
    • Patients: sign in to an identity store you operate, with email, phone and recovery flows built for the public.
    • Your support and operations team: sign in through your company directory with stricter MFA.
    • Other systems: call the API with client credentials, not with a person's login.

    Each group has different recovery, session and audit needs. Treating them as one population is where most identity gaps begin.

    How does Spring Security tell tenants apart?

    Spring Security tells tenants apart by a tenant key you choose, such as the token issuer, a subdomain or the domain of a work email. For APIs that receive JWTs, the issuer claim is the most reliable key because it is signed into the token.

    Spring Security treats a resource server as multi-tenant when there are multiple strategies for verifying a bearer token, keyed by a tenant identifier, and its JwtIssuerAuthenticationManagerResolver can tell tenants apart by the issuer claim in signed JWTs.

    • Issuer: the iss claim maps to one registered tenant, and unknown issuers are rejected before any further work.
    • Subdomain: a hospital-specific host name selects that hospital's registration on the browser login.
    • Email domain: the user types a work email and its domain selects the provider, which helps when hospitals share one URL.

    Keep the tenant registry in a database table or configuration service, not in code. Adding a hospital should be a reviewed configuration change, not a release.

    OIDC and SAML side by side

    Many hospitals still run SAML 2.0 for workforce single sign-on, while newer setups and most patient flows use OpenID Connect. A backend that serves several hospitals usually has to accept both.

    In Spring Security's SAML 2.0 login, a RelyingPartyRegistrationRepository holds the asserting party and relying party metadata, and when more than one asserting party is configured the user is first redirected to a picker page.

    • OIDC: a discovery document, the authorization code flow, an ID token with claims and signing keys published at a JWKS endpoint.
    • SAML 2.0: metadata exchange, signed assertions, attribute statements and certificates that expire on the hospital's schedule.
    • Shared result: both end in the same internal session or token, so business code never checks which protocol was used.

    Where many hospitals use SAML, replace the picker page with your own routing by email domain. Staff should not have to choose their employer from a list.

    How do logins become one internal user?

    Logins become one internal user through a mapping step that runs after the provider succeeds and before any business code. The step looks up the external subject and issuer, links them to an internal account and loads roles from your own database.

    Spring Boot healthcare login flow from hospital OIDC, hospital SAML and patient identity providers into a tenant resolver and token checks, then identity mapping on issuer plus subject, then separate staff and patient APIs, with a tenant registry lane
    • Link key: issuer plus subject, never email, because addresses change and can be reissued.
    • Roles: loaded from your database and care relationships, not from directory group claims you cannot audit.
    • Audit fields: the provider, tenant and authentication method, recorded on the session and in the access log.

    Group claims from a hospital directory can still help with provisioning. Treat them as input to a reviewed mapping, never as permissions on their own.

    Should patients and staff share a login?

    No. Patients and staff should use separate identity providers, separate login pages and separate token audiences, even when one person is both.

    A nurse who is also a patient at her hospital should hold two accounts. If one account carried both roles, a single flawed role check could expose patient records through the wrong app.

    • Audiences: staff tokens and patient tokens name different API audiences, and each API rejects the other kind.
    • Session rules: short idle timeouts for staff on shared workstations, different rules for patients on personal phones.
    • Recovery: patients reset through a verified email or phone, staff through their hospital help desk.

    Where does MFA fit?

    MFA belongs at the identity provider wherever one exists, and in your own identity store only for the users it owns. If a hospital already enforces MFA, your backend should confirm it happened rather than prompt a second time.

    The HIPAA Security Rule's person or entity authentication standard requires procedures to verify that a person or entity seeking access to electronic protected health information is the one claimed.

    • Hospital staff: ask the provider to report the method used, for example through the amr or acr claims in OIDC, and refuse logins that lack it where the contract requires MFA.
    • Patients: offer an authenticator app or passkey, keep text codes as a fallback, and step up before sensitive changes.
    • Your own team: passkeys or hardware keys for anyone with production or support access.

    Failure points with several providers

    Multi provider setups usually fail at the joins between systems rather than inside Spring Security itself. These are the failures worth designing out early.

    • Email as the join key: a hospital reissues an address and a new employee inherits an old account.
    • Trusting every issuer: a filter accepts any well-formed JWT, so a token from one tenant works on another.
    • Expired SAML certificates: a hospital rotates its signing certificate and every staff login at that site fails.
    • Roles from claims: a directory group renamed by the hospital silently removes or grants access.
    • Shared cookies: patient and staff apps on sibling subdomains read each other's session cookies.

    Most of these appear only in production because test setups use a single provider. Keep a test tenant for every protocol you support.

    A worked example: three hospital groups and their patients

    A worked example: a scheduling platform serves three hospital groups and their patients. Group one uses OIDC, group two uses SAML 2.0, and group three has no central directory, so its staff use platform-managed accounts with enforced MFA.

    The staff login asks for a work email, looks up the domain and redirects to the right provider. After login, the mapping step links issuer and subject to an internal staff record and loads roles for that group only.

    Patients sign in on a separate subdomain through the patient identity store, and their tokens carry a patient audience. When group two announced a certificate rotation, the registry alert had already flagged it, and the new metadata was loaded before the old certificate expired.

    Who owns each hospital's login setup?

    Ownership is split between each hospital, your platform team and your compliance lead. Write it into every hospital's onboarding checklist so the right people are called when a login breaks.

    • Hospital IT: its identity provider, MFA policy, joiners and leavers, and notice of certificate rotation.
    • Platform team: the tenant registry, protocol configuration, the mapping step and token validation.
    • Product and compliance: which roles exist, which actions need step-up and how access is reviewed.
    • Support: a documented way to disable one tenant's login quickly during an incident.

    How do you test multi provider login?

    Test with a real provider of each protocol in a test environment, plus automated tests that forge the failures you fear. Mock JWTs and test users cover most authorization rules without calling a live provider.

    • Cross-tenant tokens: a valid token from tenant A gets a 401 on every tenant B route.
    • Unknown issuer: a token from an unregistered issuer is rejected before it reaches a controller.
    • Missing MFA: a staff login without the required method is refused where the contract requires it.
    • Audience mix: a patient token sent to a staff API fails, and the reverse.
    • Certificate expiry: an alert fires well before any registered SAML certificate expires.

    Run these on every build. A configuration change for one hospital should never be able to open another hospital's data.

    What are Spring Security multi provider authentication best practices?

    Register each provider as data with a named owner, key accounts on issuer plus subject, and load permissions from your own database. Keep patient and staff identities, sessions and token audiences apart.

    Confirm MFA where the provider performs it, reject unknown issuers early, watch certificate expiry dates, and test cross-tenant and wrong-audience tokens on every build.

    How can Agnotic help build multi-tenant Spring Boot login?

    Agnotic builds Spring Boot backends for healthcare products that serve several organizations: tenant registries, OIDC and SAML login, patient identity, MFA checks and tests that prove one tenant cannot reach another. We also work with your customers' IT teams during onboarding.

    Plan sign-in for your healthcare platform

    We design federation, patient login and access rules for products used by hospitals and patients.

    Sources

    Frequently Asked Questions

    Yes. Spring Security supports OAuth 2.0 and OpenID Connect login alongside SAML 2.0 login, and both can end in the same internal session or token.

    Talk to an engineer about your project

    Tell us which identity providers your customers use. We will outline tenant routing, identity mapping and MFA checks for your backend.

    Go deeper

    Services and solutions on this topic