May 9, 2020
Estimated Post Reading Time ~

Creating system users session in AEM – Getting system user session in AEM

Create system users from console http://localhost:4502/crx/explorer/index.jsp

User 1: shasservice – user with no access permissions
User 2: serviceuser – user with all the access permissions

Configure this in APCHE SLING SERVICE USER MAPPER SERVICE @System/console

Service mappings: com.demo.sehns.sehns:shasservice=serviceuser

Method #1 – TRYING FOR Session

private Session getSession(String srvServiceName) throws Exception{
Session session =null;

try{
log.info(“—-Getting session for—-“+srvServiceName);
ResourceResolver resourceResolver =  resolverFactory.getServiceResourceResolver(CommonUtils.getServiceMap(strServiceName));
session = resourceResolver.adaptTo(Session.class);
}catch(Exception e){
e.printStackTrace();
log.error(“—-Exception while getting session—-“+e.getMessage());
throw e;
}
return session;
}

METHOD #2 – returning service map data

public static Map getServiceMap(final String getServiceName) throws Exception{

try {
Map params = new HashMap();
param.put(ResourceResolverFactory.SUBSERVICE, getServiceName);
return params;
}catch(Exception e){
log.error(“————Error —-“+e);
throw exception;
}
}

Here we can get the session object by following the above method.


By aem4beginner

No comments:

Post a Comment

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