SpringBoot2整合Activiti7步骤⼀:引⼊依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<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>
<!-- mvnrepository/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&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
步骤⼆:配置⽂件l
spring:
datasource:
url: jdbc:mysql://localhost:3306/activiti?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
username : root
password : 123
driver-class-name: sql.jdbc.Driver
activiti:
db-history-used: true
步骤三:将SpringSecurity配置添加到项⽬当中
1.SecurityUtil.java 关于权限登陆
@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() {
Authorities();
}
@Override
public Object getCredentials() {
Password();
}
@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() {
Username();
}
}));
ine.impl.identity.Authentication.setAuthenticatedUserId(username);
}
}
2.DemoApplicationConfiguration 关于权限配置
@Configuration
@EnableWebSecurity
public class DemoApplicationConfiguration extends WebSecurityConfigurerAdapter {
private Logger logger = Logger(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.pyOfRange(user, 2, user.length));
logger.info("> Registering new user: " + user[0] + " with the following Authorities[" + authoritiesStrings +
"]"); ateUser(new User(user[0], passwordEncoder().encode(user[1]),
authoritiesStrings.stream().map(s -> new SimpleGrantedAuthority(s)).List())));
}
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();
}
}
步骤四:启动⼯程
@SpringBootApplication
public class StartActiviti {
public static void main(String[] args) {
SpringApplication.run(StartActiviti.class,args);
}
}
⾃动部署:
注意问题:
1.Activiti7和SpringSecurity耦合,需要加⼊SpringSecurity的依赖和配置,我们可以使⽤Security中的⽤户⾓⾊组定义流程执⾏的组
2.流程默认可⾃动部署,但是需要再resources/processes⽂件夹,将流程⽂件放⼊当中
3.默认历史表不会⽣成,需要⼿动开启配置
执⾏流程:
spring framework runtimecontroller
@RestController
@RequestMapping("/activiti")
public class ActivitiController {
@Resource
private ProcessRuntime processRuntime;
@Resource
private TaskRuntime taskRuntime;
@Resource
private SecurityUtil securityUtil;
/
**
* 查询流程定义
*/
@RequestMapping("/getProcess")
public void getProcess(){
//查询所有流程定义信息
Page<ProcessDefinition> processDefinitionPage = processRuntime.processDefinitions(Pageable.of(0, 10));
System.out.println("当前流程定义的数量:"+TotalItems());
//获取流程信息
for (ProcessDefinition Content()) {
System.out.println("流程定义信息"+processDefinition);
}
}
/**
* 启动流程⽰例
*/
@RequestMapping("/startInstance")
public void startInstance(){
ProcessInstance instance = processRuntime.start(ProcessPayloadBuilder.start().withProcessDefinitionKey("demo").build());
System.out.Id());
}
/**
* 获取任务,拾取任务,并且执⾏
*/
@RequestMapping("/getTask")
public void getTask(){
securityUtil.logInAs("salaboy"); //指定组内任务⼈
Page<Task> tasks = taskRuntime.tasks(Pageable.of(0, 10));
TotalItems()>0){
for (Task Content()) {
System.out.println("任务名称:"+Name());
//拾取任务
taskRuntime.claim(TaskPayloadBuilder.claim().Id()).build());
/
/执⾏任务
taskRuntimeplete(TaskPayloadBuilderplete().Id()).build()); }
}
}
}
1.查询流程定义
需要先经过权限验证
账号密码在DemoApplicationConfiguration配置类
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"},
};
控制台打印信息:
2.启动流程⽰例
3.获取任务,拾取任务,并且执⾏
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论