SpringBoot + Activiti7 + Mybatis Plus +多数据源配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot + Activiti7 + Mybatis Plus +多数据源配置相关的知识,希望对你有一定的参考价值。

参考技术A 本文章借鉴了 https://www.jianshu.com/p/dfd5ae340011 ,但增加了无冲突依赖的版本,同时增加了Activiti的依赖。

activiti7与springboot2整合

接下来时间我会写一系列Activiti7的文章,包括Activiti7实战,Activiti7源码分析,Activiti7中遇到的坑等。那么首先我们先跑起来,将Activiti7Springboot2整合。Activiti7相比Activiti6一个显著的变化就是引入了SpringSecurity来负责鉴权工作,通俗点说就是接入的用户或者系统有没有相应的接口访问权限。

1. 引入依赖

<?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 http://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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.gd</groupId>
<artifactId>workflow</artifactId>
<version>1.0-SNAPSHOT</version>
<name>workflow</name>
<description>工作流引擎</description>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.41</version>
</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.1.0.M5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

2.添加配置文件

application.properties文件配置如下:

#数据源配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/workflow?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=workflow
spring.datasource.password=workflow
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

spring.datasource.druid.initial-size=1
spring.datasource.druid.max-active=20
spring.datasource.druid.min-idle=3
spring.datasource.druid.max-wait=60000
spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.filters=stat,wall,slf4j
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#activiti相关配置
spring.activiti.database-schema-update=true
spring.activiti.history-level=full
spring.activiti.db-history-used=true
spring.activiti.check-process-definitions=true

主要是数据源配置,中间有几点小细节:

  1. spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect这个属性配置主要是用来指定自动创建Activiti7表的时候采用INNODB引擎。

  2. 我这里采用的是Druid连接池,spring.datasource.type=com.alibaba.druid.pool.DruidDataSource这个属性用来表示我们采用Druid连接池。在配置连接池的时候比较简单,但是也有些细节要注意,否则可能会导致max-active,min-idle,max-wait等相关属性不生效,具体可以看我的另一篇文档《springboot配置druid连接池》。

  3. spring.activiti.database-schema-update用来配置每次启动应用时候数据库的更新策略,主要有四种,

    • flase:默认值。activiti在启动时,会检查数据表和版本,如果版本不匹配,将抛出异常。建议测试环境中设为false

    • true: Activiti7会对数据库中所有表进行更新操作。如果表不存在,则自动创建。常用在开发环境

    • create_drop:在Activiti7启动时创建表,在关闭时删除表。注意,只有手动关闭流程引擎的时候才会删除常用在单元测试

    • drop-create:在Activiti7启动时候会将原来的表删除,然后创建新的表。

  4. 此外我还添加了Druid监控配置。

3.SpringSecurity配置

新建SpringSecurity配置类如下:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private Logger logger = LoggerFactory.getLogger(SecurityConfig.class);

@Override
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(wfUserDetailsService());
}

@Bean
public UserDetailsService wfUserDetailsService() {

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"},
{"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;
}


@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/druid/**")
.permitAll()
.anyRequest()
.authenticated()
.and()
.httpBasic();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

}

主要是安全相关的配置,这里为了简单我直接将用户名密码Hard Coding在代码里面,如果要连接数据库,那需要重写UserDetailsService并注入AuthenticationManagerBuilder即可。既在上文类的configure(AuthenticationManagerBuilder auth)方法中auth.userDetailsService(yourUserDetailsService())

SecurityConfig中我们还可以配置其他安全相关的内容,比在这里我设置url为druid/**的不拦截。

今天我们的目标是先跑起来,下一篇我们看是怎么初始化Activiti7引擎的。


以上是关于SpringBoot + Activiti7 + Mybatis Plus +多数据源配置的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合工作流Activiti7

Activiti7工作流引擎:进阶篇 SpringBoot整合工作流Activiti7

activiti7+springboot2 禁用springsecurity

springboot+activiti7之拖拽表单kdesign整合

activiti7 + springboot2 (十五) JSP页面使用EL表达式引起的错误解决

springboot+Vue+Activiti7新增流程节点控制表单编辑/隐藏属性