Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResourceResolverFactory getServiceResourceResolver throws Exception in AEM 6.1

Tags:

aem

sling

I want to write some data to AEM, and the below code works fine for me in AEM 6.0 but not in AEM 6.1 , always throws a Login Exception as follows:

"Login Exception when obtaining a CRX User for the Service:'writeService'.org.apache.sling.api.resource.LoginException: Cannot derive user name for bundle group.tti.commons-service [395] and sub service writeService"

OSGI Config:

enter image description here

Code in my class:

import javax.jcr.Session;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
....
@Reference
private ResourceResolverFactory factory;
private ResourceResolver resourceResolverWriter;
private static Session adminSession;
...
...
Map<String, Object> param = new HashMap<String, Object>();        
    param.put(ResourceResolverFactory.SUBSERVICE, "writeService");
try {
  resourceResolverWriter = factory.getServiceResourceResolver(param);            
  adminSession = resourceResolverWriter.adaptTo(Session.class);
...
} catch (LoginException e) {
...
}

Am i missing anything on AEM 6.1?

like image 333
Suren Konathala Avatar asked Jul 10 '15 21:07

Suren Konathala


2 Answers

In AEM 6.1, service users must be system users, which effectively means that their node in the JCR is of type rep:SystemUser. These users cannot be used to log in normally, only by background processes. The admin user is not a system user, so you cannot use the admin user in a service user mapping like this. You have to create a new system user and assign them the appropriate permissions.

If you would like to read more of the background on this change, take a look at https://issues.apache.org/jira/browse/SLING-3854.

like image 172
Justin Edelson Avatar answered Nov 19 '22 12:11

Justin Edelson


With Justin's advice, i tried and found the solution. Posting so can be beneficial for others.

Goal: To write data/nodes to content (specifically to /etc/userdata) when a user logs in.

We can achieve this in 2 ways (either way, the user needs to be a 'system user')

Process 1:

Step 1: Use in-built system user in OSGI configuration. In OSGI select Apache Sling Service User Mapper Service

group.abc.commons-service:writeService=oauthservice (where oauthservice is a system user)

Step 2: Assign that system user the permissions to access the content folder.

enter image description here

You see the system users in CRX at: /home/users/system

Process 2:

Step 1: Create a new system user. to do this Open http://localhost:4502/crx/explorer/index.jsp

1. Login as admin 
2. Open 'User Administration
3. Select 'Create System User'
4. Enter "user id"
5. Hit the Green button (you will not se a save button :)`

I have created abcwriteservice user

Step 2: Go to Permissions, and for the user abcwriteservice give Permissions to access the folder where you'd like to write. (In this example: /etc/userdata ) enter image description here

Step 3: Open OSGI console and go to Apache Sling Service User Mapper Service to define the service-user mapping.

Example: group.commons-service:writeService=abcwriteservice

enter image description here

Step 4: In code, i added extra parameter, as:

Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "writeService");

try {
    resourceResolverWriter = factory.getServiceResourceResolver(param);

    if (resourceResolverWriter == null)
        throw new Exception("Could not obtain a CRX User for the Service:'writeService'");

    Node usersRootNode = adminSession.getNode("/etc/userdata/users");
}
like image 21
Suren Konathala Avatar answered Nov 19 '22 12:11

Suren Konathala