pom.xml
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
<activiti.version>7.1.0.M6</activiti.version>
<mybatis-plus.version>3.3.1</mybatis-plus.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/>
</parent>
<dependencies>
<!-- Activiti -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- java绘制activiti流程图 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-image-generator</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- activiti json转换器-->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- svg转png图片工具-->
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-all</artifactId>
<version>1.10</version>
</dependency>
<!-- web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-plusǷ-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<!-- SpringSecurity -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Springboot整合Activiti时,需要整合SpringSecurity否则就会报错
application.yml文件
server:
port: 80
servlet:
context-path: /workflow
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/activiti-boot?nullCatalogMeansCurrent=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
username: root
password: root
#activiti配置
activiti:
# 自动更新数据库结构
# true:适用开发环境,默认值。activiti会对数据库中所有表进行更新操作。如果表不存在,则自动创建
# false:适用生产环境。activiti在启动时,对比数据库表中保存的版本,如果没有表或者版本不匹配,将抛出异常
# create_drop: 在activiti启动时创建表,在关闭时删除表(必须手动关闭引擎,才能删除表)
# drop-create: 在activiti启动时删除原来的旧表,然后在创建新表(不需要手动关闭引擎)
database-schema-update: true
# activiti7与springboot整合后默认不创建历史表,需要手动开启
db-history-used: true
# 记录历史等级 可配置的历史级别有none, activity, audit, full
# none:不保存任何的历史数据,因此,在流程执行过程中,这是最高效的。
# activity:级别高于none,保存流程实例与流程行为,其他数据不保存。
# audit:除activity级别会保存的数据外,还会保存全部的流程任务及其属性。
# full:保存历史数据的最高级别,除了会保存audit级别的数据外,还会保存其他全部流程相关的细节数
# 据,包括一些流程参数等。
history-level: full
# 是否自动检查resources下的processes目录流程定义文件
check-process-definitions: false
# 日志级别是debug才能显示SQL日志
logging:
level:
org.activiti.engine.impl.persistence.entity: debug
数据库mysql:5.7
主启动类
@SpringBootApplication
public class workflowApplication {
public static void main(String[] args) {
SpringApplication.run(workflowApplication.class,args);
}
}
测试类
@SpringBootTest
public class ActivitiTesto1 {
@Autowired
ProcessEngine processEngine;
@Autowired
RepositoryService repositoryService;
@Test
public void getProcessEngine() {
System.out.println("processEngine: " + processEngine);
}
返回结果
processEngine: org.activiti.engine.impl.ProcessEngineImpl@4cddc3d9 //获取流程实例的时候,数据库的25张表就已经创建出来了.
通过zip压缩包部署流程定义
/**
* 通过zip压缩包部署流程定义
*/
@Test
public void deplayByZip(){
//部署流程定义
//读取zip资源压缩包,转成输入流
InputStream is = ReflectUtil.getResourceAsStream("processes/leave.zip");
ZipInputStream zipInputStream = new ZipInputStream(is);
Deployment deployment = repositoryService.createDeployment()
.addZipInputStream(zipInputStream)
.name("请假申请流程压缩包-boot")
.deploy();
//部署输出结果
System.out.println(deployment.getId());
System.out.println(deployment.getName());
}
返回结果
d5c1d3f3-dd04-11ec-a50b-54e1ad82f401
请假申请流程压缩包-boot
整合SpringSecurity
activiti7 与 SpringSecurity 强耦合, 创建安全认证配置类SpringSecurityConfig 类
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
private final Logger logger = LoggerFactory.getLogger(SpringSecurityConfig.class);
/**
* 内存 UserDetailsManager
*/
@Bean
public UserDetailsService myUserDetailsService() {
InMemoryUserDetailsManager inMemoryUserDetailsManager = new
InMemoryUserDetailsManager();
// 初始化账号角色数据
addGroupAndRoles(inMemoryUserDetailsManager);
return inMemoryUserDetailsManager;
}
private void addGroupAndRoles(UserDetailsManager userDetailsManager) {
// 注意:后面流程办理人,必须是当前存在的用户 username
String[][] usersGroupsAndRoles = {
{"wkh", "123456", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
{"zdx", "123456", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
{"wll", "123456", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
{"wsy", "123456", "ROLE_ACTIVITI_ADMIN", "GROUP_otherTeam"},
{"zyb", "123456", "ROLE_ACTIVITI_ADMIN", "GROUP_otherTeam"},
{"gj", "123456", "ROLE_ACTIVITI_ADMIN", "GROUP_otherTeam"}
};
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 + "]");
userDetailsManager.createUser(new User(user[0],
passwordEncoder().encode(user[1]),
authoritiesStrings.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList())));
}
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}