微信第三方平台代小程序实现业务(2)
今天任务:第三方平台方获取预授权码(pre_auth_code)
步骤1:第三方平台方获取预授权码(pre_auth_code)
获取预授权码pre_auth_code
接口调用请求说明
POST数据示例:
{
"component_appid":"appid_value"
}
请求参数说明
返回结果示例
{"pre_auth_code":"Cx_Dk6qiBE0Dmx4EmlT3oRfArPvwSQ-oa3NL_fwHM7VI08r52wazoZX2Rhpz1dEw","expires_in":600}
结果参数说明
此处给出我的实现方法
给出自己修改的两个解密操作
1、decryptMsg
public String decryptMsg(String msgSignature, String timeStamp, String nonce, String postData)
throws AesException {
// 密钥,公众账号的app secret
// 提取密文
Object[] encrypt = XMLParse.extract(postData);
// 验证安全签名
String signature = SHA1.getSHA1(token, timeStamp, nonce, encrypt[1].toString());
// 和URL中的签名比较是否相等
if (!signature.equals(msgSignature)) {
throw new AesException(AesException.ValidateSignatureError);
}
// 解密
String result = decrypt(encrypt[1].toString());
return result;
}
public static Object[] extract(String xmltext) throws AesException {
Object[] result = new Object[3];
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(xmltext);
InputSource is = new InputSource(sr);
Document document = db.parse(is);
Element root = document.getDocumentElement();
NodeList nodelist1 = root.getElementsByTagName("Encrypt");
// 修改后的代码
NodeList nodelist2 = root.getElementsByTagName("AppId");
result[0] = 0;
result[1] = nodelist1.item(0).getTextContent();
result[2] = nodelist2.item(0).getTextContent();
return result;
} catch (Exception e) {
e.printStackTrace();
throw new AesException(AesException.ParseXmlError);
}
}
2、decryptMsgTwo
注意:解析报异常java.security.InvalidKeyException:illegal Key Size的解决方案
在官方网站下载JCE无限制权限策略文件()()()
下载后解压,可以看到local_policy.jar和US_export_policy.jar以及readme.txt,如果安装了JRE,将两个jar文件放到%JRE_HOME%libsecurity目录下覆盖原来的文件;如果安装了JDK,将两个jar文件放到%JDK_HOME%jrelibsecurity目录下覆盖原来文件
掉坑1:
解析出来获取的component_verify_ticket是一串带有@@@的字符串,需要自行将@前面不要的信息截取掉,很多人在这里被坑的很惨。
通过以下这个接口获取component_access_token
POST数据示例:
{
"component_appid":"appid_value" ,
"component_appsecret": "appsecret_value",
"component_verify_ticket": "ticket_value"
}
