ResourceResolverFactory getServiceResourceResolver throws Exception in AEM 6.1
Solution 1:
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.
Solution 2:
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.
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
)
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
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");
}