调用第三方接口demo

package com.gillion.worldex.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.otechsolution.out.entity.integration.Inventory;
import com.otechsolution.web.signer.util.AesEncryptUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.DigestUtils;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

@Slf4j
public class Demo {
    public static void main(String[] args) throws Exception {
        String appId = "";
        String appSecret = "";
        long timeMillis = System.currentTimeMillis();
        String nonce = RandomStringUtils.randomAlphanumeric(16);
        Inventory req = getInventory();


        String content = new ObjectMapper().writeValueAsString(Arrays.asList(req));
        log.info("请求对象:[{}]", content);
        String encryptContent = AesEncryptUtils.encrypt(content, appSecret, nonce);
        log.info("AES加密串:[{}]", encryptContent);


        // appId+nonce+timestamp+secret
        StringBuilder builder = new StringBuilder();
        builder.append("appId=").append(appId).append("&")
                .append("nonce=").append(nonce).append("&")
                .append("timestamp=").append(timeMillis).append("&")
                .append(appSecret);
        log.info("MD5源串:[{}]", builder);
        String sign = DigestUtils.md5DigestAsHex(builder.toString().getBytes(StandardCharsets.UTF_8));
        log.info("MD5目标串:[{}]", sign);

        HttpHeaders headers = new HttpHeaders();
        headers.add("appId", appId);
        headers.add("nonce", nonce);
        headers.add("timestamp", String.valueOf(timeMillis));
        headers.add("sign", sign);
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);

        log.info("request header:[{}]", headers);
        // 转义加密串中的特殊字符
        String encode = URLEncoder.encode(encryptContent, "utf-8");
        HttpEntity<String> httpEntity = new HttpEntity<>(encode, headers);

        RestTemplate restTemplate = new RestTemplate();

        String url = "";
        ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class);
        String body = response.getBody();
        String resNonce = response.getHeaders().get("nonce").get(0);
        byte[] decrypt = AesEncryptUtils.decrypt(body, appSecret, resNonce);
        log.info("接口响应:[{}]", new String(decrypt));
    }

    private static Inventory getInventory() {
        Inventory req = new Inventory();
        req.setLimitDate("2018-02-02 18:00:00");
        req.setStockDate("2018-02-02 18:00:00");
        req.setCiqCode("1");
        req.setContrItem(BigDecimal.ZERO);
        req.setCopGNo("2");
        req.setGModel("3");
        req.setGName("4");
        req.setGrossWt(BigDecimal.ZERO);
        req.setGUnit("5");
        req.setHsCode("6");
        req.setNetWt(BigDecimal.ZERO);
        req.setOriginCountryCode("9");
        req.setProductType(0);
        req.setQty(BigDecimal.ZERO);
        req.setQty1(BigDecimal.ZERO);
        req.setQty2(BigDecimal.ZERO);
        req.setStockType(0);
        req.setTradeCurr("10");
        req.setTradeTotal(BigDecimal.ZERO);
        req.setUnit1("11");
        req.setUnit2("12");
        req.setUnitPrice(BigDecimal.ZERO);
        req.setWhLoc("13");
        req.setWhLoc("14");
        return req;
    }
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------加密  解密 demo

package com.gillion.worldex.util;

import org.apache.commons.lang3.RandomStringUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.ws.spi.http.HttpHandler;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * 前后端数据传输加密工具类
 * 如果加密过程中抛出异常:java.security.InvalidKeyException: Illegal key size
 * 请按照https://blog.youkuaiyun.com/qq_41906281/article/details/109492814解决
 *
 * @author ericyuan
 */
public class AesEncryptUtils {


    //参数分别代表 算法名称/加密模式/数据填充方式
    private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding";
    //秘钥算法
    private static final String KEY_ALGORITHM = "AES";

    public static byte[] getBytes(String string) {
        return string.getBytes(StandardCharsets.UTF_8);
    }

    /**
     * 加密字符串
     *
     * @param data    待加密字符串
     * @param key     加密秘钥
     * @param ivParam iv参数秘钥,必须16字节长度
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key, String ivParam) throws Exception {
        byte[] keyBytes = getBytes(key);
        byte[] ivBytes = getBytes(ivParam);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
        IvParameterSpec iv = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv);
        byte[] dataBytes = getBytes(data);
        byte[] encrypted = cipher.doFinal(dataBytes);
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static byte[] decrypt(String sign, String key, String ivParam) throws Exception {
        return decrypt(getBytes(sign), key, ivParam);
    }

    /**
     * 解密字符串
     *
     * @param signBytes 待解密数据
     * @param key       解密key
     * @param ivParam   iv解密参数秘钥,必须16字节长度
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] signBytes, String key, String ivParam) throws Exception {
        byte[] keyBytes = getBytes(key);
        byte[] ivBytes = getBytes(ivParam);
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        IvParameterSpec iv = new IvParameterSpec(ivBytes);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv);
        byte[] encrypted1 = Base64.getMimeDecoder().decode(signBytes);
        return cipher.doFinal(encrypted1);
    }

    public static void main(String[] args) throws Exception {
        String key = "897D660077BA86D39081B984048E0D95";
        String ivParam = RandomStringUtils.randomAlphanumeric(16);
        String content = "{\"address\":\"上海市老街\",\"id\":1001,\"name\":\"张三\"}";
        System.out.println(content + "  长度为" + content.length());

        long lStart = System.currentTimeMillis();
        String enString = encrypt(content, key, ivParam);
        System.out.println("加密后的字串是:" + enString + "长度为" + enString.length());
        System.out.println("加密耗时:" + (System.currentTimeMillis() - lStart) + "毫秒");

        lStart = System.currentTimeMillis();
        String decrypt = new String(decrypt(enString, key, ivParam), "UTF-8");
        System.out.println("解密后的字串是:" + decrypt + "  长度为" + decrypt.length());
        System.out.println("解密耗时:" + (System.currentTimeMillis() - lStart) + "毫秒");
    }
}

 

 

 

 

 

泛微OA是一款广泛应用于企业办公的办公自动化软件,提供了丰富的功能和可定制的接口,可以方便地与其他系统进行集成。调用第三方接口是在泛微OA中实现与外部系统或服务进行数据交互的重要方式之一。 要实现泛微OA调用第三方接口Demo,首先需要确定所要调用第三方接口的具体内容和功能。一般而言,调用第三方接口需要提供接口的URL、请求的参数以及相应的请求方法(GET/POST)。 在泛微OA中,可以通过编写自定义的插件或者脚本来实现调用第三方接口。插件可以直接在OA系统中安装并使用,而脚本则可以通过定时任务或触发器来执行相应的操作。 具体实现步骤如下: 1. 在泛微OA的插件管理或脚本管理界面创建一个新插件或脚本。 2. 编写相应的逻辑代码,包括向第三方接口发送请求并获取响应数据的过程。根据第三方接口的要求,通过HTTP请求发送合适的参数,并解析接口返回的数据。 3. 调试和测试插件或脚本,确保能够正确地与第三方接口进行通信并获取到预期的结果。 4. 根据需求,可以在泛微OA的流程或表单中调用刚刚创建的插件或脚本。这样,当流程或表单的相关操作触发时,相应的第三方接口也会被调用。 需要注意的是,调用第三方接口可能需要进行认证或鉴权操作,这需要根据具体情况在代码中添加相应的授权方式,如使用API Key或OAuth等。 综上所述,泛微OA调用第三方接口Demo实现过程是:确定要调用第三方接口的功能,创建并编写相应的插件或脚本,并在泛微OA的流程或表单中调用该插件或脚本,最后进行调试测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值