Search code examples
javaamqpjasperserver

Create jasper user in an organizaition


I am trying to achieve a goal where my web application and jasper are working like microservices. I use rabbitmq to pass user details from my web app to jasper. I have successfully developed a consumer for jasper and now i am facing a trouble in creating user in an organisation. i followed the jasper's API and wrote my code.(Users and Roles API)

I am getting this error,

java.lang.IllegalArgumentException: This implementation does not support tenants
    at com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.getPersistentTenant(UserAuthorityServiceImpl.java:2029)
    at com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.getRepoUser(UserAuthorityServiceImpl.java:175)
    at com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.getRepoUser(UserAuthorityServiceImpl.java:232)
    at com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.doPutUser(UserAuthorityServiceImpl.java:327)
    at com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.putUser(UserAuthorityServiceImpl.java:316)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)

I simply have below classes.

UserConsumer.java

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UsersConsumer implements MessageListener {

    @Autowired
    JasperUserManager jasperUserManager;

    @Override
    public void onMessage(Message message) {
        try {
            System.out.println("==========MESSAGE========>>>>>>>>>> : "+message.toString());
            jasperUserManager.saveUser();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

JasperUserManager.java

import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
import com.jaspersoft.jasperserver.api.metadata.user.domain.User;
import com.jaspersoft.jasperserver.api.metadata.user.service.ObjectPermissionService;
import com.jaspersoft.jasperserver.api.metadata.user.service.UserAuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class JasperUserManagerImpl implements JasperUserManager {

    @Autowired
    private UserAuthorityService userAuthorityService;

    @Override
    public void saveUser() {
        User user = userAuthorityService.newUser(null);
        user.setUsername("thusira");
        user.setTenantId("ACT");
        user.setPassword("123");
        user.setFullName("Thusira Dissanayake");
        user.setEnabled(true);
        user.setExternallyDefined(false);
        userAuthorityService.putUser(null, user);
        System.out.println("================SSSSSSSSSUUCESSS================================");
    }

}

Please help me what would be the issue in my implementation.

Thank you in advance

BR, Thusira

After using MTUserAuthorityService got this exception,

org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:339)
    at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:198)
    at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:60)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy70.putUser(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy70.putUser(Unknown Source)
    at com.cinglevue.veip.report.users.JasperUserManagerImpl.saveUser(JasperUserManagerImpl.java:33)
    at com.cinglevue.veip.report.users.UsersConsumer.onMessage(UsersConsumer.java:20)

Solution

  • The problem is that UserAuthorityService has two implementations. One of these is a simple UserAuthorityServiceImpl which doesn't know anything about tenants unlike the MTUserAuthorityServiceImpl which can save users with tenants.

    • So firstly you should check your XML configuration. js.spring.properties should contains this line: bean.userAuthorityService=mtUserAuthorityService

    • As the second approach, you can inject directly the MTUserAuthorityServiceImpl bean. For example in this way:

    @Component
    public class JasperUserManagerImpl implements JasperUserManager {
    
        @Resource(name="")
        private UserAuthorityService userAuthorityService;
    
        @Override
        public void saveUser() {
            User user = userAuthorityService.newUser(null);
            user.setUsername("thusira");
            user.setTenantId("ACT");
            user.setPassword("123");
            user.setFullName("Thusira Dissanayake");
            user.setEnabled(true);
            user.setExternallyDefined(false);
            userAuthorityService.putUser(null, user);  
        }
    }
    
    • And in the last case, you probably don't have Jasperserver MultipleTenancy license (for example if you work with community edition) and as result, you can't work with tenants at all.