SpringBoot整合Activiti7的实现代码

作者:Jello 时间:2023-11-03 20:58:29 

Activiti7发布正式版之后,它与SpringBoot2.x已经完全支持整合开发。我们可以将Activiti7与SpringBoot整合开发的坐标引入到工程中,从而达到SpringBoot支持Activti7整合。

1.使用IDEA创建SpringBoot项目

在工程的pom.xml文件中引入相关的依赖,具体依赖如下所示


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.2.1.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example</groupId>
 <artifactId>activiti</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>activiti</name>
 <description>Demo project for Spring Boot</description>
<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   <java.version>1.8</java.version>
 </properties>
<dependencies>
   <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>
<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.27</version>
   </dependency>
   <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <optional>true</optional>
   </dependency>
 </dependencies>
<build>
   <plugins>
     <plugin>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
   </plugins>
 </build>
</project>

2.application.yml文件配置

为了能够实现Activiti7生成的表放到Mysql数据库中,需要在配置文件application.yml中添加相关的配置


server:
port: 8085
spring:
application:
 name: spring-activiti
datasource:
 url: jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&nullCatalogMeansCurrent=true
 username: root
 password: 123456
 driver-class-name: com.mysql.jdbc.Driver
activiti:
 #1.flase: 默认值。activiti在启动时,会对比数据库表中保存的版本,如果没有表或者版本不匹配,将抛出异常
 #2.true: activiti会对数据库中所有表进行更新操作。如果表不存在,则自动创建
 #3.create_drop: 在activiti启动时创建表,在关闭时删除表(必须手动关闭引擎,才能删除表)
 #4.drop-create: 在activiti启动时删除原来的旧表,然后在创建新表(不需要手动关闭引擎)
 database-schema-update: true
 #检测历史表是否存在
 db-history-used: true
 #记录历史等级 可配置的历史级别有none, activity, audit, full
 history-level: full
 #校验流程文件,默认校验resources下的processes文件夹里的流程文件
 check-process-definitions: false

3.添加SpringSecurity安全框架整合配置

Activiti7与SpringBoot整合后,默认情况下,集成了SpringSecurity安全框架,这样我们就要去准备SpringSecurity整合进来的相关用户权限配置信息

1)添加SecurityUtil类

为了能够快速实现SpringSecurity安全框架的配置,所添加的一个组件。


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

这个类可以从Activiti7官方提供的Example中找到。

2)添加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<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)创建测试类


@SpringBootTest
@Slf4j
class ActivitiApplicationTests {
@Autowired
 private ProcessEngine processEngine;
/**
  * 流程定义的部署
  */
 @Test
 public void createDeploy() {
   RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()
       .addClasspathResource("diagram/holiday.bpmn")//添加bpmn资源
       .addClasspathResource("diagram/holiday.png")
       .name("请假申请单流程")
       .deploy();
log.info("流程部署id:" + deployment.getName());
   log.info("流程部署名称:" + deployment.getId());
 }
}

运行结果:

SpringBoot整合Activiti7的实现代码

SpringBoot整合Activiti7的实现代码

遇到的问题:

1)历史表默认不会生成,需在application.yml中配置


spring:
activiti:
 #检测历史表是否存在
 db-history-used: true
 #记录历史等级 可配置的历史级别有none, activity, audit, full
 history-level: full

来源:https://segmentfault.com/a/1190000021077964

标签:Spring,Boot,Activiti7
0
投稿

猜你喜欢

  • Linux CentOS 6.5 ifconfig查询不到ip的解决方法

    2023-10-18 12:25:37
  • 暴雪披露产品路线图巫妖王之怒明年在华运营

    2009-11-17 15:38:00
  • Windows服务器:切断默认共享通道七招

    2007-09-10 12:45:00
  • 病毒式网络营销五个引爆点 把网站传染出去

    2009-02-17 10:00:00
  • 新手部署DNS服务器须知

    2010-01-20 19:07:00
  • SSL 加密的 Google 网页搜索已经上线

    2010-05-27 10:14:00
  • PHPWind v7.3.2“引用”的返回链接如果链接到主题部分出错

    2009-05-31 17:09:00
  • 详解SupeSite7.0资讯首页如何显示子分类信息

    2009-06-05 17:50:00
  • 在Fedora上建立自己的邮件服务器(2)

    2007-09-07 14:02:00
  • vim编辑器常用命令总结

    2023-08-06 22:30:18
  • VMware配置VMnet8网络的方法步骤

    2023-11-04 02:11:41
  • 关注网页游戏 互联网基础服务成关键

    2009-10-14 15:44:00
  • Apache下ModSecurity的安装启用与配置

    2023-07-15 18:13:15
  • HostNine-美国主机商介绍和评测

    2010-03-30 08:05:00
  • 百度发布2008春节热点报告

    2008-02-26 09:51:00
  • SEO - 我们是不是走错了路?

    2008-11-16 18:44:00
  • 亲手教你Docker Compose安装DOClever的详细过程

    2021-05-12 08:54:08
  • 十分钟让你的外贸网站更专业

    2009-01-23 16:51:00
  • 一定要设计好一个站的域名品牌

    2008-12-26 10:40:00
  • 网络推广必学-如何写出一篇好软文

    2009-07-09 17:55:00
  • asp之家 网站运营 m.aspxhome.com