溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

SpringBoot怎么整合Activiti7

發(fā)布時(shí)間:2022-04-07 10:11:50 來(lái)源:億速云 閱讀:257 作者:iii 欄目:編程語(yǔ)言

本篇內(nèi)容主要講解“SpringBoot怎么整合Activiti7”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“SpringBoot怎么整合Activiti7”吧!

1.使用idea創(chuàng)建springboot項(xiàng)目

在工程的pom.xml文件中引入相關(guān)的依賴,具體依賴如下所示

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.2.1.release</version>
    <relativepath/> <!-- lookup parent from repository -->
  </parent>
  <groupid>com.example</groupid>
  <artifactid>activiti</artifactid>
  <version>0.0.1-snapshot</version>
  <name>activiti</name>
  <description>demo project for spring boot</description>

  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-jdbc</artifactid>
    </dependency>

    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>

    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupid>org.activiti</groupid>
      <artifactid>activiti-spring-boot-starter</artifactid>
      <version>7.0.0.beta2</version>
    </dependency>
    <dependency>
      <groupid>org.mybatis</groupid>
      <artifactid>mybatis</artifactid>
      <version>3.4.5</version>
    </dependency>
    <dependency>
      <groupid>mysql</groupid>
      <artifactid>mysql-connector-java</artifactid>
      <version>5.1.27</version>
    </dependency>
    <dependency>
      <groupid>org.projectlombok</groupid>
      <artifactid>lombok</artifactid>
      <optional>true</optional>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>

</project>

2.application.yml文件配置

為了能夠?qū)崿F(xiàn)activiti7生成的表放到mysql數(shù)據(jù)庫(kù)中,需要在配置文件application.yml中添加相關(guān)的配置

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: 默認(rèn)值。activiti在啟動(dòng)時(shí),會(huì)對(duì)比數(shù)據(jù)庫(kù)表中保存的版本,如果沒有表或者版本不匹配,將拋出異常
  #2.true: activiti會(huì)對(duì)數(shù)據(jù)庫(kù)中所有表進(jìn)行更新操作。如果表不存在,則自動(dòng)創(chuàng)建
  #3.create_drop: 在activiti啟動(dòng)時(shí)創(chuàng)建表,在關(guān)閉時(shí)刪除表(必須手動(dòng)關(guān)閉引擎,才能刪除表)
  #4.drop-create: 在activiti啟動(dòng)時(shí)刪除原來(lái)的舊表,然后在創(chuàng)建新表(不需要手動(dòng)關(guān)閉引擎)
  database-schema-update: true
  #檢測(cè)歷史表是否存在
  db-history-used: true
  #記錄歷史等級(jí) 可配置的歷史級(jí)別有none, activity, audit, full
  history-level: full
  #校驗(yàn)流程文件,默認(rèn)校驗(yàn)resources下的processes文件夾里的流程文件
  check-process-definitions: false

3.添加springsecurity安全框架整合配置

activiti7與springboot整合后,默認(rèn)情況下,集成了springsecurity安全框架,這樣我們就要去準(zhǔn)備springsecurity整合進(jìn)來(lái)的相關(guān)用戶權(quán)限配置信息

1)添加securityutil類

為了能夠快速實(shí)現(xiàn)springsecurity安全框架的配置,所添加的一個(gè)組件。

@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);
  }
}

這個(gè)類可以從activiti7官方提供的example中找到。

2)添加demoapplicationconfig類

它的作用是為了實(shí)現(xiàn)springsecurity框架的用戶權(quán)限的配置,這樣我們就可以在系統(tǒng)中使用用戶權(quán)限信息

@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) {
      list<string> authoritiesstrings = 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)創(chuàng)建測(cè)試類

@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("請(qǐng)假申請(qǐng)單流程")
        .deploy(); 
        
    log.info("流程部署id:" + deployment.getname());
    log.info("流程部署名稱:" + deployment.getid());
  }
}

運(yùn)行結(jié)果:

SpringBoot怎么整合Activiti7

SpringBoot怎么整合Activiti7

遇到的問(wèn)題:

1)歷史表默認(rèn)不會(huì)生成,需在application.yml中配置

spring:
 activiti:
  #檢測(cè)歷史表是否存在
  db-history-used: true
  #記錄歷史等級(jí) 可配置的歷史級(jí)別有none, activity, audit, full
  history-level: full

到此,相信大家對(duì)“SpringBoot怎么整合Activiti7”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI