之前画流程图都是用tomcat启动flowable modeler,但是这样启动就不能在分配任务用户/用户组的时候查询自己系统里的数据。所以现在需要把flowable modeler集成到项目里来。
之前自己也搜索了很多文章,都感觉不是很清晰,可能也是因为我刚接触不久。现在自己集成好了之后,记录一下自己学习的结果。
-
首先需要创建一个springboot应用,pom文件中引入相关jar包:
<properties>
<java.version>1.8</java.version>
<flowable.version>6.4.1</flowable.version>
<lombok.version>1.18.0</lombok.version>
<fastjson.version>1.2.9</fastjson.version>
</properties>
</dependencies>1 <dependencyManagement> 2 <dependencies> 3 <dependency> 4 <groupId>org.springframework.boot</groupId> 5 <artifactId>spring-boot-dependencies</artifactId> 6 <type>pom</type> 7 <scope>import</scope> 8 </dependency> 9 <!--Spring Cloud--> 10 <dependency> 11 <groupId>org.springframework.cloud</groupId> 12 <artifactId>spring-cloud-dependencies</artifactId> 13 <type>pom</type> 14 <scope>import</scope> 15 </dependency> 16 </dependencies> 17 </dependencyManagement> 18 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter</artifactId> 23 </dependency> 24 25 <!-- 配置文件处理器 --> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-configuration-processor</artifactId> 29 </dependency> 30 <!-- lombok --> 31 <dependency> 32 <groupId>org.projectlombok</groupId> 33 <artifactId>lombok</artifactId> 34 <version>${lombok.version}</version> 35 <scope>provided</scope> 36 </dependency> 37 <!-- fastjson --> 38 <dependency> 39 <groupId>com.alibaba</groupId> 40 <artifactId>fastjson</artifactId> 41 <version>${fastjson.version}</version> 42 </dependency> 43 <!-- Druid --> 44 <dependency> 45 <groupId>com.alibaba</groupId> 46 <artifactId>druid</artifactId> 47 <version>1.1.9</version> 48 </dependency> 49 50 <!-- 通用工具类 --> 51 52 <dependency> 53 <groupId>org.springframework.boot</groupId> 54 <artifactId>spring-boot-starter-web</artifactId> 55 </dependency> 56 57 <!-- mysql 驱动 --> 58 <dependency> 59 <groupId>mysql</groupId> 60 <artifactId>mysql-connector-java</artifactId> 61 <version>5.1.47</version> 62 </dependency> 63 <dependency> 64 <groupId>org.apache.commons</groupId> 65 <artifactId>commons-pool2</artifactId> 66 <version>2.6.1</version> 67 </dependency> 68 <dependency> 69 <groupId>org.apache.commons</groupId> 70 <artifactId>commons-lang3</artifactId> 71 <version>3.8.1</version> 72 </dependency> 73 74 <!-- Flowable spring-boot 版套餐 --> 75 <dependency> 76 <groupId>org.flowable</groupId> 77 <artifactId>flowable-spring-boot-starter</artifactId> 78 <version>${flowable.version}</version> 79 </dependency> 80 81 <!-- Flowable 内部日志采用 SLF4J --> 82 <dependency> 83 <groupId>org.slf4j</groupId> 84 <artifactId>slf4j-api</artifactId> 85 <version>1.7.21</version> 86 </dependency> 87 <dependency> 88 <groupId>org.slf4j</groupId> 89 <artifactId>slf4j-log4j12</artifactId> 90 <version>1.7.21</version> 91 </dependency> 92 93 <!-- app 依赖 包含 rest,logic,conf --> 94 <dependency> 95 <groupId>org.flowable</groupId> 96 <artifactId>flowable-ui-modeler-rest</artifactId> 97 <version>6.4.1</version> 98 </dependency> 99 <dependency> 100 <groupId>org.flowable</groupId> 101 <artifactId>flowable-ui-modeler-logic</artifactId> 102 <version>6.4.1</version> 103 </dependency> 104 <dependency> 105 <groupId>org.flowable</groupId> 106 <artifactId>flowable-ui-modeler-conf</artifactId> 107 <version>6.4.1</version> 108 </dependency> 109 110 <dependency> 111 <groupId>org.springframework.boot</groupId> 112 <artifactId>spring-boot-starter-test</artifactId> 113 <scope>test</scope> 114 <exclusions> 115 <exclusion> 116 <groupId>org.junit.vintage</groupId> 117 <artifactId>junit-vintage-engine</artifactId> 118 </exclusion> 119 </exclusions> 120 </dependency> -
然后需要将上一篇文章中说的在tomcat中启动的flowable modeler文件夹下的下面图片中的文件拷贝至springboot项目中resources文件夹下:
4.png
以下就是我的项目结构:
5.png
stencilset这个文件夹下是汉化文件。
-
下面是我们需要重写的三个文件:AppDispatcherServletConfiguration,ApplicationConfiguration,FlowableStencilSetResource这三个可以任意路径下
com.springcloud.flowable.conf.AppDispatcherServletConfiguration如下:
1import org.flowable.ui.modeler.rest.app.EditorGroupsResource;import org.flowable.ui.modeler.rest.app.EditorUsersResource;import org.flowable.ui.modeler.rest.app.StencilSetResource;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.scheduling.annotation.EnableAsync;import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;import org.springframework.web.servlet.i18n.SessionLocaleResolver;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;//www.1b23.com@Configuration@ComponentScan(value = { "org.flowable.ui.modeler.rest.app", 2 },excludeFilters = { 3 @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = EditorUsersResource.class), 4 @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = EditorGroupsResource.class), 5 @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = StencilSetResource.class), 6 })@EnableAsyncpublic class AppDispatcherServletConfiguration implements WebMvcRegistrations { 7 8 private static final Logger LOGGER = LoggerFactory.getLogger(AppDispatcherServletConfiguration.class);@Beanpublic SessionLocaleResolver localeResolver() { 9 return new SessionLocaleResolver();}@Beanpublic LocaleChangeInterceptor localeChangeInterceptor() { 10 LOGGER.debug("Configuring localeChangeInterceptor"); 11 LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); 12 localeChangeInterceptor.setParamName("language"); 13 return localeChangeInterceptor;}@Overridepublic RequestMappingHandlerMapping getRequestMappingHandlerMapping() { 14 LOGGER.debug("Creating requestMappingHandlerMapping"); 15 RequestMappingHandlerMapping requestMappingHandlerMapping = new RequestMappingHandlerMapping(); 16 requestMappingHandlerMapping.setUseSuffixPatternMatch(false); 17 requestMappingHandlerMapping.setRemoveSemicolonContent(false); 18 Object[] interceptors = { localeChangeInterceptor() }; 19 requestMappingHandlerMapping.setInterceptors(interceptors); 20 return requestMappingHandlerMapping;}
}
com.springcloud.flowable.conf.ApplicationConfiguration如下:
1 import org.flowable.ui.common.service.idm.RemoteIdmService; 2 import org.flowable.ui.modeler.properties.FlowableModelerAppProperties; 3 import org.flowable.ui.modeler.servlet.ApiDispatcherServletConfiguration; 4 import org.springframework.boot.context.properties.EnableConfigurationProperties; 5 import org.springframework.boot.web.servlet.ServletRegistrationBean; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.ComponentScan; 9 import org.springframework.context.annotation.Configuration; 10 import org.springframework.context.annotation.FilterType; 11 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 12 import org.springframework.web.servlet.DispatcherServlet;//www.1b23.com@Configuration@EnableConfigurationProperties(FlowableModelerAppProperties.class)@ComponentScan(basePackages = {// "org.flowable.ui.modeler.conf", 13 "org.flowable.ui.modeler.repository", 14 "org.flowable.ui.modeler.service",// "org.flowable.ui.modeler.security", //授权方面的都不需要// "org.flowable.ui.common.conf", // flowable 开发环境内置的数据库连接// "org.flowable.ui.common.filter", // IDM 方面的过滤器 15 "org.flowable.ui.common.service", 16 "org.flowable.ui.common.repository", 17 //// "org.flowable.ui.common.security",//授权方面的都不需要"org.flowable.ui.common.tenant" },excludeFilters = {// 移除 RemoteIdmService@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = RemoteIdmService.class),})public class ApplicationConfiguration {@Beanpublic ServletRegistrationBean modelerApiServlet(ApplicationContext applicationContext) { 18 AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext(); 19 dispatcherServletConfiguration.setParent(applicationContext); 20 dispatcherServletConfiguration.register(ApiDispatcherServletConfiguration.class); 21 DispatcherServlet servlet = new DispatcherServlet(dispatcherServletConfiguration); 22 ServletRegistrationBean registrationBean = new ServletRegistrationBean(servlet, "/api/*"); 23 registrationBean.setName("Flowable Modeler App API Servlet"); 24 registrationBean.setLoadOnStartup(1); 25 registrationBean.setAsyncSupported(true); 26 return registrationBean;}
}
com.springcloud.flowable.conf.FlowableStencilSetResource如下:
1import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import org.flowable.ui.common.service.exception.InternalServerErrorException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;//www.1b23.com@RestController@RequestMapping("/app")public class FlowableStencilSetResource {private static final Logger LOGGER = LoggerFactory.getLogger(FlowableStencilSetResource.class);@Autowiredprotected ObjectMapper objectMapper;@RequestMapping(value = "/rest/stencil-sets/editor", method = RequestMethod.GET, produces = "application/json")public JsonNode getStencilSetForEditor() {try { 2 JsonNode stencilNode 3objectMapper.readTree(this.getClass().getClassLoader().getResourceAsStream("stencilset/stencilse 4 t_bpmn.json")); 5 return stencilNode; 6 } catch (Exception e) { 7 LOGGER.error("Error reading bpmn stencil set json", e); 8 throw new InternalServerErrorException("Error reading bpmn stencil set json"); 9 } 10 } 11 12 @RequestMapping(value = "/rest/stencil-sets/cmmneditor", method = RequestMethod.GET, produces = "application/json") 13 public JsonNode getCmmnStencilSetForEditor() { 14 try { 15 JsonNode stencilNode = 16 objectMapper.readTree(this.getClass().getClassLoader().getResourceAsStream("stencilset/stencilse 17t_cmmn.json")); 18 return stencilNode; 19 } catch (Exception e) { 20 LOGGER.error("Error reading bpmn stencil set json", e); 21 throw new InternalServerErrorException("Error reading bpmn stencil set json"); 22 } 23 } 24 }
com.springcloud.flowable.controller.FlowableController:重写flowable modeler登陆的接口
7.png
1import org.flowable.ui.common.model.UserRepresentation;import org.flowable.ui.common.security.DefaultPrivileges;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;import java.util.List;/** 2 * Flowable 相关接口 3 * www.1b23.com 4 */@RestController@RequestMapping("/login")public class FlowableController { 5 6 /** 7 * 获取默认的管理员信息 8 * @return 9 */ 10 @RequestMapping(value = "/rest/account", method = RequestMethod.GET, produces = 11 "application/json") 12 public UserRepresentation getAccount() { 13 UserRepresentation userRepresentation = new UserRepresentation(); 14 userRepresentation.setId("admin"); 15 userRepresentation.setEmail("admin@flowable.org"); 16 userRepresentation.setFullName("Administrator");// userRepresentation.setLastName("Administrator"); 17 userRepresentation.setFirstName("Administrator"); 18 List<String> privileges = new ArrayList<>(); 19 privileges.add(DefaultPrivileges.ACCESS_MODELER); 20 privileges.add(DefaultPrivileges.ACCESS_IDM); 21 privileges.add(DefaultPrivileges.ACCESS_ADMIN); 22 privileges.add(DefaultPrivileges.ACCESS_TASK); 23 privileges.add(DefaultPrivileges.ACCESS_REST_API); 24 userRepresentation.setPrivileges(privileges); 25 return userRepresentation; 26 }}
添加流程时会用到用户id,这里重构SecurityUtils.getCurrentUserObject 获取用户信息,这个文件路径必须和原路径一致:
org.flowable.ui.common.security.SecurityUtils
1import org.flowable.idm.api.User;import org.flowable.ui.common.model.RemoteUser;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.context.SecurityContext;import org.springframework.security.core.context.SecurityContextHolder;import java.util.ArrayList;import java.util.List; 2 /** 3 * Utility class for Spring Security. 4* www.1b23.com */ 5 public class SecurityUtils { 6 7 private static User assumeUser; 8 9 private SecurityUtils() { 10 } 11 12 /** 13 * Get the login of the current user. 14 */ 15 public static String getCurrentUserId() { 16 User user = getCurrentUserObject(); 17 if (user != null) { 18 return user.getId(); 19 } 20 return null; 21 } 22 23 /** 24 * @return the {@link User} object associated with the current logged in user. 25 */ 26 public static User getCurrentUserObject() { 27 if (assumeUser != null) { 28 return assumeUser; 29 } 30 31 RemoteUser user = new RemoteUser(); 32 user.setId("admin"); 33 user.setDisplayName("Administrator"); 34 user.setFirstName("Administrator"); 35 user.setLastName("Administrator"); 36 user.setEmail("admin@flowable.com"); 37 user.setPassword("123456"); 38 List<String> pris = new ArrayList<>(); 39 pris.add(DefaultPrivileges.ACCESS_MODELER); 40 pris.add(DefaultPrivileges.ACCESS_IDM); 41 pris.add(DefaultPrivileges.ACCESS_ADMIN); 42 pris.add(DefaultPrivileges.ACCESS_TASK); 43 pris.add(DefaultPrivileges.ACCESS_REST_API); 44 user.setPrivileges(pris); 45 return user; 46 } 47 48 public static FlowableAppUser getCurrentFlowableAppUser() { 49 FlowableAppUser user = null; 50 SecurityContext securityContext = SecurityContextHolder.getContext(); 51 if (securityContext != null && securityContext.getAuthentication() != null) { 52 Object principal = securityContext.getAuthentication().getPrincipal(); 53 if (principal instanceof FlowableAppUser) { 54 user = (FlowableAppUser) principal; 55 } 56 } 57 return user; 58 } 59 60 public static boolean currentUserHasCapability(String capability) { 61 FlowableAppUser user = getCurrentFlowableAppUser(); 62 for (GrantedAuthority grantedAuthority : user.getAuthorities()) { 63 if (capability.equals(grantedAuthority.getAuthority())) { 64 return true; 65 } 66 } 67 return false; 68 } 69 70 public static void assumeUser(User user) { 71 assumeUser = user; 72 } 73 74 public static void clearAssumeUser() { 75 assumeUser = null; 76 }
}
配置文件:application.yml:
1server:port: 8087spring:datasource: 2 url: jdbc:mysql://127.0.0.1:3306/pyj?useUnicode=true&characterEncoding=utf8 username: root password: 123456 3 driverClassName: com.mysql.jdbc.Driver 4 resources: 5 mybatis: 6 mapper-locations: mapper/*/*.xml, classpath*:mapper/*.xml configuration: 7 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
启动类:SpringCloudApplication
1import com.springcloud.flowable.conf.AppDispatcherServletConfiguration;import com.springcloud.flowable.conf.ApplicationConfiguration;import org.flowable.ui.modeler.conf.DatabaseConfiguration;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Import;@Import(value={ 2 // 引入修改的配置 3 ApplicationConfiguration.class, 4 AppDispatcherServletConfiguration.class, 5 // 引入 DatabaseConfiguration 表更新转换 6 DatabaseConfiguration.class})// Eureka 客户端@ComponentScan(basePackages = {"com.springcloud.*"})@MapperScan("com.springcloud.*.dao")// 移除 Security 自动配置@SpringBootApplication(exclude={SecurityAutoConfiguration.class})public class SpringcloudApplication { 7 8 public static void main(String[] args) { 9 SpringApplication.run(SpringcloudApplication.class, args); 10 }}
这个配置很重要 @SpringBootApplication(exclude={SecurityAutoConfiguration.class}):排除springsecurity认证
接下来启动项目:输入:localhost:8087
8.png
最后重写获取用户,用户组接口
1import org.apache.commons.lang3.StringUtils;import org.flowable.engine.ManagementService;import org.flowable.idm.api.Group;import org.flowable.idm.api.IdmIdentityService;import org.flowable.idm.api.User;import org.flowable.ui.common.model.GroupRepresentation;import org.flowable.ui.common.model.ResultListDataRepresentation;import org.flowable.ui.common.model.UserRepresentation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;import java.util.List;//www.1b23.com@RestController@RequestMapping("app")public class UserAndGroupResourceController { 2 3 @Autowired 4 protected ManagementService managementService; 5 @Autowiredprotected IdmIdentityService idmIdentityService;@RequestMapping(value = "/rest/editor-groups", method = RequestMethod.GET)public ResultListDataRepresentation getGroups(@RequestParam(required = false, value = "filter") String filter) { 6 if (StringUtils.isNotBlank(filter)) { 7 filter = filter.trim(); 8 String sql = "select * from ACT_ID_GROUP where NAME_ like #{name}"; 9 filter = "%" + filter + "%"; 10 List<Group> groups = idmIdentityService.createNativeGroupQuery().sql(sql).parameter("name", filter).listPage(0, 10); 11 List<GroupRepresentation> result = new ArrayList<>(); 12 for (Group group : groups) { 13 result.add(new GroupRepresentation(group)); 14 } 15 return new ResultListDataRepresentation(result); 16 } 17 return null; 18 } 19 20 @RequestMapping(value = "/rest/editor-users", method = RequestMethod.GET) 21 public ResultListDataRepresentation getUsers(@RequestParam(value = "filter", required = false) String filter) { 22 if (StringUtils.isNotBlank(filter)) { 23 filter = filter.trim(); 24 String sql = "select * from ACT_ID_USER where ID_ like #{id} or LAST_ like #{name}"; 25 filter = "%"+filter+"%"; 26 List<User> matchingUsers = 27 idmIdentityService.createNativeUserQuery().sql(sql).parameter("id",filter).parameter("name",filter).listPage(0, 10);List<UserRepresentation> userRepresentations = new ArrayList<>(matchingUsers.size()); 28 for (User user : matchingUsers) { 29 userRepresentations.add(new UserRepresentation(user)); 30 } 31 return new ResultListDataRepresentation(userRepresentations); 32 } 33 return null; 34 }
}
将flowable启动时自建的表:ACT_ID_MEMBERSHIP,ACT _ID_GROUP, ACT_ID_USER 删除
自建视图查询自己系统中的用户/权限/角色表:
-- 用户表CREATE VIEW ACT_ID_USER AS SELECT SU.USER_ID AS ID_,1 AS REV_,SU.REAL_NM AS FIRST_,concat('',SU.USER_ID) AS LAST_ ,'000000' AS PWD_ , SUL.LOGIN_EMAIL AS EMAIL_, SU.WEB_FACE AS PICTURE_ID_FROM SYS_USER AS SU INNER JOIN sys_manager.SYS_USER_LOGIN AS SULON SU.USER_ID = SUL.USER_ID;-- 分组表CREATE VIEW ACT_ID_GROUP AS SELECT SR.ROLE_ID AS ID_ , 1 AS REV_, SR.ROLE_NM AS NAME_,CASE WHEN SR.IS_ADMIN=1 then 'admin' else 'ordinary' end as TYPE_ FROM SYS_ROLE AS SR;-- 关系表CREATE VIEW ACT_ID_MEMBERSHIP AS SELECT SUR.USER_ID AS USER_ID_,SUR.ROLE_ID AS GROUP_ID_ FROM SYS_USER_ROLE AS SUR;