问题描述
我想向AEM中写入一些数据,下面的代码对我来说在AEM 6.0中工作正常,但在AEM 6.1中却不行,总是抛出如下所示的Login Exception:
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:
获取服务的CRX用户时的登录异常:'writeService'.org.apache.sling.api.resource.LoginException:无法导出束group.tti.commons-service [395]的用户名,并且子服务writeService
OSGI配置:
我班上的代码:
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上缺少任何东西吗?
Am i missing anything on AEM 6.1?
推荐答案
在贾斯汀的建议下,我尝试并发现了解决方案。
With Justin's advice, i tried and found the solution. Posting so can be beneficial for others.
目标:在用户登录时将数据/节点写入内容(特别是/ etc / userdata)。
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')
步骤1:在OSGI配置中使用内置系统用户。在OSGI中,选择 Apache Sling服务用户映射器服务
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
(其中 oauthservice
是系统用户)
步骤2:向该系统用户分配权限来访问内容文件夹。
Step 2: Assign that system user the permissions to access the content folder.
您会在CRX中看到系统用户: / home / users / system
You see the system users in CRX at: /home/users/system
步骤1 :创建一个新的系统用户。为此
打开
Step 1: Create a new system user. to do thisOpen 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
)
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
)
第3步:打开OSGI控制台并转到 Apache Sling服务用户映射器服务来定义服务用户映射。
Step 3: Open OSGI console and go to Apache Sling Service User Mapper Service to define the service-user mapping.
示例: group.commons-service:writeService = abcwriteservice
步骤4:在代码中,我添加了额外的参数,例如:
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");
}
这篇关于ResourceResolverFactory getServiceResourceResolver在AEM 6.1中引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!