Springboot集成flowable modeler web流程设计器

孤人 提交于 2020-08-16 23:48:14

之前画流程图都是用tomcat启动flowable modeler,但是这样启动就不能在分配任务用户/用户组的时候查询自己系统里的数据。所以现在需要把flowable modeler集成到项目里来。

之前自己也搜索了很多文章,都感觉不是很清晰,可能也是因为我刚接触不久。现在自己集成好了之后,记录一下自己学习的结果。

  1. 首先需要创建一个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>

     <dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-dependencies</artifactId>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
         <!--Spring Cloud-->
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-dependencies</artifactId>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
     </dependencyManagement>
    
     <dependencies>
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
     </dependency>
    
     <!-- 配置文件处理器 -->
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-configuration-processor</artifactId>
     </dependency>
     <!-- lombok -->
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <version>${lombok.version}</version>
         <scope>provided</scope>
     </dependency>
     <!-- fastjson -->
     <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>fastjson</artifactId>
         <version>${fastjson.version}</version>
     </dependency>
     <!-- Druid -->
     <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid</artifactId>
         <version>1.1.9</version>
     </dependency>
    
     <!-- 通用工具类 -->
    
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
    
     <!-- mysql 驱动 -->
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.47</version>
     </dependency>
     <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-pool2</artifactId>
         <version>2.6.1</version>
     </dependency>
     <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-lang3</artifactId>
         <version>3.8.1</version>
     </dependency>
    
     <!-- Flowable spring-boot 版套餐 -->
     <dependency>
         <groupId>org.flowable</groupId>
         <artifactId>flowable-spring-boot-starter</artifactId>
         <version>${flowable.version}</version>
     </dependency>
    
     <!-- Flowable 内部日志采用 SLF4J -->
     <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-api</artifactId>
         <version>1.7.21</version>
     </dependency>
     <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
         <version>1.7.21</version>
     </dependency>
    
     <!-- app 依赖 包含 rest,logic,conf -->
     <dependency>
         <groupId>org.flowable</groupId>
         <artifactId>flowable-ui-modeler-rest</artifactId>
         <version>6.4.1</version>
     </dependency>
     <dependency>
         <groupId>org.flowable</groupId>
         <artifactId>flowable-ui-modeler-logic</artifactId>
         <version>6.4.1</version>
     </dependency>
     <dependency>
         <groupId>org.flowable</groupId>
         <artifactId>flowable-ui-modeler-conf</artifactId>
         <version>6.4.1</version>
     </dependency>
    
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
         <exclusions>
             <exclusion>
                 <groupId>org.junit.vintage</groupId>
                 <artifactId>junit-vintage-engine</artifactId>
             </exclusion>
         </exclusions>
     </dependency>
    

    </dependencies>

  2. 然后需要将上一篇文章中说的在tomcat中启动的flowable modeler文件夹下的下面图片中的文件拷贝至springboot项目中resources文件夹下:

     
    4.png
     

    以下就是我的项目结构:

     
     
    5.png

    stencilset这个文件夹下是汉化文件。
  1. 下面是我们需要重写的三个文件: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),
    }
)
@EnableAsync
public class AppDispatcherServletConfiguration implements WebMvcRegistrations {

    private static final Logger LOGGER = LoggerFactory.getLogger(AppDispatcherServletConfiguration.class);

@Bean
public SessionLocaleResolver localeResolver() {
    return new SessionLocaleResolver();
}

@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
    LOGGER.debug("Configuring localeChangeInterceptor");
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("language");
    return localeChangeInterceptor;
}

@Override
public 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 = {
// 移除 RemoteIdmService
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = 
RemoteIdmService.class),
}
)
public class ApplicationConfiguration {

@Bean
public 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);

@Autowired
protected 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("admin@flowable.org");
    userRepresentation.setFullName("Administrator");
//        userRepresentation.setLastName("Administrator");
    userRepresentation.setFirstName("Administrator");
    List<String> privileges = 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("admin@flowable.com");
    user.setPassword("123456");
    List<String> pris = 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: 8087
spring:
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;
 @Autowired
protected 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 + "%";
        List<Group> groups = 
idmIdentityService.createNativeGroupQuery().sql(sql).parameter("name", filter).listPage(0, 10);
        List<GroupRepresentation> 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<User> matchingUsers = 
 idmIdentityService.createNativeUserQuery().sql(sql).
parameter("id",filter).parameter("name",filter).listPage(0, 10);List<UserRepresentation> 
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 
SUL
ON 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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!