小编典典

配置Activiti以在Spring Boot中重用现有的用户/组数据

spring-boot

我正在使用Spring Boot 1.4.3.RELEASE。我已经创建了一个简单的MVC应用程序来管理用户和组。现在,我想的Activiti
5.21.0我的项目中集成和 REUSE 由Activiti的现有用户/组表。

我已经从Activiti 扩展了默认值 UserEntityManagerGroupEntityManager 类,如下所示:

CustomUserEntityManager.java

import java.util.List;

import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.UserQueryImpl;
import org.activiti.engine.impl.persistence.entity.IdentityInfoEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.impl.persistence.entity.UserEntityManager;

import com.example.spring.dao.UserDao;
import com.example.spring.model.User;

public class CustomUserEntityManager extends UserEntityManager {

    private UserDao userDao;

    public CustomUserEntityManager(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public UserEntity findUserById(String userId) {
        User user = userDao.findById(Integer.parseInt(userId));
        return userToActivitiUser(user);
    }

    @Override
    public org.activiti.engine.identity.User createNewUser(String userId) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void insertUser(org.activiti.engine.identity.User user) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void updateUser(org.activiti.engine.identity.User updatedUser) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void deleteUser(String userId) {
        throw new UnsupportedOperationException();
    }

    @Override
    public List<org.activiti.engine.identity.User> findUserByQueryCriteria(UserQueryImpl query, Page page) {
        throw new UnsupportedOperationException();
    }

    @Override
    public long findUserCountByQueryCriteria(UserQueryImpl query) {
        throw new UnsupportedOperationException();
    }

    @Override
    public IdentityInfoEntity findUserInfoByUserIdAndKey(String userId, String key) {
        throw new UnsupportedOperationException();
    }

    @Override
    public List<String> findUserInfoKeysByUserIdAndType(String userId, String type) {
        throw new UnsupportedOperationException();
    }

    private UserEntity userToActivitiUser(User user) {
        UserEntity activitiUser = new UserEntity();
        activitiUser.setId(user.getId().toString());
        activitiUser.setPassword(user.getPassword());
        activitiUser.setFirstName(user.getUsername());
        activitiUser.setLastName(user.getLastName());
        activitiUser.setEmail(user.getEmail());
        return activitiUser;
    }

}

ActivitiGroupEntityManager.java

import java.util.List;

import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.GroupQuery;
import org.activiti.engine.impl.GroupQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.persistence.entity.GroupEntityManager;

import com.example.spring.dao.CMSGroupDao;

public class CustomGroupEntityManager extends GroupEntityManager {

    private CMSGroupDao groupDao;

    public CustomGroupEntityManager(CMSGroupDao groupDao) {
        this.groupDao = groupDao;
    }

    @Override
    public List<Group> findGroupsByUser(String userId) {
        // TODO add logic
        return null;
    }

    @Override
    public Group createNewGroup(String groupId) {
            throw new UnsupportedOperationException();
    }

    @Override
    public void deleteGroup(String groupId) {
            throw new UnsupportedOperationException();
    }

    @Override
    public GroupQuery createNewGroupQuery() {
            return super.createNewGroupQuery();
    }

    @Override
    public List<Group> findGroupByQueryCriteria(GroupQueryImpl query, Page page) {
            throw new UnsupportedOperationException();
    }

    @Override
    public long findGroupCountByQueryCriteria(GroupQueryImpl query) {
            throw new UnsupportedOperationException();
    }

}

然后,我添加了如下的自定义工厂:

CustomUserManagerFactory.java

import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.interceptor.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.example.spring.dao.UserDao;

@Component(value = "customUserManagerFactory")
public class CustomUserManagerFactory implements SessionFactory {

    @Autowired
    private UserDao userDao;

    @Override
    public Class<?> getSessionType() {
        return CustomUserEntityManager.class;
    }

    @Override
    public Session openSession() {
        return new CustomUserEntityManager(userDao);
    }

}

CustomGroupManagerFactory.java

import org.activiti.engine.impl.interceptor.Session;
import org.activiti.engine.impl.interceptor.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.example.spring.dao.CMSGroupDao;

@Component(value = "customGroupManagerFactory")
public class CustomGroupManagerFactory implements SessionFactory {

    @Autowired
    private CMSGroupDao groupDao;

    @Override
    public Class<?> getSessionType() {
        return CustomGroupEntityManager.class;
    }

    @Override
    public Session openSession() {
        return new CustomGroupEntityManager(groupDao);
    }

}

最后,我添加了ProcessEngine配置bean,如下所示:

ProcessEngineConfig.java

package com.example.spring.configuration;

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

import javax.annotation.Resource;

import org.activiti.engine.impl.interceptor.SessionFactory;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Configuration;

@Configuration
@AutoConfigureBefore(org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class)
public class ProcessEngineConfig implements ProcessEngineConfigurationConfigurer{

    @Resource(name = "customUserManagerFactory")
    private SessionFactory userManagerFactory;

    @Resource(name = "customGroupManagerFactory")
    private SessionFactory groupManagerFactory;

    public void configure(SpringProcessEngineConfiguration pec) {
        pec.setDbIdentityUsed(false); 
        List<SessionFactory> customSessionFactories = new ArrayList<SessionFactory>();
        customSessionFactories.add(userManagerFactory);
        customSessionFactories.add(groupManagerFactory);
        if (pec.getCustomSessionFactories() == null){
            pec.setCustomSessionFactories(customSessionFactories);
        }
        else{
            pec.getCustomSessionFactories().addAll(customSessionFactories);
        }
    }
}

pom.xml的* 重要部分 *

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <activiti.version>5.21.0</activiti.version>
    </properties>

    <dependencies>

        <!-- Activiti Dependencies -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-actuator</artifactId>
            <version>${activiti.version}</version>
        </dependency>

        <!-- Spring-boot Dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        .......
        .......
     </dependencies>

应用程序在执行时没有任何运行时错误 HOWEVER
,当我使用org.activiti.engine.IdentityService.newUser()该用户创建用户时,该用户将创建并保存在Activit用户表中,UnsupportedOperationException()并且不会引发该错误,这意味着自定义配置无法正常工作。

任何帮助将不胜感激!!


阅读 338

收藏
2020-05-30

共1个答案

小编典典

对于CustomGroupManagerFactory,类型应为:

  @Override
  public Class<?> getSessionType() {
      return GroupIdentityManager.class;
  }

对于CustomUserManagerFactory,类型应为:

  @Override
  public Class<?> getSessionType() {
      return UserIdentityManager.class;
  }

我希望上面的解决方案可以帮助您使其正常运行。

2020-05-30