activiti7与springboot2整合
Posted 攻城狮Bilbo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了activiti7与springboot2整合相关的知识,希望对你有一定的参考价值。
接下来时间我会写一系列Activiti7
的文章,包括Activiti7
实战,Activiti7
源码分析,Activiti7
中遇到的坑等。那么首先我们先跑起来,将Activiti7
与Springboot2
整合。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
主要是数据源配置,中间有几点小细节:
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
这个属性配置主要是用来指定自动创建Activiti7
表的时候采用INNODB
引擎。我这里采用的是
Druid
连接池,spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
这个属性用来表示我们采用Druid
连接池。在配置连接池的时候比较简单,但是也有些细节要注意,否则可能会导致max-active,min-idle,max-wait
等相关属性不生效,具体可以看我的另一篇文档《springboot配置druid连接池》。spring.activiti.database-schema-update
用来配置每次启动应用时候数据库的更新策略,主要有四种,flase:默认值。activiti在启动时,会检查数据表和版本,如果版本不匹配,将抛出异常。建议测试环境中设为false。
true:
Activiti7
会对数据库中所有表进行更新操作。如果表不存在,则自动创建。常用在开发环境。create_drop:在
Activiti7
启动时创建表,在关闭时删除表。注意,只有手动关闭流程引擎的时候才会删除。常用在单元测试。drop-create:在
Activiti7
启动时候会将原来的表删除,然后创建新的表。此外我还添加了
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
引擎的。
以上是关于activiti7与springboot2整合的主要内容,如果未能解决你的问题,请参考以下文章
activiti 7 + springboot2SpringSecurity 安全框架整合
activiti7+springboot2 禁用springsecurity
activiti7 + springboot2 (十五) JSP页面使用EL表达式引起的错误解决
springboot2+activiti 7 整合 用流程设计器设计一个流程