AnsweredAssumed Answered

Authentication problems when using ApplicationContext

Question asked by mnybon on Dec 11, 2018
Latest reply on Dec 13, 2018 by afaust

Hello everyone.

I am trying to create a model2java code generator for the Alfresco model.xml files. Everything was going a-ok until i ran into the problem of actually getting hold of Alfresco types outside the normal bean bootstrapping.

Basically I need to get hold of both the NodeService(at least only the NodeService for now) and whatever security context is currently at play.

 

I am trying to get hold of the NodeService around the normal bean.xml heirachy of Alfresco by using

 

...

import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;

...

 

ApplicationContext context = ApplicationContextHelper.getApplicationContext();

NodeService ns = context..getBean(ServiceRegistry.class).getNodeService();

List<StoreRef> refs = ns.getStores();

 

But in my unit test which extends BaseWebScriptTest I run into the follwing exception:

net.sf.acegisecurity.AuthenticationCredentialsNotFoundException: A valid SecureContext was not provided in the RequestContext
    at net.sf.acegisecurity.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:481)
    at net.sf.acegisecurity.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:359)
    at net.sf.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:77)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:46)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:159)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.alfresco.repo.transaction.RetryingTransactionInterceptor$1.execute(RetryingTransactionInterceptor.java:79)
    at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:457)
    at org.alfresco.repo.transaction.RetryingTransactionInterceptor.invoke(RetryingTransactionInterceptor.java:69)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy19.getStores(Unknown Source)

 

After that i found a suggestiong about using AuthenticationUtil as such:

List<StoreRef> refs = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork< List<StoreRef>>()
         {
            public  List<StoreRef> doWork() throws Exception
            {
              

                  ApplicationContext context = ApplicationContextHelper.getApplicationContext();

                  NodeService ns = context..getBean(ServiceRegistry.class).getNodeService();

                  return ns.getStores();


               
            }
         }, AuthenticationUtil.getSystemUserName());

 

But I get the exact same exception.

 

I would very much appreciate some help firstly in understanding if I am doing something wrong and I have to somehow pass the SecurityContext along manually, and how to get execute code as a system-user or some other priviledged user during unit-testing.

Outcomes