SpringBoot怎么整合Activiti7
本篇内容主要讲解“SpringBoot怎么整合Activiti7”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot怎么整合Activiti7”吧!
创新互联建站成立与2013年,是专业互联网技术服务公司,拥有项目网站设计、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元洛阳做网站,已为上家服务,为洛阳各地企业和个人服务,联系电话:18980820575
1.使用idea创建springboot项目
在工程的pom.xml文件中引入相关的依赖,具体依赖如下所示
4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.1.release com.example activiti 0.0.1-snapshot activiti demo project for spring boot utf-8 utf-8 1.8 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.activiti activiti-spring-boot-starter 7.0.0.beta2 org.mybatis mybatis 3.4.5 MySQL mysql-connector-java 5.1.27 org.projectlombok lombok true org.springframework.boot spring-boot-maven-plugin
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) { 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(); } }
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()); } }
运行结果:
遇到的问题:
1)历史表默认不会生成,需在application.yml中配置
spring: activiti: #检测历史表是否存在 db-history-used: true #记录历史等级 可配置的历史级别有none, activity, audit, full history-level: full
到此,相信大家对“SpringBoot怎么整合Activiti7”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
新闻名称:SpringBoot怎么整合Activiti7
URL地址:http://ybzwz.com/article/ghesgd.html