SpringBoot 整合 Activiti7

news2025/3/2 4:24:21

一.pom依赖引入

通过该 pom.xml 文件所导入的坐标,我们就可以实现 activiti7 与 Springboot 整合

<!--activiti7与SpringBoot整合的相关依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</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>

        <!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring-boot-starter -->
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter</artifactId>
            <version>7.0.0.Beta2</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

二.配置文件编写

为了能够实现 Activiti7 生成的表放到 Mysql 数据库中,需要在 springboot 的配置文件 application.yml 中添加相关的配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
    username : root
    password : root
    driver-class-name: com.mysql.jdbc.Driver
  activiti:
    db-history-used: true  #进行相关activiti操作时,要生成history相关的表
    history-level: full  #history相关表中的历史记录都要被记录

说明:正常如果在配置文件中不加入activiti的配置,是不会生成相关历史记录表的,所以必须加上上面的activiti配置才能在数据库中生成对应的历史记录表

三.添加 SpringSecurity 安全框 架整合配置

因为 Activiti7 与 SpringBoot 整合后,默认情况下,集成了 SpringSecurity 安全框架,这样我们就要去 准备 SpringSecurity 整合进来的相关用户权限配置信息。

1.添加 SecurityUtil工具类

package com.itheima.activiti;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class SecurityUtil {

    @Autowired
    private UserDetailsService userDetailsService;

    public void logInAs(String username) {

        UserDetails user = userDetailsService.loadUserByUsername(username);
        if (user == null) {
            throw new IllegalStateException("User " + username + " doesn't exist, please provide a valid user");
        }

        SecurityContextHolder.setContext(new SecurityContextImpl(new Authentication() {
            @Override
            public Collection<? extends GrantedAuthority> getAuthorities() {
                return user.getAuthorities();
            }

            @Override
            public Object getCredentials() {
                return user.getPassword();
            }

            @Override
            public Object getDetails() {
                return user;
            }

            @Override
            public Object getPrincipal() {
                return user;
            }

            @Override
            public boolean isAuthenticated() {
                return true;
            }

            @Override
            public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {

            }

            @Override
            public String getName() {
                return user.getUsername();
            }
        }));
        org.activiti.engine.impl.identity.Authentication.setAuthenticatedUserId(username);
    }
}

2.添加 DemoApplicationConfig 类

在 Activiti7 官方下载的 Example 中找到 DemoApplicationConfig 类,它的作用是为了实现 SpringSecurity 框架的用户权限的配置,这样我们就可以在系统中使用用户权限信息。本次项目中基 本是在文件中定义出来的用户信息,当然也可以是数据库中查询的用户权限信息。

/*
 * Copyright 2018 Alfresco, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.itheima.activiti;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
@EnableWebSecurity
public class DemoApplicationConfiguration extends WebSecurityConfigurerAdapter {

    private Logger logger = LoggerFactory.getLogger(DemoApplicationConfiguration.class);

    @Override
    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService());
    }

    @Bean
    public UserDetailsService myUserDetailsService() {

        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();

        String[][] usersGroupsAndRoles = {
                {"salaboy", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
                {"ryandawsonuk", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
                {"erdemedeiros", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
                {"other", "password", "ROLE_ACTIVITI_USER", "GROUP_otherTeam"},
                {"admin", "password", "ROLE_ACTIVITI_ADMIN"},
        };

        for (String[] user : usersGroupsAndRoles) {
            List<String> authoritiesStrings = Arrays.asList(Arrays.copyOfRange(user, 2, user.length));
            logger.info("> Registering new user: " + user[0] + " with the following Authorities[" + authoritiesStrings + "]");
            inMemoryUserDetailsManager.createUser(new User(user[0], passwordEncoder().encode(user[1]),
                    authoritiesStrings.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList())));
        }


        return inMemoryUserDetailsManager;
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .httpBasic();


    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

四.设置流程(构建bpmn文件)

五.使用 SpringBoot 整合 Junit 方式测试

package com.itheima.activiti;

import org.activiti.api.process.model.ProcessDefinition;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.activiti.api.task.model.Task;
import org.activiti.api.task.model.builders.TaskPayloadBuilder;
import org.activiti.api.task.runtime.TaskRuntime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @DESCRIPTION:
 * @USER: tangbingbing
 * @DATE: 2023/3/11 11:46
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ActivitiTest {

    @Autowired
    private ProcessRuntime processRuntime; //流程定义相关

    @Autowired
    private TaskRuntime taskRuntime;  //任务操作相关

    @Autowired
    private SecurityUtil securityUtil; //springsecurity相关的工具类

    //流程定义信息查看
    @Test
    public void testDefinition(){
        securityUtil.logInAs("salaboy"); //需要先认证
        Page<ProcessDefinition> page = processRuntime.processDefinitions(Pageable.of(0, 10));
        System.out.println(page.getTotalItems());// 查看已部署的流程
        //page.getContent()查看当前每一个部署的信息
        for (ProcessDefinition processDefinition : page.getContent()) {
            System.out.println(processDefinition);
        }
    }

    //启动流程实例
    @Test
    public void testStartInstance(){
        securityUtil.logInAs("salaboy");
        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
                .start()
                .withProcessDefinitionKey("myProcess_1")
                .build());
        System.out.println("流程实例id:" + processInstance.getId()); //流程实例id
    }

    //查询任务,并完成任务
    @Test
    public void testTask(){
        securityUtil.logInAs("ryandawsonuk");
        Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
        if (tasks.getTotalItems() > 0) {
            for (Task task : tasks.getContent()) {
                System.out.println("任务1:" + task);
                //拾取任务
                taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
                //执行任务
                taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
            }
        }
        //再次查看任务
        tasks = taskRuntime.tasks(Pageable.of(0, 10));
        if (tasks.getTotalItems() > 0) {
            for (Task task : tasks.getContent()) {
                System.out.println("任务2:" + task);
            }
        }

    }

    //查询任务,并完成任务
    @Test
    public void testTaskAll(){
        securityUtil.logInAs("erdemedeiros");
        Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
        if (tasks.getTotalItems() > 0) {
            for (Task task : tasks.getContent()) {
                System.out.println("任务1:" + task);
                //拾取任务
                taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
                //执行任务
                taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
            }
        }
        //再次查看任务
        tasks = taskRuntime.tasks(Pageable.of(0, 10));
        if (tasks.getTotalItems() > 0) {
            for (Task task : tasks.getContent()) {
                System.out.println("任务2:" + task);
            }
        }

    }

}

六.整合SpringMVC

代码与测试类中相似

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/404646.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【教学典型案例】04.生产环境app打包导致不能自动升级

目录一&#xff1a;背景介绍现象原因产生影响二&#xff1a;问题复现过程1、注册服务空间2、添加Admin项目3、创建uniapp项目4、App打包5、发布到升级中心三&#xff1a;总结一&#xff1a;背景介绍 现象 用户端安装了74版本的app&#xff0c;不能自动升级。 原因 产生该问…

win10 +cuda11.3.1+VS2019-社区版+drive445.87

参考&#xff1a;https://blog.csdn.net/kunhe0512/article/details/124331221这里的推荐离线安装包的方式进行cuda安装&#xff0c;官方也给了conda的安装方式&#xff0c;我试过一直出问题&#xff0c;所以安装包的方式比较靠谱&#xff0c;windows比linux下更方便。vs2019-c…

Uipath Excel 自动化系列20-Insert Chart(插入图表)

活动描述 Insert Chart(插入图表)&#xff1a;在Excel工作表的指定位置插入图表&#xff0c;该活动需与Use Excel File 活动选择的 Excel 文件一起使用。 使用如下图&#xff1a; Insert Chart(插入图表)属性配置 属性 作用 DisplayName 在设计器面板中设置活动显示的名称…

计算机网络笔记——计算机网络体系结构

计算机网络笔记——计算机网络体系结构1.计算机网络体系结构1.1 计算机网络概述1.1.1 计算机网络的概念1.1.2 计算机网络组成物理组成工作方式组成功能组成1.1.3 计算机网路的功能1.1.4 计算机网络的分类1.1.5 计算机网络的标准化公工作及相关组织1.2 计算机网络体系结构与参考…

Python调用Shell命令 (python,shell 混合编程)

Python经常被称作“胶水语言”&#xff0c;因为它能够轻易地操作其他程序&#xff0c;轻易地包装使用其他语言编写的库&#xff0c;也当然可以用Python调用Shell命令。 用Python调用Shell命令有如下几种方式&#xff1a; 1. os.system os.system("The command you want&…

fiddler

文章目录fiddler简介URL与HTTPURLhttp模拟测试场景弱网测试自定义规则前端性能分析及优化fiddler 简介 Fiddler是位于客户端和服务端的HTTP代理 目前最常用的http抓包工具之一功能非常强大&#xff0c;是web调试的利器 监控浏览器所有的HTTP/HTTPS流量查看、分析请求内容细节伪…

Android之屏幕适配方案

在说明适配方案之前&#xff0c;我们需要对如下几个概念有所了解&#xff1a;屏幕尺寸&#xff0c;屏幕分辨率&#xff0c;屏幕像素密度。 屏幕尺寸 屏幕尺寸指屏幕的对角线的物理长度&#xff0c;单位是英寸&#xff0c;1英寸2.54厘米。 比如常见的屏幕尺寸&#xff1a;5.0、5…

粒子群优化SVM含水率预测

目录 支持向量机SVM的详细原理 SVM的定义 SVM理论 SVM应用实例,基于粒子群改进SVM的含水率回归分析 代码 结果分析 展望 支持向量机SVM的详细原理 SVM的定义 支持向量机(support vector machines, SVM)是一种二分类模型,它的基本模型是定义在特征空间上的间隔最大的线性…

python --生成时间序列,作为横轴的标签。时间跨越2008-2022年,生成每年的6-10月的第一天作为时间序列

python 生成制定的时间序列作为绘图时x轴的标签 问题需求 在绘图时&#xff0c;需要对于x轴的标签进行专门的设置&#xff0c;整体时间跨越2008年-2022年&#xff0c;将每年的6-10月的第一天生成一条时间序列&#xff0c;绘制成图。 解决思路 对于时间序列的生成&#xff0…

【react 全家桶】条件渲染

文章目录05 【条件渲染】基础配置1.条件判断语句2.三目运算符3.与运算符&&4.元素变量5.阻止组件渲染05 【条件渲染】 在 React 中&#xff0c;你可以创建不同的组件来封装各种你需要的行为。然后&#xff0c;依据应用的不同状态&#xff0c;你可以只渲染对应状态下的部…

VB实现点爆炸效果

需在窗体放置以下 4 个控件&#xff0c;所有控件不用设置任何属性&#xff0c;均采用默认设置&#xff1a; ’ Picture1&#xff0c;Command1&#xff0c;Check1&#xff0c;Timer1 Option Explicit Dim I Dim ctD() As tyD, ctDs As Long, ctR As Single Private Type tyD x…

java单元测试(二)H2数据库篇

java单元测试&#xff08;二&#xff09;H2数据库篇一、什么是H2&#xff1f;二、Springboot项目集成H22.1、引入H2依赖2.2、初始化spring配置文件application.test.xml2.3、初始化H2数据库DDL/DML语句三、编写单元测试3.1、首先我们创建测试类3.2、编写测试用例3.3、测试用例一…

elasticsearch集群搭建(ES集群搭建)

目录一、下载Elasticsearch1.选择你要下载的Elasticsearch版本二、采用通用搭建集群的方法三、配置三台es1.上传压缩包到任意一台虚拟机中2.解压并修改配置文件(配置单台es)3.配置三台es集群一、下载Elasticsearch 1.选择你要下载的Elasticsearch版本 es下载地址 这里我下载…

Mysql架构以及Mysql引擎

Mysql架构1.连接层&#xff1a;负责接收客户端的连接请求最上层是一些客户端和连接服务&#xff0c;包含本地 sock 通信和大多数基于客户端/服务端工具实现的类似于 tcp/ip 的通信。主要完成一些类似于连接处理、授权认证、及相关的安全方案。2.服务层&#xff1a;sql接口接收s…

【UEFI实战】BIOS与IPMI

KCS KCS全称是Keyboard Controller Style&#xff0c;关于这个名称不用过多的追究&#xff0c;只需要知道它是系统&#xff08;BIOS和OS&#xff09;和BMC通信的一种基本方式即可。本文将介绍BIOS下的KCS接口&#xff0c;包括接口使用方式和数据。内容参考自《ipmi-second-gen…

[LeetCode周赛复盘] 第 99 场双周赛20230304

[LeetCode周赛复盘] 第 99 场双周赛20230304 一、本周周赛总结二、 [Easy] 2578. 最小和分割1. 题目描述2. 思路分析3. 代码实现三、[Medium] 2579. 统计染色格子数1. 题目描述2. 思路分析3. 代码实现四、[Medium] 2580. 统计将重叠区间合并成组的方案数1. 题目描述2. 思路分析…

DALL·E 2 论文阅读笔记

《Hierarchical Text-Conditional Image Generation with CLIP Latents》Paper: https://cdn.openai.com/papers/dall-e-2.pdfProject: https://openai.com/product/dall-e-2Author: OpenAI闲言碎语时间线&#xff1a;2021.01推出DALLE&#xff0c;2021年底推出GLIDE&#xff0…

锦正茂风冷系列电源JCP-10-80的技术参数

JCP-10-80为高稳定度的双极性恒流电源&#xff0c;广泛应用于电磁铁、亥姆霍兹线圈等感性负载的励磁。电源采用线性电源结构&#xff0c;输出电流稳定度高&#xff0c;纹波和噪声低。电源输出电流可在正负额定电流*值之间连续变化&#xff0c;电流平滑连续过零&#xff0c;可使…

引出生命周期、生命周期_挂载流程、生命周期_更新流程、生命周期_销毁流程、生命周期_总结——Vue

目录 一、引出生命周期 二、生命周期_挂载流程 三、生命周期_更新流程 四、生命周期_销毁流程 五、生命周期_总结 一、引出生命周期 生命周期&#xff1a; 1.又名&#xff1a;生命周期回调函数、生命周期函数、生命周期钩子。 2.是什么&#xff1a;Vue在关键时刻帮我们调…

centos安装 jenkins

目录 一、安装 jdk 二、安装 maven 三、安装 jenkins 官网地址&#xff1a;Jenkins 一、安装 jdk 首先检索包含 java 的列表 yum list java* 检索1.8的列表 yum list java-1.8* 安装 1.8.0 的所有文件 yum install java-1.8.0-openjdk* -y使用命令检查是否安装成功 …