@partyush , Please confirm my understanding.
Thanks,
RK.
Hey,@nsvsrk great follow-up question and it's smart to break this down into the high-level flow before getting into the code. Here's how it works, step by step.
FYI: Username and password are never passed to AEM in a SAML flow — they go to the IDP.
SAML Setup via OSGi Configs
First, we configure the SAML Authentication Handler using AEM’s OSGi console. This includes:
-
IDP URL (e.g., Okta, Azure AD)
-
AEM SP entity ID
-
Assertion Consumer Service (ACS) path (e.g., /saml_login)
-
Attribute mapping (like email, uid, etc.)
Once this is set, AEM will automatically redirect unauthenticated users to the configured IDP.
User Logs In
When an end user visits the portal (built on AEM), they're redirected to the IDP login page. The user enters their username and password on the IDP side not in AEM.
After a successful login, the IDP posts a signed SAML assertion to AEM, which then validates it and creates an authenticated session for the user.
So to clarify your point , AEM never receives the username or password directly. It only gets the identity via the assertion from the IDP.
ResourceResolver Usage Post Authentication
Once authentication is complete, AEM maintains the user session internally via session cookies. On every subsequent request, AEM ties the session to the appropriate user.
From that point on, any AEM component (Sling Model, Servlet, Filter, etc.) can access the authenticated user's session via:
request.getResourceResolver();
This is already tied to the user and doesn’t need to be explicitly created unless you’re in a background thread or doing system-level operations. So no, each component does not need to create a ResourceResolver from scratch. If you’re in the context of a request, use the one AEM already provides.
so overall you don’t need to share or reuse resolvers across components, use the one tied to the current request.
When and Where Should You Close the ResourceResolver?
This depends on how you obtained it:
-
If you created it manually using a service user:
try (ResourceResolver resolver = resolverFactory.getServiceResourceResolver(params)) {
// use resolver
}
-
Then yes, you should close it — and ideally within a try-with-resources block.
-
If you got it from the request, like:
request.getResourceResolver();
Then no, do not close it. AEM handles it internally as part of the request lifecycle.
- So the general rule is: only close resolvers that you explicitly open coz, Only manually created resolvers should be closed, and they should be scoped tightly to avoid leaks.
hope that helps
Thanks
Partyush.