Java集成E签宝实现签署

复制代码
完整代码:java-boot-highpin-background: 背调服务 (gitee.com)  【暂不开源】
1.在application.yml中配置appid、密钥信息,包含沙箱环境
    ```java
        esign:
            host: https://smlopenapi.esign.cn
            appId: your appId
            appSecret: your secret
    ```
2.实现电子签的主要流程在BaseAuthInfoServiceImpl里面
    1.根据模板生成word文件(word文件模板在resources里面)

    2.生成好的文件进行上传,上传分两步:具体实现看uploadMFile方法

    3.查询文件上传状态

    4.获取文件坐标

    5.创建签署流程,返回签署流程id
    
    6.最后返回页面签署路径url,返回给前端用于给用户访问签署的页面

3.代码实现:

java 复制代码
package io.renren.modules.zhaopin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import io.renren.common.esign.EsignFileBean;
import io.renren.common.esign.EsignHttpHelper;
import io.renren.common.esign.EsignHttpResponse;
import io.renren.common.esign.enums.EsignHeaderConstant;
import io.renren.common.esign.enums.EsignRequestType;
import io.renren.common.esign.exception.EsignDemoException;
import io.renren.common.utils.*;
import io.renren.modules.zhaopin.dao.BaseAuthInfoDao;
import io.renren.modules.zhaopin.entity.BackgroundCheckOrdersEntity;
import io.renren.modules.zhaopin.entity.BaseAuthInfoEntity;
import io.renren.modules.zhaopin.service.BackgroundCheckOrdersService;
import io.renren.modules.zhaopin.service.BaseAuthInfoService;
import io.renren.modules.zhaopin.util.HTTPHelper;
import io.renren.modules.zhaopin.util.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;

@Slf4j
@Service("baseAuthInfoService")
public class BaseAuthInfoServiceImpl extends ServiceImpl<BaseAuthInfoDao, BaseAuthInfoEntity> implements BaseAuthInfoService {

    @Value("${esign.host}")
    private String host;
    @Value("${esign.appId}")
    private String appId;
    @Value("${esign.appSecret}")
    private String appSecret;
    @Value("${esign.noticeUrl}")
    private String noticeUrl;
    @Autowired
    private BackgroundCheckOrdersService backgroundCheckOrdersService;

    @Autowired
    private BaseAuthInfoService baseAuthInfoService;
    @Autowired
    private IdWorker idWorker;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BaseAuthInfoEntity> page = this.page(
                new Query<BaseAuthInfoEntity>().getPage(params),
                new QueryWrapper<BaseAuthInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 生成word文件
     *
     * @param reportId 报告id
     */
    private void generateWordFile(String reportId) {
        LambdaQueryWrapper<BackgroundCheckOrdersEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BackgroundCheckOrdersEntity::getReportId, reportId);
        BackgroundCheckOrdersEntity checkOrders = backgroundCheckOrdersService.getOne(queryWrapper);
        try {
            File file = ResourceUtils.getFile("classpath:授权声明.docx");
            Map<String, Object> params = new HashMap<>();
            // 渲染文本
            params.put("company", checkOrders.getOrgName());
            params.put("id_card", checkOrders.getIdCard());
            params.put("date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日")));

            String path = file.getPath();
            String fileDir = path.substring(0, path.lastIndexOf("授"));

            String templatePath = file.getPath(); // "D:\\zdd.docx";
            String fileName = "最终版授权声明";

            String wordPath = GeneratorWordUtils.createWord(templatePath, fileDir, fileName, params);
            System.out.println("生成文档路径:" + wordPath);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 上传本地文件
     *
     * @return
     */
    private Map<String, Object> uploadMFile() {
        String contentType = "application/pdf";

        String postUrl = "/v3/files/file-upload-url";
        String postAllUrl = host + postUrl;
        try {
            File file = ResourceUtils.getFile("classpath:最终版授权声明.docx");
            String path = file.getPath();
            String contentMD5 = ESignUtils.getFileContentMD5(path);
            // 计算签名拼接的url
            // 构建请求Body体
            JSONObject reqBodyObj = new JSONObject();
            reqBodyObj.put("contentMd5", contentMD5);
            reqBodyObj.put("contentType", contentType);
            reqBodyObj.put("convertToPDF", true);
            reqBodyObj.put("fileName", "最终版授权声明.docx");
            reqBodyObj.put("fileSize", file.length() + "");

            Map<String, Object> resultMap = getStringObjectMapPost(reqBodyObj, postUrl, postAllUrl);
            Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
            String fileUploadUrl = (String) dataMap.get("fileUploadUrl");

            // 文件上传
            Gson gson = new Gson();
            EsignHttpResponse uploadFileResponse = ESignUtils.uploadFile(fileUploadUrl, path, contentMD5);
            JSONObject uploadFileResponseJsonObject = gson.fromJson(uploadFileResponse.getBody(), JSONObject.class);
            String code = uploadFileResponseJsonObject.get("errCode").toString();
            System.out.println("文件上传成功,状态码:" + code);

            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            String msg = MessageFormat.format("请求签名鉴权方式调用接口出现异常: {0}", e.getMessage());
            System.out.println(msg);
            throw new RuntimeException(e);
        }
    }



    private Map<String, Object> getStringObjectMapPost(JSONObject reqBodyObj, String postUrl, String postAllUrl) throws Exception {
        // 请求Body体数据
        String reqBodyData = reqBodyObj.toString();
        // 对请求Body体内的数据计算ContentMD5
        String contentMD5 = ESignUtils.getBodyContentMD5(reqBodyData);
        System.out.println("请求body数据:" + reqBodyData);
        System.out.println("body的md5值:" + contentMD5);

        // 构建待签名字符串
        String method = "POST";
        String accept = "*/*";
        String contentType = "application/json; charset=UTF-8";
        String date = "";
        String headers = "";

        StringBuffer sb = new StringBuffer();
        sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
                .append(contentType).append("\n").append(date).append("\n");
        if ("".equals(headers)) {
            sb.append(headers).append(postUrl);
        } else {
            sb.append(headers).append("\n").append(postUrl);
        }

        // 构建参与请求签名计算的明文
        String plaintext = sb.toString();
        // 计算请求签名值
        String reqSignature = ESignUtils.doSignatureBase64(plaintext, appSecret);
        System.out.println("计算请求签名值:" + reqSignature);

        // 获取时间戳(精确到毫秒)
        long timeStamp = DateUtils.timeStamp();

        // 构建请求头
        LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
        header.put("X-Tsign-Open-App-Id", appId);
        header.put("X-Tsign-Open-Auth-Mode", "Signature");
        header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
        header.put("Accept", accept);
        header.put("Content-Type", contentType);
        header.put("X-Tsign-Open-Ca-Signature", reqSignature);
        header.put("Content-MD5", contentMD5);
        System.out.println("header" + header);

        String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
        JSONObject resultObj = JSONObject.parseObject(result);
        // json转map
        Map<String, Object> resultMap = (Map<String, Object>) JSONObject.toJavaObject(resultObj, Map.class);
        System.out.println("请求返回信息: " + resultObj.toString());
        System.out.println("请求返回信息: " + resultMap.toString());
        return resultMap;
    }

    /**
     * 创建签署流程发送请求API
     *
     * @param reqBodyObj 请求体
     * @param postUrl    url
     * @param postAllUrl 全url
     * @return 返回数据
     * @throws Exception 异常处理
     */
    private Map<String, Object> getCreatePostSign(JSONObject reqBodyObj, String postUrl, String postAllUrl) throws Exception {
        // 请求Body体数据
        String reqBodyData = reqBodyObj.toString();
        // 对请求Body体内的数据计算ContentMD5
        String contentMD5 = ESignUtils.getBodyContentMD5(reqBodyData);
        System.out.println("请求body数据:" + reqBodyData);
        System.out.println("body的md5值:" + contentMD5);

        // 构建待签名字符串
        String method = "POST";
        String accept = "*/*";
        String contentType = "application/json;charset=UTF-8";
        String date = "";
        String headers = "";

        StringBuffer sb = new StringBuffer();
        sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
                .append(contentType).append("\n").append(date).append("\n");
        if ("".equals(headers)) {
            sb.append(headers).append(postUrl);
        } else {
            sb.append(headers).append("\n").append(postUrl);
        }

        // 构建参与请求签名计算的明文
        String plaintext = sb.toString();
        // 计算请求签名值
        String reqSignature = ESignUtils.doSignatureBase64(plaintext, appSecret);
        System.out.println("计算请求签名值:" + reqSignature);

        // 获取时间戳(精确到毫秒)
        long timeStamp = DateUtils.timeStamp();

        // 构建请求头
        LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
        header.put("X-Tsign-Open-App-Id", appId);
        header.put("X-Tsign-Open-Auth-Mode", "Signature");
        header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
        header.put("Accept", accept);
        header.put("Content-Type", contentType);
        header.put("X-Tsign-Open-Ca-Signature", reqSignature);
        header.put("Content-MD5", contentMD5);
        System.out.println("header" + header);

        String result = HTTPHelper.sendPOST(postAllUrl, reqBodyData, header, "UTF-8");
        JSONObject resultObj = JSONObject.parseObject(result);
        // json转map
        Map<String, Object> resultMap = (Map<String, Object>) JSONObject.toJavaObject(resultObj, Map.class);
        int code = (int) resultMap.get("code");
        if (code == 0) {
            Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
            // 签署流程id
            String signFlowId = (String) dataMap.get("signFlowId");
            resultMap.put("signFlowId", signFlowId);
        }
        System.out.println("请求返回信息: " + resultObj.toString());
        return resultMap;
    }

    /**
     * 签署流程id 获取签署页面链接
     *
     * @param signFlowId 签署流程id
     */
    private String getSignUrl(String signFlowId) {
        String postAllUrl = host + "/v3/sign-flow/" + signFlowId + "/sign-url";
        String postUrl = "/v3/sign-flow/" + signFlowId + "/sign-url";

        Map<String, Object> operatorMap = new HashMap<>();
        operatorMap.put("psnAccount", "16619880853");
        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("clientType", "ALL");
        reqBodyObj.put("needLogin", false);
        reqBodyObj.put("urlType", 2);
        reqBodyObj.put("operator", operatorMap);
        Map<String, Object> redirectConfigMap = new HashMap<>();
        redirectConfigMap.put("redirectUrl", noticeUrl);
        reqBodyObj.put("redirectConfig", redirectConfigMap);
        System.out.println("reqBodyObj" + reqBodyObj);
        try {
            Map<String, Object> map = getStringObjectMapPost(reqBodyObj, postUrl, postAllUrl);
            System.out.println(map);
            int code = (int) map.get("code");
            if (code == 0) {
                Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
                String shortUrl = (String) dataMap.get("shortUrl");
                return shortUrl;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 查询文件上传状态
     *
     * @param fileId 报告id
     * @return
     */
    private Integer getFileStatus(String fileId) throws Exception {
        String getAllUrl = host + "/v3/files/" + fileId;
        String postUrl = "/v3/files/" + fileId;
        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("fileId", fileId);

        // GET请求时ContentMD5为""
        String contentMD5 = "";

        // 构建待签名字符串
        String method = "GET";
        String accept = "*/*";
        String contentType = "application/json; charset=UTF-8";
        String date = "";
        String headers = "";

        StringBuffer sb = new StringBuffer();
        sb.append(method).append("\n").append(accept).append("\n").append(contentMD5).append("\n")
                .append(contentType).append("\n").append(date).append("\n");
        if ("".equals(headers)) {
            sb.append(headers).append(postUrl);
        } else {
            sb.append(headers).append("\n").append(postUrl);
        }

        // 构建参与请求签名计算的明文
        String plaintext = sb.toString();
        // 计算请求签名值
        String reqSignature = ESignUtils.doSignatureBase64(plaintext, appSecret);
        System.out.println("计算请求签名值:" + reqSignature);
        // 获取时间戳(精确到毫秒)
        long timeStamp = DateUtils.timeStamp();
        // 构建请求头
        LinkedHashMap<String, String> header = new LinkedHashMap<String, String>();
        header.put("X-Tsign-Open-App-Id", appId);
        header.put("X-Tsign-Open-Auth-Mode", "Signature");
        header.put("X-Tsign-Open-Ca-Timestamp", String.valueOf(timeStamp));
        header.put("Accept", accept);
        header.put("Content-Type", contentType);
        header.put("X-Tsign-Open-Ca-Signature", reqSignature);
        header.put("Content-MD5", contentMD5);

        HashMap<String, Object> query = new HashMap<String, Object>();
        // 发送POST请求
        String result = HTTPHelper.sendGet(getAllUrl, query, header, "UTF-8");
        JSONObject resultObj = JSONObject.parseObject(result);
        System.out.println("请求返回信息: " + resultObj.toString());
        Map<String, Object> resultMap = (Map<String, Object>) JSONObject.toJavaObject(resultObj, Map.class);
        System.out.println(resultMap);
        return (Integer) resultMap.get("code");
    }

    /**
     * 获取文件坐标
     */
    private Map<String, Object> getPosition(String fileId) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        String postUrl = "/v3/files/" + fileId + "/keyword-positions";
        String postAllUrl = host + postUrl;

        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("fileId", fileId);
        List<String> keywords = new ArrayList<>();
        keywords.add("本人签名");
        reqBodyObj.put("keywords", keywords);
        try {
            Map<String, Object> map = getStringObjectMapPost(reqBodyObj, postUrl, postAllUrl);
            System.out.println(map);
            Map<String, Object> dataMap = (Map<String, Object>) map.get("data");
            if (dataMap == null) {
                throw new RuntimeException("解析失败");
            }
            List keywordPositions = (List) dataMap.get("keywordPositions");
            Map<String, Object> objectMap = (Map<String, Object>) keywordPositions.get(0);
            List positions = (List) objectMap.get("positions");
            Map<String, Object> positionMap = (Map<String, Object>) positions.get(0);
            List coordinates = (List) positionMap.get("coordinates");
            Integer pageNums = (Integer) positionMap.get("pageNum");
            Map<String, Object> position = (Map<String, Object>) coordinates.get(0);
            position.put("pageNums", pageNums);
            return position;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 签署
     *
     * @param reportId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R sign(String reportId) {
        try {
            String id = idWorker.nextId() + "";
            generateWordFile(reportId);
            Map<String, Object> resultMap = uploadMFile();
            Map<String, Object> dataMap = (Map<String, Object>) resultMap.get("data");
            String fileId = (String) dataMap.get("fileId");
            String fileUploadUrl = (String) dataMap.get("fileUploadUrl");
            System.out.println(resultMap);
            /*
              1. 查看所上传文件的当前状态(转换pdf/html文件状态)文件名称和下载链接。
              2. 当返回的文件状态status值为 2 或 5 时,此文件才可以被应用到签署流程中。
             */
            Integer fileStatus = getFileStatus(fileId);
            if (fileStatus == 0) {
                Map<String, Object> position = getPosition(fileId);
                BigDecimal positionY = (BigDecimal) position.get("positionY");
                BigDecimal positionX = (BigDecimal) position.get("positionX");
                Integer pageNums = (Integer) position.get("pageNums");
                Map<String, Object> sign = createSign(fileId, reportId, positionX, positionY, pageNums);
                String signUrl = (String) sign.get("signUrl");
                String signFlowId = (String) sign.get("signFlowId");
                // 保存到数据库 fileUploadId
                BaseAuthInfoEntity baseAuthInfoEntity = new BaseAuthInfoEntity();
                baseAuthInfoEntity.setId(id);
                baseAuthInfoEntity.setOrderId(reportId);
                baseAuthInfoEntity.setAuthFlowId(signFlowId);
                baseAuthInfoEntity.setAuthUrl(signUrl);
                baseAuthInfoEntity.setCreateTime(LocalDateTime.now());
                baseAuthInfoEntity.setUpdateTime(LocalDateTime.now());
                baseAuthInfoEntity.setFileId(fileId);
                baseAuthInfoEntity.setFileUploadUrl(fileUploadUrl);
                baseAuthInfoService.save(baseAuthInfoEntity);
                return R.ok(sign);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return R.ok("签署流程失败");
    }

    /**
     * 创建签署流程
     */
    public Map<String, Object> createSign(String fileId, String reportId, BigDecimal positionX, BigDecimal positionY, Integer pageNums) {
        LambdaQueryWrapper<BackgroundCheckOrdersEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BackgroundCheckOrdersEntity::getReportId, reportId);
        BackgroundCheckOrdersEntity checkOrders = backgroundCheckOrdersService.getOne(queryWrapper);

        Map<String, Object> paramsMap = new HashMap<>();

        Map<String, Object> docsMap = new HashMap<>();
        // 待签署文件ID
        docsMap.put("fileName", "最终版授权声明.docx");
        docsMap.put("fileId", fileId);

        Map<String, Object> signFlowConfigMap = new HashMap<>();
        signFlowConfigMap.put("signFlowTitle", "企业合同签署");
        signFlowConfigMap.put("notifyUrl", "www.baidu.com");
        Map<String, Object> redirectConfigMap = new HashMap<>();
        redirectConfigMap.put("redirectUrl", "www.baidu.com");
        signFlowConfigMap.put("redirectConfig", redirectConfigMap);
        signFlowConfigMap.put("autoFinish", true);

        Map<String, Object> signersMap = new HashMap<>();
        Map<String, Object> signConfigMap = new HashMap<>();
        signConfigMap.put("forcedReadingTime", 5);
        /*
            签署方类型,0 - 个人,1 - 企业/机构,2 - 法定代表人,3 - 经办人
            若指定签署方为个人,则psnSignerInfo为必传项;
            若指定签署方为机构或法定代表人手动签署(autoSign参数为false)时,则orgSignerInfo为必传项;
            若指定签署方为经办人,在同级数组内必须还有机构类型存在,且orgSignerInfo为必传项,即:指定3 - 经办人签的前提是必须同时存在1 - 企业/机构,且经办人签属于企业合同,不在个人名下。
         */
        signersMap.put("signerType", 0);
        signersMap.put("signConfig", signConfigMap);

        Map<String, Object> psnSignerInfoMap = new HashMap<>();
        // 企业/机构名称(账号标识)
        psnSignerInfoMap.put("psnAccount", checkOrders.getHrTel());
        signersMap.put("psnSignerInfo", psnSignerInfoMap);

        List<Object> docsList = new ArrayList<>();
        docsList.add(docsMap);
        paramsMap.put("docs", docsList);
        paramsMap.put("signFlowConfig", signFlowConfigMap);
        List<Object> signersList = new ArrayList<>();
        signersList.add(signersMap);
        // signersList.add(psnSignerInfoMap);
        // paramsMap.put("signers", signersList);

        List<Object> signFieldsList = new ArrayList<>();

        Map<String, Object> signFieldsMap = new HashMap<>();
        signFieldsMap.put("fileId", fileId);
        signFieldsMap.put("customBizNum", idWorker.nextId() + "");

        Map<String, Object> normalSignFieldConfigMap = new HashMap<>();
        // 1 - 单页签章,2 - 骑缝签章
        normalSignFieldConfigMap.put("signFieldStyle", 1);
        normalSignFieldConfigMap.put("freeMode", false);
        normalSignFieldConfigMap.put("autoSign", false);
        Map<String, Object> signFieldPositionMap = new HashMap<>();
        signFieldPositionMap.put("positionX", positionX);
        signFieldPositionMap.put("positionY", positionY);
        signFieldPositionMap.put("positionPage", pageNums);

        signFieldsMap.put("normalSignFieldConfig", normalSignFieldConfigMap);
        normalSignFieldConfigMap.put("signFieldPosition", signFieldPositionMap);

        signFieldsList.add(signFieldsMap);
        signersMap.put("signFields", signFieldsList);

        String postAllUrl = host + "/v3/sign-flow/create-by-file";
        String postUrl = "/v3/sign-flow/create-by-file";
        JSONObject reqBodyObj = new JSONObject();
        reqBodyObj.put("docs", docsList);
        reqBodyObj.put("signFlowConfig", signFlowConfigMap);
        reqBodyObj.put("signers", signersList);
        System.out.println("reqBodyObj:" + reqBodyObj);
        System.out.println("paramsMap:" + paramsMap);
        try {
            Map<String, Object> map = getCreatePostSign(reqBodyObj, postUrl, postAllUrl);
            String signFlowId = (String) map.get("signFlowId");
            // 签署流程id 获取签署页面链接
            String signUrl = getSignUrl(signFlowId);
            map.put("signUrl", signUrl);
            map.put("signFlowId", signFlowId);
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

工具类代码比较多,就不全部写出来了

相关推荐
Cachel wood3 分钟前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Code哈哈笑6 分钟前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
gb42152879 分钟前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
程序猿进阶9 分钟前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
qq_4336184411 分钟前
shell 编程(二)
开发语言·bash·shell
zfoo-framework17 分钟前
【jenkins插件】
java
风_流沙22 分钟前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch
charlie11451419126 分钟前
C++ STL CookBook
开发语言·c++·stl·c++20
袁袁袁袁满26 分钟前
100天精通Python(爬虫篇)——第113天:‌爬虫基础模块之urllib详细教程大全
开发语言·爬虫·python·网络爬虫·爬虫实战·urllib·urllib模块教程