与第三方公司对接接口的权限校验

与第三方对接接口时的校验

调用方法时需要传参:

@ApiModelProperty("加密时间戳")
public String timestamp;

@ApiModelProperty("加密密文")
public String cipher;

调用方封装参数:

//被调用方法地址
private static final String url = "";

//DTO为参数对象 根据业务需求调整
Map<String, String> cipher = CipherUtil.getCipher();
DTO dto = DTO.builder()
    .cipher(cipher.get("cipher"))
    .timestamp(cipher.get("timestamp"))
    .build();
String jsonString = JSON.toJSONString(dto);
HttpClientUtils.doPostWithJson(url,jsonString);

被调用方校验

if (!CipherUtil.verifyCipher(dto)){
	throw new BusinessException( "验证失败!");
}

工具类:

public class CipherUtil {//32位小写加密

    //双方定义好密钥
    private static final String PRIVATE_KEY = "";

    /**
     * 获取时间戳和密文
     * @return
     */
    public static Map<String,String> getCipher(){
        try {
            String timestamp = Long.toString(System.currentTimeMillis());
            String cipher = getMD5(PRIVATE_KEY + "&" + timestamp);
            Map<String,String> map = new HashMap<>();
            map.put("timestamp", timestamp);
            map.put("cipher",cipher);
            return map;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 给请求对象添加时间戳和密文
     * @param t
     * @param <T>
     */
    public static <T> void addCipher(T t){
        try {
            String timestamp = Long.toString(System.currentTimeMillis());
            String cipher = getMD5(PRIVATE_KEY + "&" + timestamp);

            Class<?> clazz = t.getClass();
            clazz.getMethod("setTimestamp", String.class).invoke(t,timestamp);
            clazz.getMethod("setCipher", String.class).invoke(t,cipher);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 验证时间戳和密文是否正确
     * @param timestamp
     * @param cipher
     * @return
     */
    public static boolean verifyCipher(String timestamp, String cipher){
        try {
            String trueCipher = getMD5(PRIVATE_KEY + "&" + timestamp);
            if(cipher.equals(trueCipher)){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 验证请求对象时间戳和密文是否正确
     * @param t
     * @param <T>
     * @return
     */
    public static <T> boolean verifyCipher(T t){
        try {
            Class<?> clazz = t.getClass();
            String timestamp = clazz.getMethod("getTimestamp").invoke(t).toString();
            String cipher = clazz.getMethod("getCipher").invoke(t).toString();

            String trueCipher = getMD5(PRIVATE_KEY + "&" + timestamp);
            if(cipher.equals(trueCipher)){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    //获取md5方法
    private static String getMD5(String requestBody) {
        return encode("md5", requestBody);
    }
    private static String encode(String algorithm, String value) {
        if (value == null) {
            return null;
        }
        try {
            MessageDigest messageDigest
                    = MessageDigest.getInstance(algorithm);
            messageDigest.update(value.getBytes());
            return getFormattedText(messageDigest.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private static String getFormattedText(byte[] bytes) {
        int len = bytes.length;
        StringBuilder buf = new StringBuilder(len * 2);
        for (int j = 0; j < len; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }
        return buf.toString();
    }
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

}

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值