Activiti-SpringBoot整合
Activiti-SpringBoot整合
Activiti7发布正式版之后,它与springboot2.x已经完全支持整合开发;我们可以将activiti7与springboot整合开发的坐标引入到工程中,从而达到springboot支持activiti7整合;
springboot整个activiti7的具体步骤如下:
1.添加springboot整个activiti7的坐标;
2.添加SpringSecurity安全框架的整合配置信息;
3.使用activiti7新支持的类来实现工作流开发;
ProcessRuntime接口;
TaskRuntime接口;
4.使用新的API实现工作流开发,主要包括;
流程定义查询
启动流程实例
任务的查询
任务的完成
一、目录展示
二、导入依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent>
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</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>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
三、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
四、添加 SpringSecurity安全框架整合配置
因为Activiti7 与SpringBoot 整合后,默认情况下,集成了 SpringSecurity 安全框架,这样我们就要去准备SpringSecurity 整合进来的相关用户权限配置信息。
1、添加SecurityUtil类
为了能够快速实现 SpringSecurity 安全框架的配置,所添加的一个组件。
package com.zn.avtiviti; 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 框架的用户权限的配置,这样我们就可以在系统中使用用户权限信息。本次项目中基本是在文件中定义出来的用户信息,当然也可以是数据库中查询的用户权限信息。
package com.zn.avtiviti; 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文件
(将流程图放在resources下的processes包里)
六、启动类
package com.zn; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class StartActiviti { public static void main(String[] args) { SpringApplication.run(StartActiviti.class,args); } }
注意问题:
1.Activiti7和SpringSecurity耦合,需要加入SpringSecurity的依赖和配置,我们可以使用Security中的用户角色组定义流程执行的组
2.流程默认可自动部署,但是需要再resources/processes文件夹,将流程文件放入当中
3.默认历史表不会生成,需要手动开启配置
七、查询流程定义
1、代码块
package com.zn.controller; import com.zn.avtiviti.SecurityUtil; import org.activiti.api.process.model.ProcessDefinition; 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.runtime.TaskRuntime; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/activiti") public class ActivitiController { @Resource private ProcessRuntime processRuntime; @Resource private TaskRuntime taskRuntime; @Resource private SecurityUtil securityUtil; /** * 查询流程定义 */ @RequestMapping("/getProcess") public void getProcess(){ //查询所有流程定义信息 Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10)); System.out.println("当前流程定义的数量:"+processDefinitionPage.getTotalItems()); //获取流程信息 for (ProcessDefinition processDefinition:processDefinitionPage.getContent()) { System.out.println("流程定义信息"+processDefinition); } } }
2、数据库
3、访问localhost:8080/activiti/getProcess进行登录
4、控制台
八、启动流程示例
1、代码块
package com.zn.controller; import com.zn.avtiviti.SecurityUtil; 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.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/activiti") public class ActivitiController { @Resource private ProcessRuntime processRuntime; @Resource private TaskRuntime taskRuntime; @Resource private SecurityUtil securityUtil; /** * 启动流程示例 */ @RequestMapping("/startInstance") public void startInstance(){ ProcessInstance instance = processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("demo").build()); System.out.println(instance.getId()); } }
2、访问localhost:8080/activiti/startInstance
3、数据库
九、拾取任务
1、代码块
package com.zn.controller; import com.zn.avtiviti.SecurityUtil; 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.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @RequestMapping("/activiti") public class ActivitiController { @Resource private ProcessRuntime processRuntime; @Resource private TaskRuntime taskRuntime; @Resource private SecurityUtil securityUtil; /** * 获取任务,拾取任务,并且执行 */ @RequestMapping("/getTask") public void getTask(){ securityUtil.logInAs("salaboy"); //指定组内任务人 Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10)); if(tasks.getTotalItems()>0){ for (Task task:tasks.getContent()) { System.out.println("任务名称:"+task.getName()); //拾取任务 taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build()); //执行任务 taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build()); } } } }
2、访问localhost:8080/activiti/getTask
3、控制台