User creation in alfresco through java

2019-09-11 06:18发布

问题:

PersonServiceImpl personService = new PersonServiceImpl();
            PropertyMap ppOne = new PropertyMap(6);
            AuthenticationService authenticationService = new AuthenticationServiceImpl();
            AuthorityService aService = new AuthorityServiceImpl();
            if(authenticationService.authenticationExists("admin123")==false){
                authenticationService.authenticate("admin123","admin123".toCharArray());
            ppOne.put(ContentModel.PROP_USER_USERNAME,"admin123");
                ppOne.put(ContentModel.PROP_FIRSTNAME,"admin123");
                ppOne.put(ContentModel.PROP_LASTNAME,"Technologies");
                ppOne.put(ContentModel.PROP_EMAIL,"ghfghfh@gmail.com");
                ppOne.put(ContentModel.PROP_PASSWORD,"admin123");
                NodeRef nodeRef=personService.createPerson(ppOne);
            }

When ever i keep code like this if person is new person then also authenticationExits true...

If i keep true inplace of false then persionService.createPerson(ppOne) will raise null pointer exception...

Any body please help me...

Jul 26, 2016 9:36:07 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [testing] in context with path [/SpringWithAlfrescoScreens] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at org.alfresco.repo.security.authentication.AuthenticationServiceImpl.clearCurrentSecurityContext(AuthenticationServiceImpl.java:188)
    at org.alfresco.repo.security.authentication.AuthenticationServiceImpl.authenticate(AuthenticationServiceImpl.java:66)
    at com.service.AlfService.saveApplication(AlfService.java:135)
    at com.controller.AlfController.saveApplication(AlfController.java:144)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

回答1:

Can you just @Autowire the PersonService, AuthenticationService instead of creating object with "new".

Following is the working code snippet :

@Autowired
PersonService personService;
@Autowired
MutableAuthenticationService authenticationService;



public void createUser(User user) throws Exception {
    if (user.getUserName() != null) {
        if (authenticationService.authenticationExists(user.getUserName())) {
            throw new Exception("User" + user.getUserName() + " already exists.");
        } else {
            authenticationService.createAuthentication(user.getUserName(), user.getPassword().toCharArray());
            Map<QName, Serializable> properties = new HashMap<>();
            properties.put(ContentModel.PROP_USERNAME, user.getUserName());
            NodeRef personNodeRef = personService.createPerson(properties);
            user.setId(personNodeRef.getId());
        } 
    }
}


回答2:

try this worked fine for me https://forums.alfresco.com/comment/146735#comment-146735 just make a copy/paste and do not forget to read all the post