SpringBoot2整合Activiti7
步骤一:引入依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> </parent> <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>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</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 : 123 driver-class-name: com.mysql.jdbc.Driver activiti: db-history-used: true
步骤三:将SpringSecurity配置添加到项目当中
1.SecurityUtil.java 关于权限登陆
@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.DemoApplicationConfiguration 关于权限配置
@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(); } }
步骤四:启动工程
@SpringBootApplication public class StartActiviti { public static void main(String[] args) { SpringApplication.run(StartActiviti.class,args); } }
自动部署:
注意问题:
1.Activiti7和SpringSecurity耦合,需要加入SpringSecurity的依赖和配置,我们可以使用Security中的用户角色组定义流程执行的组
2.流程默认可自动部署,但是需要再resources/processes文件夹,将流程文件放入当中
3.默认历史表不会生成,需要手动开启配置
执行流程:
controller
@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); } } /** * 启动流程示例 */ @RequestMapping("/startInstance") public void startInstance(){ ProcessInstance instance = processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("demo").build()); System.out.println(instance.getId()); } /** * 获取任务,拾取任务,并且执行 */ @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()); } } } }
1.查询流程定义
需要先经过权限验证
账号密码在DemoApplicationConfiguration配置类
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"}, };
控制台打印信息:
2.启动流程示例
3.获取任务,拾取任务,并且执行