Activiti使用

二、Activiti-自定义用户表

2018-07-13  本文已影响2869人  Tomthy

在很多项目中都不会使用activiti提供的用户信息表,因为一般公司都会有独立的用户系统,所以重构activiti的用户表是十分棘手且重要的事情。activiti中用户表是已act_id_*开头的四张表,分别是用户信息表、分组(角色)表、用户角色关联表、用户扩展信息表。具体数据表字段这里不展开叙述,大家有需要可以去网上查阅,接下来阐述怎么弃用activiti流程引擎中自带的4张用户表,构建自己的用户表。在重构的时候着实费了一番功夫,记录下来以备以后翻阅,也为大家提供一个参考。

1.POM文件依赖引入

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--activiti-->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>5.22.0</version>
        </dependency>
        <!-- shiro -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.4.0</version>
        </dependency>
        <!--mybatis start-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!--mybatis generator-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!--mybatis end-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--json-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
    </dependencies>

2.配置文件

server.port=8080
#数据库配置
spring.datasource.url = jdbc:mysql://localhost:3306/roc_process?characterEncoding=utf8&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
#关闭activiti自动部署扫描
spring.activiti.check-process-definitions=false
#上传文件大小配置
spring.servlet.multipart.max-file-size=5MB
#请求最大限制
spring.servlet.multipart.max-request-size=20MB
##指向mapper的xml文件位置
mybatis.mapper-locations=classpath:mapper/*.xml

3.自定义session工厂

重点来了:

Activiti的每一张表都有一个对应的实体管理器,在引擎初始化时会初始化所有表的实体管理器(提供CRUD等功能),每一个实体类都有一个对应的实体管理类及实体管理工厂类。实体管理工厂类实现SeesionFactory接口。

开始前先看一下我项目中的用户数据表字段。

用户信息表:

//用户编号
 private String id;
//用户昵称,用于在界面显示
 private String nickname;
//用户名
 private String username;
//密码
 private String password;
//加密盐值
 private String salt;
//状态信息
 private Integer status;

角色表

    private String id;
    //角色名称
    private String role;
    //描述
    private String description;
    //是否可用
    private Integer enable;
    //权限信息
    private String permissions;

用户、角色关联表

 private Integer id;
 //用户id
 private String userId;
 //角色id
 private String roleId;

其他数据表这里不做展示,关键的就是这三张表。

1).编写User的实体管理类

/**
 * 自定义用户管理类,管理用户方法
 * 添加其他方法
 */
@Component
public class CustomUserEntityManager extends UserEntityManager{
    @Autowired
    private RocIdUserMapper rocIdUserMapper;
    @Autowired
    private RocIdRoleMapper rocIdRoleMapper;
    @Autowired
    private RocIdUserRoleMapper rocIdUserRoleMapper;
    @Override
    public User findUserById(String userId){
        User userEntity=new UserEntity();
        RocIdUser rocIdUser=rocIdUserMapper.selectByPrimaryKey(userId);
        //将自定义的user转化为activiti的类
        userEntity=ActivitiUserUtils.toActivitiUser(rocIdUser);
        //返回activiti的实体类
        return userEntity;
    }

    @Override
    public List<Group> findGroupsByUser(final String userId) {
        if(userId==null){
            return null;
        }
        List<RocIdRole> roleList=new ArrayList<RocIdRole>();
        List<RocIdUserRole> userRoleList=rocIdUserRoleMapper.selectByUserId(userId);
        for (RocIdUserRole userrole:userRoleList
             ) {
            String roleId=userrole.getRoleId();
            RocIdRole role=rocIdRoleMapper.selectByPrimaryKey(roleId);
            roleList.add(role);
        }
        List<Group> gs=null;
        gs=ActivitiUserUtils.toActivitiGroups(roleList);
        return gs;
    }
}

2).编写Group的实体管理类

/**
 * 自定义角色管理
 * 具体方法进入GroupEntityManager中查看
 */
@Component
public class CustomGroupEntityManager extends GroupEntityManager{
    @Autowired
    private RocIdUserMapper rocIdUserMapper;
    @Autowired
    private RocIdUserRoleMapper rocIdUserRoleMapper;
    @Autowired
    private RocIdRoleMapper rocIdRoleMapper;

    @Override
    public Group createNewGroup(String groupId) {
        return super.createNewGroup(groupId);
    }

    /**
     * 查找角色
     * @param userId
     * @return
     */
    @Override
    public List<Group> findGroupsByUser(final String userId) {
        if(userId==null){
            return null;
        }
        System.out.println("userId:"+userId);
        RocIdUser rocIdUser=rocIdUserMapper.selectByUserName(userId);
        List<RocIdUserRole> userRoleList=rocIdUserRoleMapper.selectByUserId(rocIdUser.getId());
        System.out.println("userRoleList size:"+userRoleList.size());
        List<Group> gs=new ArrayList<Group>();
        GroupEntity groupEntity;
        String roleId;
        String activitiRole;
        for (RocIdUserRole userRole:userRoleList
             ) {
            groupEntity=new GroupEntity();
            groupEntity.setRevision(1);
            groupEntity.setType("assignment");
            roleId=userRole.getRoleId();
            RocIdRole role=rocIdRoleMapper.selectByPrimaryKey(roleId);
            groupEntity.setId(role.getRole());
            groupEntity.setName(role.getRole());
            gs.add(groupEntity);
        }
        return gs;
    }

}

3).编写User的管理工厂类

/**
 * 自定义user的管理工厂类
 */
@Service
public class CustomUserEntityManagerFactory implements SessionFactory{

    @Resource
    private CustomUserEntityManager customUserEntityManager;

    @Override
    public Class<?> getSessionType() {
        //此处也必须为activiti原生类
        return UserIdentityManager.class;
    }

    @Override
    public Session openSession() {
        return customUserEntityManager;
    }
    @Autowired
    public void setCustomUserEntityManager(CustomUserEntityManager customUserEntityManager){
        this.customUserEntityManager=customUserEntityManager;
    }
}

4).编写Group的管理工厂类

/**
 * 角色类的管理
 */
@Service
public class CustomGroupEntityManagerFactory implements SessionFactory{
    @Resource
    private CustomGroupEntityManager customGroupEntityManager;

    @Override
    public Class<?> getSessionType() {
        //返回原始的groupmanager类型
        return GroupIdentityManager.class;
    }

    @Override
    public Session openSession() {
        //返回自定义的GroupManager实例
        return customGroupEntityManager;
    }

    @Autowired
    public void setCustomGroupEntityManager(CustomGroupEntityManager customGroupEntityManager){
        this.customGroupEntityManager=customGroupEntityManager;
    }
}

5).编写工具类,将我们自定义的实体类转换成Activiti的实体类。

/**
 * 将业务中自己定义的用户、角色转化为activiti中使用的user、group
 */
public class ActivitiUserUtils {
    public static User toActivitiUser(RocIdUser bUser){
        User userEntity=new UserEntity();
        userEntity.setId(bUser.getUsername());
        userEntity.setFirstName(bUser.getNickname());
        userEntity.setLastName(bUser.getNickname());
        userEntity.setPassword(bUser.getPassword());
        return userEntity;
    }
    public static GroupEntity toActivitiGroup(RocIdRole sysRole){
        GroupEntity groupEntity=new GroupEntity();
        groupEntity.setRevision(1);
        groupEntity.setType("assignment");
        groupEntity.setId(sysRole.getRole());
        groupEntity.setName(sysRole.getRole());
        return groupEntity;
    }
    public static List<Group> toActivitiGroups(List<RocIdRole> sysRoles){
        List<Group> groups=new ArrayList<Group>();
        for (RocIdRole role:sysRoles
             ) {
            GroupEntity groupEntity=toActivitiGroup(role);
            groups.add(groupEntity);
        }
        return groups;
    }
}

6).activiti.cfg.xml中进行配置

    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
          id="transactionManager">
        <!--注入数据库连接池-->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/roc_process?useUnicode=true&amp;characterEncoding=utf8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>
    <!-- Activiti引擎配置 -->
    <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
        <property name="dataSource" ref="dataSource"/>
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="true"/>
        <!-- 自定义SessionFactory -->
        <property name="customSessionFactories">
            <list>
                <bean class="com.uestc.roc.process.config.identity.CustomUserEntityManagerFactory">
                    <property name="customUserEntityManager" ref="customUserEntityManager">
                    </property>
                </bean>
                <bean class="com.uestc.roc.process.config.identity.CustomGroupEntityManagerFactory">
                    <property name="customGroupEntityManager" ref="customGroupEntityManager">
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    <bean id="customUserEntityManager" class="com.uestc.roc.process.config.identity.CustomUserEntityManager">
    </bean>
    <bean id="customGroupEntityManager" class="com.uestc.roc.process.config.identity.CustomGroupEntityManager">
    </bean>

4.启动类中引入配置文件

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)   //不添加会报错
@ImportResource(locations = {"classpath:activiti.cfg.xml"})  //引入xml配置文件
public class ProcessApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProcessApplication.class, args);
    }
}

总结

其实activiti用户表的重构核心在于编写自定义的实体管理类和工厂类,覆盖activiti原来的方法,在方法中进行增删查改时使用dao接口进行操作,最后将得到的对象转化为activiti的对象,不然会出错。


致谢
activiti 自定义用户:https://blog.csdn.net/meng564764406/article/details/53789958

上一篇 下一篇

猜你喜欢

热点阅读