1 SpringBoot integration Activiti7 configuration
In order to achieve SpringBoot and Activiti7 integration and development, we need to rely on the introduction of relevant support.
Therefore, we in the project pom.xml related to the introduction of the dependent files, specifically dependent on the following:
<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>mysql</groupId> <artifactId>mysql-connector-java</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> <!-- https://mvnrepository.com/artifact/org.activiti/activiti-spring-boot-starter --> <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>8.0.15 </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
By the pom.xml coordinate the imported file, we can achieve activiti7 with Springboot integration.
2 SpringBoot of application.yml file configuration
In order to enable Activiti7 table generated into Mysql database, it is necessary springboot profile application.yml
add relevant configuration
spring: datasource: url: jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT username : root password : 123456 driver-class-name: com.mysql.jdbc.Driver
3 Add SpringSecurity security framework to integrate configuration
Because Activiti7 and SpringBoot after integration, by default, integrated SpringSecurity security framework, so we're going to
prepare SpringSecurity related to the integration of user rights incoming configuration information.
You can look at the integration of SpringBoot dependencies, but also found that the SpringSecurity dependent package is also added to the project,
as follows:
3.1 Add SecurityUtil class
Add SecurityUtil class .
In order to be able to quickly implement configuration SpringSecurity security framework, added a component.
package comtopcheeractiviti; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextImpl; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.stereotype.Component; import java.util.Collection; @Component public class SecurityUtil { @Autowired 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"); } 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); } }
This class can be downloaded from our Activiti7 official of Example found in.
3.2 Add DemoApplicationConfig class
In Activiti7 official download Example found in DemoApplicationConfig class, its role is to achieve
SpringSecurity configure user rights framework, so that we can use the information in the system user rights. The project group
this is out of user-defined information in the file, of course, can be user permissions information in the database query.
/* * Copyright 2018 Alfresco, Inc. and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package comtopcheeractiviti; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.provisioning.InMemoryUserDetailsManager; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @Configuration @EnableWebSecurity public class DemoApplicationConfiguration extends WebSecurityConfigurerAdapter { private Logger logger = LoggerFactory.getLogger(DemoApplicationConfiguration.class); @Override @Autowired public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(myUserDetailsService()); } @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"}, {"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() .anyRequest() .authenticated() .and() .httpBasic(); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } }
4 Use SpringBoot integrated Junit way to test new features
@RunWith(SpringRunner.class) @SpringBootTest public class ActivitiApplicationTests { @Autowired private ProcessRuntime processRuntime; @Autowired private TaskRuntime taskRuntime; @Autowired private SecurityUtil securityUtil; // // / * // * See process definition // * / @Test public void contextLoads () { securityUtil.logInAs("salaboy"); Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10)); System.out.println ( "the number of available process definitions:" + processDefinitionPage.getTotalItems ()); for (the ProcessDefinition PD: processDefinitionPage.getContent ()) { System.out.println ( "Process Definition:" + PD); } } // // / * // * process instance is started // * / @Test public void testStartProcess () { securityUtil.logInAs("salaboy"); ProcessInstance pi = processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("myProcess_1") .build()); System.out.println ( "Process Instance ID:" + pi.getId ()); // process instance ID: 6d2a9bc1-0cee-11 ea-acf5-f894c25ebadd } // / ** * Query tasks and carry out their tasks */ @Test public void testTask() { securityUtil.logInAs ( "ryandawsonuk"); // specify the user authentication information Page <Task> taskPage = taskRuntime.tasks (Pageable.of (0,10)); // page query list IF (taskPage.getTotalItems ()> 0 ) { // illustrate the task, is traversed for (the task task: taskPage.getContent ()) { // pickup task taskRuntime.claim; (TaskPayloadBuilder.claim () withTaskId ( task.getId ()) build ()..) System.out.println("任务:"+task); //完成任务 taskRuntime.complete(TaskPayloadBuilder.complete().withTaskId(task.getId()).build()); } } Page<Task> taskPage2=taskRuntime.tasks(Pageable.of(0,10)); if (taskPage2.getTotalItems()>0){ System.out.println("任务:"+taskPage2.getContent()); } //任务:TaskImpl{id='6d310464-0cee-11ea-acf5-f894c25ebadd', owner='null', assignee='null', name='first', description='null', createdDate=Fri Nov 22 14:07:56 CST 2019, claimedDate=null, dueDate=null, priority=50, processDefinitionId='myProcess_1:1:7f72b399-0cec-11ea-8c61-f894c25ebadd', processInstanceId='6d2a9bc1-0cee-11ea-acf5-f894c25ebadd', parentTaskId='null', status=CREATED} //任务:[TaskImpl{id='b3cecc85-0cf0-11ea-89ae-f894c25ebadd', owner='null', assignee='null', name='second', description='null', createdDate=Fri Nov 22 14:24:13 CST 2019, claimedDate=null, dueDate=null, priority=50, processDefinitionId='myProcess_1:1:7f72b399-0cec-11ea-8c61-f894c25ebadd', processInstanceId='6d2a9bc1-0cee-11ea-acf5-f894c25ebadd', parentTaskId='null', status=CREATED}] } }
Activiti new API introduced
https://github.com/Activiti/activiti-7-developers-guide/blob/51a1681c0e4bb5e2f96a6dea73516c9fd53d8521/getting-started/getting-started-activiti-core.md