1、选择一个开源项目

首先,在开始集成项目之前,选选择一个开源项目作为“底座”进行集成,由于作者对小诺的开源项目比较熟悉,所以选择xiaonuo的vue单体版本,作为底座进行集成:小诺开源技术

当然如果你对其他开源项目比较熟悉,也可以使用其他的开源项目,也是一样的效果。

2、下载camunda官方继承springboot案例

Download Open Source Workflow Software | Camunda

 这个链接是官方的一些资源的链接,如果你想直接跑一下camunda的项目,可以直接下下来看看,最重要的是modeler设计器,要下载下来安卓到您的个人电脑上

Download The Camunda BPMN / DMN Process Modeler | Camunda

Camunda Platform Initializr

 这个是camunda官方集成springboot的一个资源,可以下载下来,主要看一下它集成的pom包都有哪些。

3、camunda集成所需要的包

上面已经说了,关于camunda集成项目所需要的包都在官方集成的那个资源案例中,笔者在写这篇文章时案例为camunda的7.15版本。所以集成的过程包是7.15版本,如果后来更新了,那么就按照上面说的那个步骤找一下springboot集成所需要的最近的版本包就可以了。

<!--camunda的依赖包start-->
        <dependency>
            <groupId>org.camunda.bpm.springboot</groupId>
            <artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
            <version>7.15.0</version>
        </dependency>

        <dependency>
            <groupId>org.camunda.bpm.springboot</groupId>
            <artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
            <version>7.15.0</version>
        </dependency>

        <dependency>
            <groupId>org.camunda.bpm</groupId>
            <artifactId>camunda-engine-plugin-spin</artifactId>
        </dependency>

        <dependency>
            <groupId>org.camunda.spin</groupId>
            <artifactId>camunda-spin-dataformat-all</artifactId>
        </dependency>

        <dependency>
            <groupId>org.camunda.bpm.springboot</groupId>
            <artifactId>camunda-bpm-spring-boot-starter-test</artifactId>
            <version>7.15.0</version>
        </dependency>
        <!--camunda的依赖包end-->

这是7.15版本集成,所需要的包。

将上述的pom文件集成到项目中就可以了。

配置camunda平台超管账户

camunda.bpm.admin-user:
  id: admin
  password: admin

其实从某种意义上来说,camunda的基础集成已经完成了,启动后端项目,输入camunda的地址:http://localhost:82/camunda/app/welcome/default/#!/login

会发现无法进行访问,或者说访问异常,其实原因是因为你的项目被你自己的项目权限了拦截掉了,所以说,此时要对camunda项目和你自己的项目权限进行对接

4、xianuo权限放行camunda相关接口

Configure Authentication | docs.camunda.org

 首先根据camunda官方的文档,可以看出,camunda所有接口在不进行任何配置的情况下,默认是被放开的,也就是说,此时你访问:http://localhost:82/camunda/app/welcome/default/#!/login无法访问到,问题不是出现在camunda身上,而是你自己原来的系统上,其实不难想出答案,就是出现在权限上,所以这个时候,需要将你系统(xiaonuo)的权限关于camunda的接口全部放开

            // modeler 流程部署接口
            "/engine-rest/**",
            // modeler 表单部署
            "/forms/**",


            // camunda工作流(web)
            "/camunda/**",
            // "/**",

 此时你再启动项目就可以正常访问

5、camunda权限开启

有的小伙伴,肯定会想,原系统也就是xiaonuo将camunda的权限放开了,那camunda系统的权限默认又是关闭的,要是部署到生产环境,那工作流岂不是裸奔吗?确实是存在这样的问题,关于官方文档开启权限,那段也没有描述在springboot中如何实现camunda的权限开启,后来在camunda的官方论坛上找到了相关的代码,笔者做了一些改进:

package com.cn.xiaonuo.core.filter;

import org.camunda.bpm.engine.rest.security.auth.ProcessEngineAuthenticationFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 87761
 */
@Configuration
public class CamundaSecurityFilter {
    @Bean
    public FilterRegistrationBean<ProcessEngineAuthenticationFilter> processEngineAuthenticationFilter() {
        FilterRegistrationBean<ProcessEngineAuthenticationFilter> processEngineAuthenticationFilterRegistrationBean = new FilterRegistrationBean<>();
        processEngineAuthenticationFilterRegistrationBean.setName("camunda-auth");
        processEngineAuthenticationFilterRegistrationBean.setFilter(new ProcessEngineAuthenticationFilter());
        processEngineAuthenticationFilterRegistrationBean.addInitParameter("authentication-provider", "com.cn.xiaonuo.core.provider.CamundaAuthenticationProvider");
        processEngineAuthenticationFilterRegistrationBean.addUrlPatterns("/camunda/*","/engine-rest/*");
        return processEngineAuthenticationFilterRegistrationBean;
    }
}
package com.cn.xiaonuo.core.provider;

import com.cn.xiaonuo.core.context.login.LoginContextHolder;
import com.cn.xiaonuo.core.pojo.login.SysLoginUser;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.identity.User;
import org.camunda.bpm.engine.impl.digest._apacheCommonsCodec.Base64;
import org.camunda.bpm.engine.rest.security.auth.AuthenticationProvider;
import org.camunda.bpm.engine.rest.security.auth.AuthenticationResult;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.HttpHeaders;

/**
 * @author 87761
 */
public class CamundaAuthenticationProvider implements AuthenticationProvider {


    protected static final String BASIC_AUTH_HEADER_PREFIX = "Basic ";
    protected static final String TOKEN_TYPE_BEARER = "Bearer";

    @Override
    public AuthenticationResult extractAuthenticatedUser(HttpServletRequest request,
                                                         ProcessEngine engine) {
        String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);

        if (authorizationHeader != null) {

            if(authorizationHeader.startsWith(BASIC_AUTH_HEADER_PREFIX)){
                String encodedCredentials = authorizationHeader.substring(BASIC_AUTH_HEADER_PREFIX.length());
                String decodedCredentials = new String(Base64.decodeBase64(encodedCredentials));
                int firstColonIndex = decodedCredentials.indexOf(":");
                if (firstColonIndex != -1) {
                    String userName = decodedCredentials.substring(0, firstColonIndex);
                    String password = decodedCredentials.substring(firstColonIndex + 1);
                    if (isAuthenticated(engine, userName, password)) {
                        return AuthenticationResult.successful(userName);
                    }
                }
            }

            if(authorizationHeader.startsWith(TOKEN_TYPE_BEARER)){
                SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
                // AuthService authService = SpringUtil.getBean(AuthService.class);
                User user = engine.getIdentityService().createUserQuery().userId(sysLoginUser.getAccount()).singleResult();
                if(!StringUtils.isEmpty(user)){
                    return AuthenticationResult.successful(user.getId());
                }
            }
        }
        return AuthenticationResult.unsuccessful();
    }

    protected boolean isAuthenticated(ProcessEngine engine, String userName, String password) {
        return engine.getIdentityService().checkPassword(userName, password);
    }

    @Override
    public void augmentResponseByAuthenticationChallenge(
            HttpServletResponse response, ProcessEngine engine) {
        response.setHeader(HttpHeaders.WWW_AUTHENTICATE, BASIC_AUTH_HEADER_PREFIX + "realm=\"" + engine.getName() + "\"");
    }

}

开启springboot中camunda的权限如上,其实大概的意思就是xianuo那边放开的camunda的所有接口权限,那么camunda这边也要将这些权限给管理起来

对于小诺来说,系统原有的鉴权要稍微改一下

 原因是因为xiaonuo这边只要请求头中有Authorization他就拿去跑鉴权了,而camunda中的请求头中确实也有Authorization,只不过,开头不太一样,它是Basic开头的,而xiaonuo是以Bearer开头的,所以这个地方建议,判断的规则最好改成是否已Bearer开头,如果不是就直接返回null就好了。

6、鉴权开启效果

1、modeler设计器在部署的时候会要求你输入账户和密码

 2、输入网址后它会要求你输入账户和密码

 输入正确之后才会出现登录页面

Logo

前往低代码交流专区

更多推荐