AEM 6.3 ResourceResolverFactory为null并抛出NullPointerException

问题描述 投票:0回答:2

也试过这个帖子AEM 6.3 - ResourceResolverFactory is null in Service and throwing LoginException in Sling Model class

接口

package org.employee.employee.core.admin;

import org.apache.sling.api.resource.ResourceResolver;

public interface CustomerService {
    public String getCustomerData();
}

服务类==>获取resolverFactory null

package org.employee.employee.core.admin; 

import java.util.HashMap;
import java.util.Map;    
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype = true, immediate = true)
@Service
public class CustomerServiceImpl implements CustomerService {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Reference
    private ResourceResolverFactory resolverFactory;

    public String getCustomerData() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put(ResourceResolverFactory.SUBSERVICE, "CustomerService");
        ResourceResolver resolver = null;
        try {
            resolver = resolverFactory.getServiceResourceResolver(param);
            System.out.println(resolver);
        } catch (LoginException loginExcp) {
            log.error("Exception while getting resource resolver." + loginExcp);
        }
        return resolver.toString();
    }
}

Service Component显示state => Active

enter image description here

UserMapper

enter image description here

aem osgi-bundle sling aem-6
2个回答
0
投票

我正在访问它

CustomerService customerService = new CustomerService();
String resolver = customerService.getCustomerData();

这是不正确的解决方案

@Reference
private CustomerService customerService;
String resolver = customerService.getCustomerData();

0
投票

另外我相信您不应该使用admin用户(如屏幕截图所示)来访问服务,因为开发了Service映射以防止对存储库的无限制访问。

而是创建一个新的系统用户,并仅为用户提供该服务所需的权限。

要创建新系统用户,您可以按照以下步骤操作:

  1. 在您的AEM实例上,打开http://server:port/crx/explorer/index.jsp
  2. 按屏幕左上角的“登录”链接,然后以管理员身份登录。
  3. 单击create system user并将用户ID指定为myServiceUser,将中间路径指定为system / MyModule。
  4. 使用useradmin控制台为用户分配权限。
  5. 向ServiceUserMapper配置添加配置修订。

请参阅此helpx以获取完整步骤。 https://helpx.adobe.com/in/experience-manager/6-3/sites/administering/using/security-service-users.html

另一个SO答案证明了这一点:ResourceResolverFactory getServiceResourceResolver throws Exception in AEM 6.1

ACS Commons的这个工具对确保服务用户存在非常有帮助。 https://adobe-consulting-services.github.io/acs-aem-commons/features/ensure-service-users/index.html

© www.soinside.com 2019 - 2024. All rights reserved.