整合Activit7
pom.xml文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>boot-activiti7</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<activiti.version>7.1.0.M6</activiti.version>
<mybatis-plus.version>3.3.1</mybatis-plus.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus.version}</version>
</dependency>
<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>
application.yml文件:
server:
servlet:
context-path: /workflow
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/activiti01?nullCatalogMeansCurrent=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
username: root
password: 1234
activiti:
database-schema-update: true
# activiti7 默认不生成历史信息表,true开启历史表
db-history-used: true
# 记录历史等级 可配置的历史级别有none, activity, audit, full
# none:不保存任何的历史数据,因此,在流程执行过程中,这是最高效的。
# activity:级别高于none,保存流程实例与流程行为,其他数据不保存。
# audit:除activity级别会保存的数据外,还会保存全部的流程任务及其属性。
# full:保存历史数据的最高级别,除了会保存audit级别的数据外,还会保存其他全部流程相关的细节数据,包括一些流程参数等。
history-level: full
# 是否自动检查 resources 下的 processes 目录中的流程文件
check-process-definitions: false
# 每次启动项目,默认会向 act_re_deployment 表插入一条 SpringAutoDeployment 数据,指定never-fail 可关闭插入
# deployment-mode: never-fail
logging:
level:
org.activiti.engine.impl.persistence.entity: debug
主启动类:
@SpringBootApplication
public class Activiti7Application {
public static void main(String[] args) {
SpringApplication.run(Activiti7Application.class, args);
}
}
测试类:
@SpringBootTest
public class Activiti7Test01 {
@Autowired
ProcessEngine processEngine;
@Test
public void test1() {
System.out.println(processEngine);
}
}
运行结果:
查询历史信息:整合了SpringBoot后,所有的activiti相关的service可以通过@Autowired注入获取
@Autowired
HistoryService historyService;
@Test
public void historyInfo(){
// 1. 实例化流程引擎实例
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 2. 获取 HistoryService
HistoryService historyService = processEngine.getHistoryService();
// 3. 获取节点历史记录查询对象 ACT_HI_ACTINST 表
HistoricActivityInstanceQuery query = historyService.createHistoricActivityInstanceQuery();
// 实例 id
String processInstanceId = "5001";
List<HistoricActivityInstance> list = query.processInstanceId(processInstanceId)
.orderByHistoricActivityInstanceStartTime() // 根据开始时间排序 asc 升序
.asc()
.list();
for (HistoricActivityInstance hi : list) {
System.out.print("流程定义ID: " + hi.getProcessDefinitionId());
System.out.print(",流程实例ID: " + hi.getProcessInstanceId());
System.out.print(",节点ID: " + hi.getActivityId());
System.out.print(",节点名称: " + hi.getActivityName());
System.out.print(",任务办理人:" + hi.getAssignee());
System.out.print(",开始时间:" + hi.getStartTime());
System.out.println("结束时间:" + hi.getEndTime());
}
}
整合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 = {
{"zhangsan", "123456", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
{"wj1", "123456", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
{"wj2", "123456", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam"},
{"张三", "123456", "ROLE_ACTIVITI_ADMIN", "GROUP_otherTeam"},
{"李四", "123456", "ROLE_ACTIVITI_ADMIN", "GROUP_otherTeam"},
{"王五", "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();
}
}