目录
2 引入的依赖
3 添加配置类(因为activiti内置了springboot的安全框架,所以需要配置)
4 添加安全工具类(也是因为安全框架)
5 在项目运行前,需要手动建表
6 yml配置
7 启动即可
该项目仅作为Demo使用,仅作为工作流学习的项目,以此学习工作流activiti的基本操作。
项目的github地址:
githubhttps://github.com/DamonKima/activiti/tree/main
mysql-connector-j
lombok
spring-boot-starter-test
spring-boot-starter-jdbc
activiti-spring-boot-starter
junit-jupiter
activiti-spring-boot-starter8.1.0版本的最新依赖在maven官网中没有更新,需要导入activiti的库才行。
activiti8依赖地址https://activiti.gitbook.io/activiti-7-developers-guide/releases/8.1.0
即引入库:
activiti-releases https://artifacts.alfresco.com/nexus/content/repositories/activiti-releases
pom.xml文件如下:
4.0.0 org.springframework.boot spring-boot-starter-parent3.2.0 com.example Activiti8Test0.0.1-SNAPSHOT Activiti8Test Activiti8Test 17 activiti-releases https://artifacts.alfresco.com/nexus/content/repositories/activiti-releases org.springframework.boot spring-boot-starter-webcom.mysql mysql-connector-jruntime org.projectlombok lomboktrue org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-jdbcorg.activiti activiti-spring-boot-starter8.1.0 org.junit.jupiter junit-jupitertest org.springframework.boot spring-boot-maven-pluginorg.projectlombok lombok
项目目录如下:
该类是config下的DemoApplicationConfiguration类
package com.example.activiti8test.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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 public class DemoApplicationConfiguration { private Logger logger = LoggerFactory.getLogger(DemoApplicationConfiguration.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) { ListauthoritiesStrings = 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(); } }
该类是utils下的SecurityUtil
package com.example.activiti8test.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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 { 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); } }
在外部库中找到activiti-engine依赖,在org.activiti的db的create中找到mysql的建表语句,自己进行建表。
如果使用的连接器(mysql-connector-java)版本在6以下,使用的驱动类就是com.mysql.jdbc.Driver,如果连接器版本是6以及6以上,使用的驱动类就是com.mysql.cj.jdbc.Driver,如果依然使用过的是com.mysql.jdbc.Driver,那么就会有个警告性的错误提示:
Loading class 'com.mysql.jdbc.Driver'. This is deprecated. The new driver class is 'com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
记得修改username和password
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/activiti8?nullCatalogMeansCurrent=true username: root password: root activiti: database-schema-update: true db-history-used: true history-level: full check-process-definitions: true
启动即可