如何以编程方式在AEM中创建用户和密码?

时间:2016-06-30 05:20:13

标签: java servlets aem sling

我需要以编程方式在AEM中创建用户和他的密码。我在AEM中创建了一个HTML页面,当我们点击该按钮时我有一个SUBMIT按钮我想运行我在Eclips中运行的servlet代码。     *用户创建程序应该在Servlet中

My HTML CODE INSIDE CRXDE Lite:-
-------------------------------

<form method="get" action="/bin/userCreationupdateservlet/get" enctype="multipart/form-data">
</br>
</br>
   <h2>User Creation</h2>

  <input type="submit" name="submit" value="Submit" class="submit">
</form>

我的Servlet代码内部Eclips

@Component(immediate = true, metatype = true, label = "userCreationupdateservlet")
    @Service(Servlet.class)
    @Properties(value = {
            @Property(name = "sling.servlet.paths", value = "/bin/userCreationupdateservlet/get"),
            @Property(name = "sling.servlet.methods", value = "GET") })
    public class userCreation extends SlingAllMethodsServlet {

                private static final long serialVersionUID = -4364646607782417077L;

                private static final Logger log = LoggerFactory.getLogger(userCreation.class);

                @Reference
                private CreateAEMUser semUser;

                @Override
                protected void doGet(SlingHttpServletRequest request,
                        SlingHttpServletResponse response) throws ServletException,
                        IOException {
                        log.info("Hi this Servlet :::::::");
                      final boolean isMultipart = org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(request);
                         PrintWriter out = null;
                         log.info("Started"); 
                         semUser.newCQUser("Little");

                }

                @Override
                protected void doPost(SlingHttpServletRequest request,
                        SlingHttpServletResponse response) throws ServletException,
                        IOException {

                }
    }

服务这里从Servlet上面调用: -

import org.w3c.dom.Document;
import org.w3c.dom.Element;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.io.StringWriter;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

import java.util.HashMap;
import java.util.Map;


import javax.jcr.Repository; 
import javax.jcr.SimpleCredentials; 
import javax.jcr.Node; 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import org.apache.jackrabbit.commons.JcrUtils;


import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

import javax.jcr.RepositoryException;

import org.apache.felix.scr.annotations.Reference;
import org.apache.jackrabbit.commons.JcrUtils;


import javax.jcr.Session;
import javax.jcr.Node; 



//Sling Imports
import org.apache.sling.api.resource.ResourceResolverFactory ;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.resource.ResourceResolver; 
import org.apache.sling.api.resource.Resource; 

//Jackrabbit User APIs
import org.apache.jackrabbit.api.JackrabbitSession ;
import org.apache.jackrabbit.api.security.user.UserManager  ;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.Group;
import  org.apache.jackrabbit.api.security.user.Authorizable ;
import javax.jcr.security.AccessControlManager ; 
import org.apache.jackrabbit.api.security.principal.PrincipalManager;
import java.security.Principal;
import javax.jcr.security.Privilege;
import javax.jcr.security.AccessControlList ; 


//This is a component so it can provide or consume services
@Component(metatype = true, immediate = true, label = "USER CREATION")
@Service(CreateAEMUser.class) 
public class CreateAEMUser {
    /** Default log. */
    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    private Session session;

    //Inject a Sling ResourceResolverFactory
    @Reference
    private ResourceResolverFactory resolverFactory;

    public String newCQUser(String name) {

     try
     {
      //Invoke the adaptTo method to create a Session 
      log.info("Began");
      ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
      session = resourceResolver.adaptTo(Session.class);

      //Create a UserManager instance from the session object
      UserManager userManager = ((JackrabbitSession) session).getUserManager();

      String path = "/home/users/azusers"; 


      log.info(path);
      JackrabbitSession js = (JackrabbitSession) session;
      // get user/principal for whom to read/set ACLs


      // Create a Group and User
      Group group = userManager.createGroup("My Group");
      User user = userManager.createUser(name, "AEM");
      // Add Users to Group
      Authorizable authUser = userManager.getAuthorizable(user.getID());
      group.addMember(authUser);
      // Provide permissions to Group
      AccessControlManager accCtrlMgr = session.getAccessControlManager();
      JackrabbitSession jcrSession = (JackrabbitSession) session;
      PrincipalManager principalMgr = jcrSession.getPrincipalManager();
      Principal groupPrincipal = principalMgr.getPrincipal("My Group");  // Here am getting Null Vaue
      Privilege[] privileges = new Privilege[] { accCtrlMgr.privilegeFromName(Privilege.JCR_ALL) };

      // Set privileges to particular path
      javax.jcr.security.AccessControlPolicyIterator accList= accCtrlMgr.getApplicablePolicies(path); 
      log.info(accList.toString());
      javax.jcr.security.AccessControlList  acl =(AccessControlList) accList.nextAccessControlPolicy();

      acl.addAccessControlEntry(groupPrincipal, privileges);
      accCtrlMgr.setPolicy(path, acl);
      session.save();
      session.logout(); 
      return "All AEM Users are written to the log file" ;  

    }
    catch(Exception e)
    {
             log.info("CQ ERROR: "+e.getMessage())  ; 
    }

    return null;
     }
    }

并且我将这个jar添加到osgi bundle但是用户没有创建程序正在打击并且debuging很好

0 个答案:

没有答案