Service user, resource resolver and session

 Hello Everyone,

This is about service user/ system user, resource resolver and session. 

In AEM if we want to read or write anything from backend we have two APIs Sling/resource API and jcr/node API. if we are using sling/resource api we need to have resources resolver and for jcr/node api we need to have jcr session. To get jcr session we use SlingRespositoy.loginService() and to get resource we use ResourceResolverFactory.getServiceResourceResolver() method.

Previously with aem if we need resource resolver or session we were using sling methods SlingRepository.loginAdministrative() and ResourceResolverFactory.getAdministrativeResourceResolver() but using this method we are getting the admin session sometimes we might not need to have admin session still we are using admin session. Now these methods are deprecated and replaced with SlingRepository.loginService() and ResourceResolverFactory.getServiceResourceResolver() lets see how to use these method to get session and resource resolver.

Now to get resource or session you must have a service user/system user with permission as per application need. Follow below step to create service user/system user.

go to --> http://localhost:4502/crx/explorer/index.jsp --> login -->user administration --> create system user --> give any userID --> save


After creation of system user give required permission to system user, for that go to http://localhost:4502/useradmin console search your system user in permissions tab give required permission using checkboxes --> click save


We can have multiple system user like if we need to perform read operation then we can have a user with read permission and for write operation we can user with write permission.
Once the system user is ready with required permission bind it with bundle using mapper configuration.
for that go to http://localhost:4502/system/console/configMgr --> search for user mapper service --> from result select Apache sling Service User Mapper Service Amendment --> click Plus sign to add new factory configuration -->Enter mapping details in the form BundleId:subserviceName=userName
BundleID: symbolic name of your bundle , for me sampleproject.core
subserviceName: this name will be used in your backend code so user name will not get exposed in your code for me it is subserviceName
userName: service user /system user you have created for me it is sampleUser
bundleId and subServiceName identify the service and userName defines the name of the user to provide to the service;
PFB snapshot for more details


Now you can use this service user/system user in backend to get resource resolver or session.

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "subServiceName");
ResourceResolver resolver = resourceResolverFactory.getServiceResourceResolver(param);

Using this resolver we will get resource using getResource() method and using using adaptTo() method we can have session as well.
Resource resource = resolver.getResource("resource path");
Session session=resolver.adaptTo(Session.class);

KEEP EDUCATING YOURSELF!!!!

Comments

Popular posts from this blog

AEM in docker

Sling API vs JCR API

Creating or finding .M2 folder