Activiti 如何与springboot进行整合?
1. 添加springboot整合 Activiti的坐标。
2. 添加springSecurity的安全框架的整合配置信息。
3. 使用Activiti新支持的类来实现工作流的开发
ProcessRuntimeService接口
TaskRuntimeService接口
4. 使用新的API实现工作流开发,主要包括
(1)流程定义查询
(2)启动流程实例。
(3)任务查询。
(4)任务完成。
为了能够实现springboot与Activiti7 的整合开发,首先我们需要引入相关依赖的支持,所以我们在工作的pom.xml文件中引入相关依赖,具体的依赖如下:
org.springframework.boot
spring-boot-starter-parent
2.1.0.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.activiti
activiti-spring-boot-starter
7.0.0.Beta2
org.mybatis
mybatis
3.4.5
mysql
mysql-connector-java
5.1.27
org.springframework.boot
spring-boot-maven-plugin
通过pom.xml文件所导入的坐标依赖, 我们就可以实现activiti7 与springboot的有效整合。
为了能够实现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
(1)添加 SecurityUtil类
为了能够快速实现 SpringSecurity 安全框架的配置,所添加的一个组件。
这个类可以从我们下载的 Activiti7 官方提供的 Example 中找到。
@Component
public class SecurityUtil {
private Logger logger = LoggerFactory.getLogger(SecurityUtil.class);
@Autowired
@Qualifier("myUserDetailsService")
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");
}
logger.info("> Logged in as: " + username);
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 框架的用户权限的配置,这样我们就可以在系统中使用用户权限信息。本次项目中基
本是在文件中定义出来的用户信息,当然也可以是数据库中查询的用户权限信息。
@Configuration
public class DemoApplicationConfiguration {
private Logger logger =
LoggerFactory.getLogger(DemoApplicationConfiguration.class);
@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"},
{"system", "password", "ROLE_ACTIVITI_USER"},
{"admin", "password", "ROLE_ACTIVITI_ADMIN"},
};
for (String[] user : usersGroupsAndRoles) {
List 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;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
使用springboot整合Junit方式测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Actviti7DemoApplicationTests {
@Autowired
private ProcessRuntime processRuntime;
@Autowired
private TaskRuntime taskRuntime;
@Autowired
private SecurityUtil securityUtil;
}
/**
* 查看流程定义
*/
@Test
public void contextLoads() {
securityUtil.logInAs("system");
Page processDefinitionPage =processRuntime.processDefinitions(Pageable.of(0, 10));
System.out.println(" 可 用 的 流 程 定 义 数 量 : " +
processDefinitionPage.getTotalItems());
for (org.activiti.api.process.model.ProcessDefinition pd :
processDefinitionPage.getContent()) {
System.out.println("流程定义:" + pd);
}
}
/**
* 启动流程实例
*/
@Test
public void testStartProcess() {
securityUtil.logInAs("system");
ProcessInstance pi =
processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("myProc
ess").build());
System.out.println("流程实例 ID:" + pi.getId());
}
/**
* 查询任务,并完成自己的任务
*/
@Test
public void testTask() {
securityUtil.logInAs("ryandawsonuk");
Page taskPage=taskRuntime.tasks(Pageable.of(0,10));
if (taskPage.getTotalItems()>0){
for (Task task:taskPage.getContent()){
taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
System.out.println(" 任务:"+task);
taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build()
);
}
}
Page taskPage2=taskRuntime.tasks(Pageable.of(0,10));
if (taskPage2.getTotalItems()>0){
System.out.println(" 任务:"+taskPage2.getContent());
}
}
使用TaskRuntime接口的task()方法实现任务的查询
使用TaskRuntime接口的claim()方法实现任务拾取
使用TaskRunTime接口complete()方法实现任务完成。