签名校验失败 返回{"successful":false,"redirect":false}

阅读次数 15

签名校验失败 返回{"successful":false,"redirect":false}

请求抓包如下图: image.png

代码如下: private static String APPID = ""; private static String APPKEY = ""; private static String DOMAIN = "https://solution.wps.cn";

public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
	
    System.setProperty("http.proxyHost", "127.0.0.1");
    System.setProperty("http.proxyPort", "8866");

    System.setProperty("https.proxyHost", "127.0.0.1");
    System.setProperty("https.proxyPort", "8866");
	
	String url = "/api/developer/v1/office/pdf/convert/to/xlsx";
	SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
	//Wed, 23 Jan 2013 06:43:08 GMT
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    String formattedDate = dateFormat.format(new Date());
    
    
    System.out.println("====formattedDate==="+formattedDate);
	String sign = signature(url,formattedDate);
	
	System.out.println("====sign==="+sign);
	String authorization = getAuthorization(sign);
	
	System.out.println("====authorization==="+authorization);
	createTask( authorization, url, formattedDate);
	
}

public static void createTask(String authorization,String url,String formattedDate) throws IOException, NoSuchAlgorithmException {
	OkHttpClient client = new OkHttpClient.Builder()
            .sslSocketFactory(CustomSSLSocketFactory.getSSLSocketFactory(),CustomSSLSocketFactory.getX509TrustManager())
            .hostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            }).build();
	
	MediaType mediaType = MediaType.parse("application/json");
	String md5 = DigestUtils.md5Hex(url);
	System.out.println("====md5==="+md5);
	System.out.println("====request url==="+DOMAIN+url);
	RequestBody body = RequestBody.create(mediaType, "{\"url\":\"https://appdownload.wingtatusa.com/test.pdf\",\"text_unify\":true,\"export_type\":\"xlsx\"}");
	Request request = new Request.Builder()
		.url(DOMAIN+url)
		.post(body)
		.addHeader("Date", formattedDate)
		.addHeader("Content-Md5", md5)
		.addHeader("Content-Type", "application/json")
		.addHeader("Authorization", authorization)
		.build();

	Response response = client.newCall(request).execute();
	System.out.println(getObjectMapper().writeValueAsString(response));
}


private static String getAuthorization(String signature) {
	 String Authorization = String.format("WPS-2:%s:%s", APPID, signature);
     return Authorization;
}

private static String signature(String url,String formattedDate) throws NoSuchAlgorithmException, UnsupportedEncodingException {
    
	// 将字符串转换为字节数组
    byte[] appKeyBytes = APPKEY.getBytes(StandardCharsets.UTF_8);
    byte[] contentMd5Bytes = url.getBytes(StandardCharsets.UTF_8);
    byte[] contentTypeBytes = "application/json".getBytes(StandardCharsets.UTF_8);
    byte[] dateBytes = formattedDate.getBytes(StandardCharsets.UTF_8);

    // 合并字节数组
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(appKeyBytes, 0, appKeyBytes.length);
    outputStream.write(contentMd5Bytes, 0, contentMd5Bytes.length);
    outputStream.write(contentTypeBytes, 0, contentTypeBytes.length);
    outputStream.write(dateBytes, 0, dateBytes.length);
    byte[] concatenated = outputStream.toByteArray();
    
    	
	// 计算SHA-1哈希
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    byte[] hashBytes = digest.digest(concatenated);

    // 将字节数组转换为十六进制字符串
    StringBuilder hexString = new StringBuilder();
    for (byte b : hashBytes) {
        String hex = String.format("%02x", b);
        hexString.append(hex);
    }
    String signature = hexString.toString();
    return signature;
}

private static ObjectMapper getObjectMapper() {
	ObjectMapper mapper = new ObjectMapper();
	mapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
	mapper.setSerializationInclusion(Include.NON_NULL);
	SimpleModule module = new SimpleModule();
	mapper.registerModule(module);
	return mapper;
}
6 Answers

APPID:SX20250207JOSRCL request-id:47a5b4a621b440a795ac57de9e409f1b

返回打印结果如下: ====formattedDate===Mon, 10 Feb 2025 03:28:33 GMT ====sign===0c4c5dc36f0ad97cf78e47454a335dcd8cff350a ====authorization===WPS-2:SX20250207JOSRCL:0c4c5dc36f0ad97cf78e47454a335dcd8cff350a
====md5===65c802e6190ef6af5f56725d014a5996
====request url===https://solution.wps.cn/api/developer/v1/office/pdf/convert/to/xlsx
{"successful":false,"redirect":false}

您好,md5计算错误,建议参考文档https://solution.wps.cn/docs/convert/overview.html#%E7%AD%BE%E5%90%8D%E7%AE%97%E6%B3%95 排查一下post请求计算方法。err:code=30003 msg="InvalidSignature" detail="" hint=md5 not match. expect: d4d9cd344f3dc0a278c18760e7b5bf62, actual: 65c802e6190ef6af5f56725d014a5996

这个文档有看 没看明白 请问要怎么排查呢?

public static void main(String[] args) throws IOException { String appId = "xxxxxx"; String appKey = "xxxxxx"; String fileName = "商城原型评审-汇总版0224.pptx";
String fileUrl = "https://wpspublic.oss-cn-guangzhou.aliyuncs.com/oss_disk/1697180c04ac476ba73d234284c6f42a%E5%85%B7%E8%BA%AB%E8%9E%8D%E5%85%A520241011170449358.pptx";

    JSONObject jso = new JSONObject();
    jso.put("url", fileUrl);
    jso.put("filename", fileName);

    System.out.println("jso.toJSONString() = " + jso.toJSONString());
    String md5 = null;
    try {
        md5 = calculateMD5(jso.toJSONString());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    String date = dateRFC1123();

    String sha2 = appKey + md5 + "application/json" + date;
    //String sha3 = sha1(appKey + md5 + "application/json" + date);
    System.out.println("sha2 = " + sha2);

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("Date", date);
    headers.set("Content-Md5", md5);
    headers.set("Authorization", "WPS-2:" + appId + ":" + sha1(appKey + md5 + "application/json" + date));
    HttpEntity requestEntity = new HttpEntity<>(jso.toJSONString(), headers);
    ResponseEntity response = restTemplate.exchange("https://solution.wps.cn/api/developer/v1/office/convert/to/png", HttpMethod.POST, requestEntity, String.class);


    System.out.println(JSONObject.toJSONString(response.getBody()));

}





private static String sha1(String data) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-1");
        byte[] bytes = digest.digest(data.getBytes());
        StringBuilder builder = new StringBuilder();
        for (byte b : bytes) {
            builder.append(String.format("%02x", b));
        }
        return builder.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}


public static String dateRFC1123() {
    LocalDateTime now = LocalDateTime.now(ZoneId.of("UTC"));
    DateTimeFormatter rfc1123Formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
    String formattedTime = now.format(rfc1123Formatter);

    return formattedTime;
}




public static String calculateMD5(String input) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] hashInBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

    StringBuilder sb = new StringBuilder();
    for (byte b : hashInBytes) {
        sb.append(String.format("%02x", b));
    }
    return sb.toString();
}

}

请问您MD5,SHA1怎么计算的 方便看一下源代码么?

calculateMD5、sha1方法 能分享一下么?谢谢

package com.example.demo.oauth.util;

import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;


public class WpsV2ControllerRest {

//    @Resource
//    private RestTemplate restTemplate;


    public static void main(String[] args) throws IOException {
        String appId = "xxxxx";
        String appKey = "xxxxxx";
        String fileName = "商城原型评审-汇总版0224.pptx";
        String fileUrl = "https://wpspublic.oss-cn-guangzhou.aliyuncs.com/oss_disk/1697180c04ac476ba73d234284c6f42a%E5%85%B7%E8%BA%AB%E8%9E%8D%E5%85%A520241011170449358.pptx";

        JSONObject jso = new JSONObject();
        jso.put("url", fileUrl);
        jso.put("filename", fileName);

        System.out.println("jso.toJSONString() = " + jso.toJSONString());
        String md5 = null;
        try {
            md5 = calculateMD5(jso.toJSONString());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        String date = dateRFC1123();

        String sha2 = appKey + md5 + "application/json" + date;
        //String sha3 = sha1(appKey + md5 + "application/json" + date);
        System.out.println("sha2 = " + sha2);

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Date", date);
        headers.set("Content-Md5", md5);
        headers.set("Authorization", "WPS-2:" + appId + ":" + sha1(appKey + md5 + "application/json" + date));
        HttpEntity requestEntity = new HttpEntity<>(jso.toJSONString(), headers);
        ResponseEntity response = restTemplate.exchange("https://solution.wps.cn/api/developer/v1/office/convert/to/png", HttpMethod.POST, requestEntity, String.class);


        System.out.println(JSONObject.toJSONString(response.getBody()));

    }





    private static String sha1(String data) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            byte[] bytes = digest.digest(data.getBytes());
            StringBuilder builder = new StringBuilder();
            for (byte b : bytes) {
                builder.append(String.format("%02x", b));
            }
            return builder.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String dateRFC1123() {
        LocalDateTime now = LocalDateTime.now(ZoneId.of("UTC"));
        DateTimeFormatter rfc1123Formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
        String formattedTime = now.format(rfc1123Formatter);

        return formattedTime;
    }




    public static String calculateMD5(String input) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] hashInBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

        StringBuilder sb = new StringBuilder();
        for (byte b : hashInBytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

}

calculateMD5、sha1方法 能分享一下么?谢谢

public static String calculateMD5(String input) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] hashInBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));

    StringBuilder sb = new StringBuilder();
    for (byte b : hashInBytes) {
        sb.append(String.format("%02x", b));
    }
    return sb.toString();
}

以上问题已经解决,content_type=application/json;charset=utf-8 这个导致的

目前获取到转换后的TASKID后 没办法直接获取到下载文件

1739519139406.jpg

转换的文档截图如下

image.png

您好,转换的样张方便提供下嘛?

重新编辑了 设计敏感信息打码了

无法下载是因为转换后的链接,中有转义,\u0026,你将转换后的链接中的\u0026替换为&即可下载了。