ResourceResolverFactory getServiceResourceResolver 在 AEM 6.1 中抛出异常

ResourceResolverFactory getServiceResourceResolver throws Exception in AEM 6.1

我想向 AEM 写入一些数据,下面的代码在 AEM 6.0 中对我来说工作正常,但在 AEM 6.1 中却不行,总是抛出如下登录异常:

"获取服务的 CRX 用户时出现登录异常:'writeService'。org.apache.sling.api.resource.LoginException:无法为捆绑包 group.tti.commons-服务 [395] 派生用户名和子服务 writeService"

OSGI 配置:

我的代码 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) {
...
}

我在 AEM 6.1 上遗漏了什么吗?

在 AEM 6.1 中,服务用户必须是系统用户,这实际上意味着他们在 JCR 中的节点类型为 rep:SystemUser。这些用户不能用于正常登录,只能通过后台进程。管理员用户不是系统用户,因此您不能在这样的服务用户映射中使用管理员用户。您必须创建一个新的系统用户并为其分配适当的权限。

如果您想了解有关此更改的更多背景信息,请查看 https://issues.apache.org/jira/browse/SLING-3854

代替制作会话:

adminSession = resourceResolverWriter.adaptTo(Session.class);`

像下面这样创建Session,希望不会出现登录异常

final Session session;
session= resourceResolver.adaptTo(Session.class);

发生这种情况是因为 resourceResolverWriter 不是隐式对象。

在 Justin 的建议下,我尝试并找到了解决方案。如此发帖对他人有益。

目标:当用户登录时将 data/nodes 写入内容(特别是 /etc/userdata)。

我们可以通过两种方式实现这一点(无论哪种方式,用户都需要是 'system user')

进程 1:

第 1 步:在 OSGI 配置中使用内置系统用户。在 OSGI select Apache Sling 服务用户映射器服务

group.abc.commons-service:writeService=oauthservice(其中oauthservice是系统用户)

第 2 步:为该系统用户分配访问内容文件夹的权限。

您在 CRX 中看到的系统用户位于:/home/users/system

进程 2:

第一步:新建系统用户。去做这个 打开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 :)`

我创建了abcwriteservice用户

第 2 步:转到权限,并为用户 abcwriteservice 授予访问您要写入的文件夹的权限。 (在这个例子中:/etc/userdata

第 3 步:打开 OSGI 控制台并转到 Apache Sling Service User Mapper Service 以定义服务-用户映射。

示例:group.commons-service:writeService=abcwriteservice

第 4 步:在代码中,我添加了额外的参数,如:

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");
}

此外,如果您计划将来迁移到 AEM 6.2,请考虑使用 ACS Commons 来促进系统用户的创建和可用性。它消除了所有这些容易出错的手动过程。

https://adobe-consulting-services.github.io/acs-aem-commons/features/ensure-service-users/index.html