之前画流程图都是用tomcat启动flowable modeler,但是这样启动就不能在分配任务用户/用户组的时候查询自己系统里的数据。所以现在需要把flowable modeler集成到项目里来。
之前自己也搜索了很多文章,都感觉不是很清晰,可能也是因为我刚接触不久。现在自己集成好了之后,记录一下自己学习的结果。
首先需要创建一个springboot应用,pom文件中引入相关jar包:
1.8 6.4.1 1.18.0 1.2.9 org.springframework.boot spring-boot-dependencies pom import org.springframework.cloud spring-cloud-dependencies pom import org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-configuration-processor org.projectlombok lombok ${lombok.version} provided com.alibaba fastjson ${fastjson.version} com.alibaba druid 1.1.9 org.springframework.boot spring-boot-starter-web mysql mysql-connector-java 5.1.47 org.apache.commons commons-pool2 2.6.1 org.apache.commons commons-lang3 3.8.1 org.flowable flowable-spring-boot-starter ${flowable.version} org.slf4j slf4j-api 1.7.21 org.slf4j slf4j-log4j12 1.7.21 org.flowable flowable-ui-modeler-rest 6.4.1 org.flowable flowable-ui-modeler-logic 6.4.1 org.flowable flowable-ui-modeler-conf 6.4.1 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine 然后需要将上一篇文章中说的在tomcat中启动的flowable modeler文件夹下的下面图片中的文件拷贝至springboot项目中resources文件夹下:
4.png
以下就是我的项目结构:
5.png
stencilset这个文件夹下是汉化文件。
下面是我们需要重写的三个文件:AppDispatcherServletConfiguration,ApplicationConfiguration,FlowableStencilSetResource这三个可以任意路径下
com.springcloud.flowable.conf.AppDispatcherServletConfiguration如下:
import 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", },excludeFilters = { @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = EditorUsersResource.class), @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = EditorGroupsResource.class), @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = StencilSetResource.class), })@EnableAsyncpublic class AppDispatcherServletConfiguration implements WebMvcRegistrations { private static final Logger LOGGER = LoggerFactory.getLogger(AppDispatcherServletConfiguration.class);@Beanpublic SessionLocaleResolver localeResolver() { return new SessionLocaleResolver();}@Beanpublic LocaleChangeInterceptor localeChangeInterceptor() { LOGGER.debug("Configuring localeChangeInterceptor"); LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); localeChangeInterceptor.setParamName("language"); return localeChangeInterceptor;}@Overridepublic RequestMappingHandlerMapping getRequestMappingHandlerMapping() { LOGGER.debug("Creating requestMappingHandlerMapping"); RequestMappingHandlerMapping requestMappingHandlerMapping = new RequestMappingHandlerMapping(); requestMappingHandlerMapping.setUseSuffixPatternMatch(false); requestMappingHandlerMapping.setRemoveSemicolonContent(false); Object[] interceptors = { localeChangeInterceptor() }; requestMappingHandlerMapping.setInterceptors(interceptors); return requestMappingHandlerMapping;}
}
com.springcloud.flowable.conf.ApplicationConfiguration如下:
import org.flowable.ui.common.service.idm.RemoteIdmService; import org.flowable.ui.modeler.properties.FlowableModelerAppProperties; import org.flowable.ui.modeler.servlet.ApiDispatcherServletConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.ApplicationContext; 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.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet;//www.1b23.com@Configuration@EnableConfigurationProperties(FlowableModelerAppProperties.class)@ComponentScan(basePackages = {// "org.flowable.ui.modeler.conf", "org.flowable.ui.modeler.repository", "org.flowable.ui.modeler.service",// "org.flowable.ui.modeler.security", //授权方面的都不需要// "org.flowable.ui.common.conf", // flowable 开发环境内置的数据库连接// "org.flowable.ui.common.filter", // IDM 方面的过滤器 "org.flowable.ui.common.service", "org.flowable.ui.common.repository", //// "org.flowable.ui.common.security",//授权方面的都不需要"org.flowable.ui.common.tenant" },excludeFilters = {// 移除 [email protected](type = FilterType.ASSIGNABLE_TYPE, value = RemoteIdmService.class),})public class ApplicationConfiguration {@Beanpublic ServletRegistrationBean modelerApiServlet(ApplicationContext applicationContext) { AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext(); dispatcherServletConfiguration.setParent(applicationContext); dispatcherServletConfiguration.register(ApiDispatcherServletConfiguration.class); DispatcherServlet servlet = new DispatcherServlet(dispatcherServletConfiguration); ServletRegistrationBean registrationBean = new ServletRegistrationBean(servlet, "/api/*"); registrationBean.setName("Flowable Modeler App API Servlet"); registrationBean.setLoadOnStartup(1); registrationBean.setAsyncSupported(true); return registrationBean;}
}
com.springcloud.flowable.conf.FlowableStencilSetResource如下:
import 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 { JsonNode stencilNode objectMapper.readTree(this.getClass().getClassLoader().getResourceAsStream("stencilset/stencilse t_bpmn.json")); return stencilNode; } catch (Exception e) { LOGGER.error("Error reading bpmn stencil set json", e); throw new InternalServerErrorException("Error reading bpmn stencil set json"); } } @RequestMapping(value = "/rest/stencil-sets/cmmneditor", method = RequestMethod.GET, produces = "application/json") public JsonNode getCmmnStencilSetForEditor() { try { JsonNode stencilNode = objectMapper.readTree(this.getClass().getClassLoader().getResourceAsStream("stencilset/stencilse t_cmmn.json")); return stencilNode; } catch (Exception e) { LOGGER.error("Error reading bpmn stencil set json", e); throw new InternalServerErrorException("Error reading bpmn stencil set json"); } } }
com.springcloud.flowable.controller.FlowableController:重写flowable modeler登陆的接口
7.png
import 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;/** * Flowable 相关接口 * www.1b23.com */@RestController@RequestMapping("/login")public class FlowableController { /** * 获取默认的管理员信息 * @return */ @RequestMapping(value = "/rest/account", method = RequestMethod.GET, produces = "application/json") public UserRepresentation getAccount() { UserRepresentation userRepresentation = new UserRepresentation(); userRepresentation.setId("admin"); userRepresentation.setEmail("[email protected]"); userRepresentation.setFullName("Administrator");// userRepresentation.setLastName("Administrator"); userRepresentation.setFirstName("Administrator"); Listprivileges = new ArrayList<>(); privileges.add(DefaultPrivileges.ACCESS_MODELER); privileges.add(DefaultPrivileges.ACCESS_IDM); privileges.add(DefaultPrivileges.ACCESS_ADMIN); privileges.add(DefaultPrivileges.ACCESS_TASK); privileges.add(DefaultPrivileges.ACCESS_REST_API); userRepresentation.setPrivileges(privileges); return userRepresentation; }}
添加流程时会用到用户id,这里重构SecurityUtils.getCurrentUserObject 获取用户信息,这个文件路径必须和原路径一致:
org.flowable.ui.common.security.SecurityUtils
import 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; /** * Utility class for Spring Security. * www.1b23.com */ public class SecurityUtils { private static User assumeUser; private SecurityUtils() { } /** * Get the login of the current user. */ public static String getCurrentUserId() { User user = getCurrentUserObject(); if (user != null) { return user.getId(); } return null; } /** * @return the {@link User} object associated with the current logged in user. */ public static User getCurrentUserObject() { if (assumeUser != null) { return assumeUser; } RemoteUser user = new RemoteUser(); user.setId("admin"); user.setDisplayName("Administrator"); user.setFirstName("Administrator"); user.setLastName("Administrator"); user.setEmail("[email protected]"); user.setPassword("123456"); Listpris = new ArrayList<>(); pris.add(DefaultPrivileges.ACCESS_MODELER); pris.add(DefaultPrivileges.ACCESS_IDM); pris.add(DefaultPrivileges.ACCESS_ADMIN); pris.add(DefaultPrivileges.ACCESS_TASK); pris.add(DefaultPrivileges.ACCESS_REST_API); user.setPrivileges(pris); return user; } public static FlowableAppUser getCurrentFlowableAppUser() { FlowableAppUser user = null; SecurityContext securityContext = SecurityContextHolder.getContext(); if (securityContext != null && securityContext.getAuthentication() != null) { Object principal = securityContext.getAuthentication().getPrincipal(); if (principal instanceof FlowableAppUser) { user = (FlowableAppUser) principal; } } return user; } public static boolean currentUserHasCapability(String capability) { FlowableAppUser user = getCurrentFlowableAppUser(); for (GrantedAuthority grantedAuthority : user.getAuthorities()) { if (capability.equals(grantedAuthority.getAuthority())) { return true; } } return false; } public static void assumeUser(User user) { assumeUser = user; } public static void clearAssumeUser() { assumeUser = null; }
}
配置文件:application.yml:
server:port: 8087spring:datasource: url: jdbc:mysql://127.0.0.1:3306/pyj?useUnicode=true&characterEncoding=utf8 username: root password: 123456 driverClassName: com.mysql.jdbc.Driver resources: mybatis: mapper-locations: mapper/*/*.xml, classpath*:mapper/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
启动类:SpringCloudApplication
import 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={ // 引入修改的配置 ApplicationConfiguration.class, AppDispatcherServletConfiguration.class, // 引入 DatabaseConfiguration 表更新转换 DatabaseConfiguration.class})// Eureka 客户端@ComponentScan(basePackages = {"com.springcloud.*"})@MapperScan("com.springcloud.*.dao")// 移除 Security 自动配置@SpringBootApplication(exclude={SecurityAutoConfiguration.class})public class SpringcloudApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudApplication.class, args); }}
这个配置很重要 @SpringBootApplication(exclude={SecurityAutoConfiguration.class}):排除springsecurity认证
接下来启动项目:输入:localhost:8087
8.png
最后重写获取用户,用户组接口
import 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 { @Autowired protected ManagementService managementService; @Autowiredprotected IdmIdentityService idmIdentityService;@RequestMapping(value = "/rest/editor-groups", method = RequestMethod.GET)public ResultListDataRepresentation getGroups(@RequestParam(required = false, value = "filter") String filter) { if (StringUtils.isNotBlank(filter)) { filter = filter.trim(); String sql = "select * from ACT_ID_GROUP where NAME_ like #{name}"; filter = "%" + filter + "%"; Listgroups = idmIdentityService.createNativeGroupQuery().sql(sql).parameter("name", filter).listPage(0, 10); List result = new ArrayList<>(); for (Group group : groups) { result.add(new GroupRepresentation(group)); } return new ResultListDataRepresentation(result); } return null; } @RequestMapping(value = "/rest/editor-users", method = RequestMethod.GET) public ResultListDataRepresentation getUsers(@RequestParam(value = "filter", required = false) String filter) { if (StringUtils.isNotBlank(filter)) { filter = filter.trim(); String sql = "select * from ACT_ID_USER where ID_ like #{id} or LAST_ like #{name}"; filter = "%"+filter+"%"; List matchingUsers = idmIdentityService.createNativeUserQuery().sql(sql).parameter("id",filter).parameter("name",filter).listPage(0, 10);List userRepresentations = new ArrayList<>(matchingUsers.size()); for (User user : matchingUsers) { userRepresentations.add(new UserRepresentation(user)); } return new ResultListDataRepresentation(userRepresentations); } return null; }
}
将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;