3

I have to get administrative prevailed org.apache.sling.api.resource.ResourceResolver.

But the API, ResourceResolverFactory.getAdministrativeResourceResolver(Map<String,Object> authenticationInfo) is depricated.

Dileepa
  • 1,019
  • 1
  • 15
  • 40

3 Answers3

6

Solution by @Dileepa would work, but definitely not a good practice to hardcode passwords and also, its always good to prevent over-use of administrative resourceresolvers/sessions.

Take a look at the implementation mentioned here in sling docs.

SubSul
  • 2,523
  • 1
  • 17
  • 27
  • According to your suggestion, changed the code a little. Can you please validate it? – Dileepa Mar 09 '16 at 07:10
  • :) yep, this would work fine. I had implemented a similar code in one of my projects. – SubSul Mar 10 '16 at 09:35
  • 1
    yes, this would be the recommended approach using ServiceUserMapper, here is a detailed working implementation of that: https://helpx.adobe.com/experience-manager/using/querying-experience-manager-sling.html – Ahmed Musallam Jul 20 '16 at 04:06
3

We have added the following configuration:

\apps\qwerty\configs\config\org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.xml


<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="sling:OsgiConfig"
    user.default="admin"
    user.mapping="[com.adobe.granite.oauth.server=oauthservice,com.adobe.granite.oauth.server:authentication-handler=admin,com.day.cq.cq-search-suggest=suggestionservice,com.adobe.cq.social.cq-social-scoring:utility-reader=admin,com.adobe.cq.social.cq-social-commons-oauth:user-reader=admin,com.adobe.cq.social.cq-social-calendar:ugc-writer=admin,com.adobe.cq.social.cq-social-forum:ugc-reader=admin,com.adobe.cq.social.cq-social-group:user-admin=admin,org.apache.sling.scripting.core:workflow-launcher=admin,com.adobe.cq.social.cq-social-storage:ugc-writer=admin,com.adobe.cq.social.cq-social-messaging:utility-reader=admin,com.adobe.cq.social.cq-social-commons-oauth:ugc-writer=admin,com.adobe.cq.social.cq-social-messaging:ugc-writer=admin,com.adobe.cq.social.cq-social-journal:ugc-writer=admin,com.adobe.cq.social.cq-social-forum:workflow-launcher=admin,com.adobe.cq.social.cq-social-commons:utility-reader=admin,com.adobe.cq.social.cq-social-commons:user-reader=admin,com.adobe.cq.social.cq-social-console:communities-user-admin=admin,com.adobe.cq.social.cq-social-ugcbase:utility-reader=admin,com.adobe.cq.social.cq-social-as-provider:ugc-writer=admin,com.adobe.cq.social.cq-social-blog:ugc-writer=admin,com.adobe.cq.social.cq-social-forum:ugc-writer=admin,com.adobe.cq.social.cq-social-ugcbase:ugc-writer=admin,com.adobe.cq.social.cq-social-commons:ugc-writer=admin,com.adobe.cq.social.cq-social-commons:workflow-launcher=admin,com.adobe.cq.social.cq-social-commons-oauth:user-admin=admin,com.adobe.cq.social.cq-social-messaging:user-reader=admin,com.adobe.cq.social.cq-social-handlebars:utility-reader=admin,com.adobe.cq.social.cq-social-tally:ugc-writer=admin]"/>

Or you may configure it using /sysem/console/configMgr for Apache Sling Service User Mapper Service:

Apache Sling Service User Mapper Service

And now to get ResourceResolver service we need to run the following method:

resolverFactory.getServiceResourceResolver(null);
dzenisiy
  • 855
  • 10
  • 32
1

Create a user in AEM with right privileges.
Use the following code, with username and password of user which are externalized. The password is protected by AEM's default crypto support.

@Component(immediate = true, metatype = true, label = "Configuration Details")
@Service(value = { MyResourceResolver.class })
@Properties({
    @Property(label = "Username", name = "username", description = "Username"),
    @Property(label = "Password", name = "password", description = "Password for the above user (Use crypto support)"),
})
public class MyResourceResolver {       
    @Reference
    private ResourceResolverFactory resourceFact;

    @Reference
    private CryptoSupport cryptoSupport;

    private String username;
    private String password;

    public ResourceResolver getUserResourceResolver() throws LoginException{
        Map<String,Object> authenticationInfo = new HashMap<>(2);
        authenticationInfo.put(ResourceResolverFactory.USER, username);
        String unprotectedPass;
        try {
            unprotectedPass = cryptoSupport.unprotect(password);
        } catch (CryptoException e) {
            unprotectedPass = password;
            log.error(e.getMessage());
        }
        authenticationInfo.put(ResourceResolverFactory.PASSWORD, unprotectedPass.toCharArray());
        return resourceFact.getResourceResolver(authenticationInfo);
    }

    @Activate
    protected void activate(Map<String, Object> mapCreated) {
        log.info("Configuration Map");
        username = mapCreated.get("username").toString();
        password = mapCreated.get("password").toString();
    }

    @Modified
    protected void modified(Map<String, Object> mapModified) {
        log.info("Configuration Modified");
        username = mapModified.get("username").toString();
        password = mapModified.get("password").toString();
    }
}
Dileepa
  • 1,019
  • 1
  • 15
  • 40