April 26, 2020
Estimated Post Reading Time ~

Different ways to get AEM admin session

Method 1: 
AEM admin session can be obtained using below method but this is deprecated

@Reference private SlingRepository repository; adminSession = repository.loginAdministrative(null);
Method 2: 
Another way to get the session (not the recommended way)
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()),"crx.default");

Method 3: 
@Reference 
public ResourceResolverFactory rrFactory; 

ResourceResolver adminResolver = rrFactory.getAdministrativeResourceResolver(null); 
Session adminSession = adminResolver.adaptTo(Session.class);
Method 4: 
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "jqom");
ResourceResolver resolver = null;          
        try {                     
            //Invoke the getServiceResourceResolver method to create a Session instance
            resolver = resolverFactory.getServiceResourceResolver(param);
            session = resolver.adaptTo(Session.class);
           }

See Community Article

Method 5:
You can create a user and assign it to the administrative group and use its credentials rather than the default admin user.
You may also want to take a look at this article which goes over using the correct resource resolver in AEM 6.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.