Activiti7与Springboot的整合

本文详细介绍了如何在项目中集成Activiti 7.1.0.M6版本,同时排除Mybatis依赖,以避免冲突。内容包括:POM依赖排除Mybatis,YML配置,处理SpringSecurity权限问题,自定义UserDetailsService,模拟登录工具类,BPMN文件创建,以及Activiti服务工具类的封装。示例代码展示了具体的实现步骤。

 本文链接: https://www.bugyy.com/archives/57
  (文中图片若未正常显示,请移步以上原文链接继续阅读)
这里用的7.1.0.M6版本,如果项目中有使用mybatis-plus需要排除mybatis依赖
1. 引入依赖
这里用的7.1.0.M6版本,如果项目中有使用mybatis-plus需要排除mybatis依赖

        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis</artifactId>
                </exclusion>
            </exclusions>
            <version>${activiti.version}</version>
        </dependency>
2. 配置yml


另外,如果启动没有生成表,数据库链接需增加

nullCatalogMeansCurrent=true
否则在使用mybatis-generator生成表对应的xml等时会扫描整个服务器里面的全部数据库中的表,而不是扫描对应数据库的表

3. 相关配置类、服务类准备
1. 权限问题
默认情况下,集成了SpringSecurity安全框架,这样我们就要去准备SpringSecurity整合进来的相关用户权限配置信息

本案例项目中未使用SpringSecurity

实现SpringSecurity框架的用户权限的配置,重写userservice服务,从业务数据库获取用户信息
package com.xl.finance.module.activiti.service;

import cn.hutool.core.collection.CollUtil;
import com.xl.finance.module.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service("userDetailsServiceImpl")
@Slf4j
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return new User(username,"",CollUtil.toList(new SimpleGrantedAuthority("ROLE_ACTIVITI_USER")));
//        UserInfo userInfo = userService.getUserInfo(username);
//        if(Objects.isNull(userInfo)){
//            throw new ResultException("用户不存在:"+username);
//        }
//        List<String> roleListStr = new ArrayList<>();
//        roleListStr.add("ROLE_ACTIVITI_USER");
//        if(CollUtil.isNotEmpty(userInfo.getRoleList())){
//            roleListStr.addAll(userInfo.getRoleList().stream().map(e -> "GROUP_" + e.getId()).collect(Collectors.toList()));
//        }
////        return new User(userInfo.getUser().getUsername(), passwordEncoder().encode(userInfo.getUser().getPassword()),
////                roleListStr.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList()));
//        return new User(userInfo.getUser().getId().toString(),"",
//                roleListStr.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList()));
    }

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

添加工具类,用于设置登录用户
package com.xl.finance.module.activiti.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 {

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

    @Autowired
    @Qualifier("userDetailsServiceImpl")
    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);
    }
}

4. 创建bpmn文件
在resources目录下新增processes文件夹,将bpmn文件放在此处,可实现自动部署


设计费用申请的bpmn

备注: 这里的任务都分配给角色


5. 封装activiti服务工具类
工具类已包含启动、部署、完成、领取任务、待办查询等等功能
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值