AEM Service User Mapping
Service user mapping with the help of system user allows us to create a session or resource resolver object to access resource.
Before creation of service user mapping it will require a system user. Follow this link for System User creation.
SlingRepository.loginAdministrative() and ResourceResolverFactory.getAdministrativeResourceResolver() methods provided by Sling are the ways to create an administrative session or resource resolver. These methods are not recommended to use as it is having full access to create, modify and delete resource / node via code. Using these methods anyone can temper resource and can access resource without any restrictions.
Service user is a way to create a resource resolver object to access resource depending on Access Control Levels (ACL’s) with limited set of required access.
Follow below steps to consume service user via code:
- Create below highlighted serviceusermapping OSGI configuration to access resource using service user. Follow below naming convention for the same:
org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.<custom_name>.xml
2. Provide below service mapping details:
<bundleId>:<subServiceName>=<systemUser>
bundleId can be find out under core package as artifactId or Bundle-SymbolicName as shown below.
subServiceName is a custom service name.
systemUser created as practice user created above as part of AEM Service User.
Paste below content inside org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.practice.xml file created in first step:
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0"
xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="sling:OsgiConfig"
service.ranking="100"
user.mapping="[practice.core:practiceService\=practiceuser]"/>
3. Create PracticeService.java interface to create ResourceResolver instance
4. Create an OSGI service PracticeServiceImpl.java class by implementing PracticeService.java interface to create ResourceResolver instance.
5. Create SlingServletByPath.java servlet class to consume created service created in previous step.
6. Build code using below command and deploy on AEM instance using below command.
mvn clean install -PautoInstallBundle -DskipTests=true
7. Hit below URL, ctrl + f and search for practiceuser will shown below view:
http://localhost:4502/system/console/configMgr
Click on above option and verify below highlighted Service Mappings:
practice.core:practiceService=practiceuser
8. Hit below URL to call SlingServletByPath.java servlet class and give below output:
http://localhost:4502/bin/practice
Check error.log file and looks for below logs:
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.