AnsweredAssumed Answered

Error when authenticating in Alfresco programatically

Question asked by guesttlmq.4 on Apr 23, 2009
Hello,

I have a bean that is intanciated when Alfresco starts in Tomcat but I am unable to authenticate to create a new node.
I get the exception: A valid SecureContext was not provided in the RequestContext when I call serviceRegistry.getAuthenticationService();

The serviceRegistry and nodeService are injected via Spring like this:

<bean id="foldersBuilder" class="com.companyname.alfresco.extension.FoldersBuilder" init-method="init">
   <property name="nodeService" ref="NodeService" />
   <property name="serviceRegistry" ref="ServiceRegistry" />
</bean>

Here is the Java code that is called when the bean is intantiated:

System.out.println("Start");
            
            AuthenticationService authenticationService = serviceRegistry
                  .getAuthenticationService();
            
            System.out.println("Current User 1:"
                  + authenticationService.getCurrentUserName());
            
            authenticationService.authenticate("admin", "admin"
                  .toCharArray());
            System.out.println("Current User 2:"
                  + authenticationService.getCurrentUserName());
            SearchService searchService = serviceRegistry
                  .getSearchService();
            System.out.println(searchService.toString());

            StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE,
                  "SpacesStore");
            System.out.println(storeRef.toString());

            ResultSet resultSet = searchService.query(storeRef,
                  SearchService.LANGUAGE_LUCENE, "PATH:\"/app:my_home\"");
            System.out.println(resultSet.toString());

            NodeRef companyHome = resultSet.getNodeRef(0);
            System.out.println("CompanyHome: " + companyHome);

            String name = "Foundation API sample ("
                  + System.currentTimeMillis() + ")";

            Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>();

            contentProps.put(ContentModel.PROP_NAME, name);

            ChildAssociationRef association = nodeService.createNode(
                  companyHome, ContentModel.ASSOC_CONTAINS, QName
                        .createQName(
                              NamespaceService.CONTENT_MODEL_PREFIX,
                              "example"), ContentModel.TYPE_CONTENT,
                  contentProps);
            System.out.println(association.toString());

Could you help out to get my authentication in Alfresco working?

Thank you,
Ana

Outcomes