SpringBoot整合工作流Activiti7

Posted vbirdbest

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合工作流Activiti7相关的知识,希望对你有一定的参考价值。

SpringBoot整合Activiti7需要与SpringSecurity整合。
SpringBoot整合Activiti7之后的所有数据库Id值都使用了类似于UUID样的值。
SpringBoot又封装了一套API来操作Activiti。

1. pom.xml

<dependency>
	<groupId>org.activiti</groupId>
	<artifactId>activiti-spring-boot-starter</artifactId>
	<version>7.0.0.SR1</version>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>2.2.0</version>
</dependency>

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
</dependency>
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<optional>true</optional>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

2. applicaton.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
    username: root
    password: root123
    driver-class-name: com.mysql.cj.jdbc.Driver
  activiti:
    #1.flase:默认值。activiti在启动时,对比数据库表中保存的版本,如果没有表或者版本不匹配,将抛出异常
    #2.true: activiti会对数据库中所有表进行更新操作。如果表不存在,则自动创建
    #3.create_drop: 在activiti启动时创建表,在关闭时删除表(必须手动关闭引擎,才能删除表)
    #4.drop-create: 在activiti启动时删除原来的旧表,然后在创建新表(不需要手动关闭引擎)
    database-schema-update: true
    #检测历史表是否存在 activiti7默认没有开启数据库历史记录 启动数据库历史记录
    db-history-used: true
    #记录历史等级 可配置的历史级别有none, activity, audit, full
    #none:不保存任何的历史数据,因此,在流程执行过程中,这是最高效的。
    #activity:级别高于none,保存流程实例与流程行为,其他数据不保存。
    #audit:除activity级别会保存的数据外,还会保存全部的流程任务及其属性。audit为history的默认值。
    #full:保存历史数据的最高级别,除了会保存audit级别的数据外,还会保存其他全部流程相关的细节数据,包括一些流程参数等。
    history-level: full
    #校验流程文件,true表示自动部署resources下的processes文件夹里的流程文件
    check-process-definitions: true

3. Spring Security

@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);
    

@Configuration
public class SpringSecurityConfiguration 
    private Logger logger = LoggerFactory.getLogger(SpringSecurityConfiguration.class);
     @Bean
     public UserDetailsService myUserDetailsService() 
         InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
         // 这里添加用户,后面处理流程时用到的任务负责人,需要添加在这里
         String[][] usersGroupsAndRoles = 
                 "jack", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam",
                 "rose", "password", "ROLE_ACTIVITI_USER", "GROUP_activitiTeam",
                 "tom", "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<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;
     
     @Bean
     public PasswordEncoder passwordEncoder() 
         return new BCryptPasswordEncoder();
     

4. .bpmn

Candidate Groups候选组,只要是组里面的人都可以拾取任务,办理任务。

Candidate Groups中的内容与上面SpringSecurityConfiguration类中出现的用户组名称要保持一致,可以填写:group1 或者 group2。

5. test

@SpringBootTest
class SpringbootActiviti7ApplicationTests 

	@Autowired
	private ProcessRuntime processRuntime;
	@Autowired
	private TaskRuntime taskRuntime;
	@Autowired
	private SecurityUtil securityUtil;
	

	@Test
	void testQueryProcessDefinition() 
		securityUtil.logInAs("system");

		Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10));
		for (ProcessDefinition processDefinition : processDefinitionPage.getContent()) 
			System.out.println(processDefinition.getKey());
		
	


	@Test
	void testStartProcess() 
		securityUtil.logInAs("system");
		processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("HelloWorldProcess").build());
	

	@Test
	public void testCompleteTask() 
		securityUtil.logInAs("jack");
		Page<Task> taskPage = taskRuntime.tasks(Pageable.of(0, 10));
		if (taskPage.getTotalItems() > 0) 
			for (Task task : taskPage.getContent()) 
				// 拾取任务
				taskRuntime.claim(TaskPayloadBuilder.claim().withTaskId(task.getId()).build());
				// 完成任务
				taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build());
			
		
	

以上是关于SpringBoot整合工作流Activiti7的主要内容,如果未能解决你的问题,请参考以下文章

activiti7与springboot2整合

Activiti7工作流引擎:进阶篇 SpringBoot整合Activiti6

二十一SpringBoot2核心技术——整合activiti7

springboot+activiti7之拖拽表单kdesign整合

Activiti7工作流引擎:进阶篇 Activiti7与Spring整合

Activiti 7 springboot 工作流引擎