April 8, 2020
Estimated Post Reading Time ~

Access to ResourceResolver in OSGi Services: AEM 6.1



We all know that from AEM 6.0, usage of Admin Session to access the ResourceResolver is deprecated which means we cannot use session = repository.loginAdministrative(null); anymore !

Instead, AEM comes with the concept of Service based authentication to get access to ResourceResolver.

Let us see how to create the Service Users and Use the same to get access to ResourceResolver in the OSGi service

Step1: Creating Service User Mapping
Go tohttp://<host>:<port>/system/console/configMgr
Search for ‘Apache Sling Service User Mapper Service’ and click on edit
Add an entry by clicking ‘+’
<bundleId>:<subServiceName> = <systemUserName>

Ex: org.test.core:readService=testreaduser
User Mapping Service

Step 2: Create a User Mapper Service Amendment
Add a new Amendment as shown below
User Mapping Service Amendment

If you are using multiple User Mapping for the same service, then the highest Ranking User will be used to authenticate the access for the ResourceResolver.

Step 3: Create the System User
In AEM 6.0, even the normal user could be used in mapping the service but from AEM 6.1 it is mandatory to use only the ‘System User’ in the Mapping.

Goto http://<host>:<port>/crx/explorer/index.jsp

Click on ‘User Administration’
CRX Explorer

Click on ‘Create System User’
User Administration

Add a userId ‘testreaduser’ and click the tick mark to create System User

Step 4: Permissions to the System User

Once you have created the system User, goto /useradmin
Select the user you created and click on the ‘Permission’ tab
Enable the ACLs accordingly and ‘Save’.
User Permission

Step 5: OSGi Service
Now you have successfully created the service users which can be used in your services to get access to ResourceResolver. Below is an example that shows how to use the service user to get https://www.acheterviagrafr24.com/viagra-online/ the access.

Create an Interface
package org.test.test.core.service;
public interface ReadService {
public void listTitles();
}

Create an Impl class
package org.test.test.core.service.impl;
import java.util.HashMap;
import java.util.Map;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.test.test.core.service.ReadService;
import com.day.cq.wcm.api.Page;
@Component(immediate = true) @Service public class ReadServiceImpl implements ReadService {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Reference private ResourceResolverFactory resourceFactory;
@Override @Activate public void listTitles() {
Map < String, Object > paramMap = new HashMap < String, Object > (); //Mention the subServiceName you had used in the User Mapping paramMap.put(ResourceResolverFactory.SUBSERVICE, "readService"); log.info("After the param"); ResourceResolver rr = null; try{ rr = resourceFactory.getServiceResourceResolver(paramMap); log.info("UserId : " + rr.getUserID()); Resource res = rr.getResource("/content/geometrixx"); log.info("Resource : " + res.getPath()); Page page = res.adaptTo(Page.class); log.info("page path : " + page.getPath()); log.info("page title : " + page.getTitle()); }catch(Exception e){ log.error(e.getMessage());
}
}
}

Once you install the bundle, you should be able to see the mentioned logs in your <project>.log file

This is slightly different from AEM 6.0 were in just having the UserMapping and the User would be sufficient to get access in ResourceResolver. In 6.1 it's changed a bit with Amendments and the compulsory of creating System User only to work.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.