当前位置:网站首页>Wechat applet obtains mobile number
Wechat applet obtains mobile number
2022-07-06 17:41:00 【Flying wings】
1、 Main interface
// Applet AppID
private static final String appid = "wxa******e0b3";
// Applet AppSecret
private static final String secret = "8ffc**************bc0896e937d";
@PostMapping("/wxphone")
public Map<String,Object> wxphone(@RequestBody Image image)
{
Map<String,Object> map=new HashMap<>();
String encryptedData=image.getParams().get("encryptedData").toString();
String session_key="";
String iv=image.getParams().get("iv").toString();
String code=image.getParams().get("code").toString();
// obtain session_key, Perfect it Save to database
HttpUtil httpUtil=new HttpUtil();
String params = "appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
String session_keyData=HttpUtil.sendGet("https://api.weixin.qq.com/sns/jscode2session", params);
JSONObject json_session=JSONObject.parseObject(session_keyData);
session_key=json_session.get("session_key").toString();
// Get cell phone number
String result= WXBizDataCrypt.decrypt1(encryptedData,session_key,iv);
JSONObject json=JSONObject.parseObject(result);
if (!StringUtils.isEmpty(result)&&result.length()>0) {
map.put("purePhoneNumber", json.getString("purePhoneNumber"));
map.put("phoneNumber", json.getString("phoneNumber"));
map.put("countryCode", json.getString("countryCode"));
map.put("msg","success");
}
map.put("msg"," Press to operate successfully ");
map.put("code",200);
return map;
}
2. Tool class Request interface
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class HttpUtil {
/**
* Assign to URL send out GET Method request
*
* @param url Send requested URL
* @param param Request parameters , Request parameter should be name1=value1&name2=value2 In the form of .
* @return URL Response result of the remote resource represented
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// Open and URL Connection between
URLConnection connection = realUrl.openConnection();
// Set common request properties
connection.setRequestProperty("accept","*/*");
connection.setRequestProperty("connection","Keep-Alive");
connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// Establish the actual connection
connection.connect();
// Get all response header fields
Map<String, List<String>> map = connection.getHeaderFields();
// Traverse all response header fields
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// Definition BufferedReader Input stream to read URL Response
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println(" send out GET Exception in request !" + e);
e.printStackTrace();
}
// Use finally Block to close the input stream
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
3. For decryption
import org.apache.commons.codec.binary.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
public class WXBizDataCrypt {
/**
* AES Decrypt
*
* @param data // Ciphertext , Encrypted data
* @param key // Secret key
* @param iv // Offset
* @return
* @throws Exception
*/
public static String decrypt1(String data, String key,String iv){
// Encrypted data
byte[] dataByte = Base64.decodeBase64(data);
// Encryption key
byte[] keyByte = Base64.decodeBase64(key);
// Offset
byte[] ivByte = Base64.decodeBase64(iv);
try {
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivByte);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec keySpec = new SecretKeySpec(keyByte, "AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
return new String(cipher.doFinal(dataByte),"UTF-8");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}
边栏推荐
- [translation] principle analysis of X Window Manager (I)
- mysql高級(索引,視圖,存儲過程,函數,修改密碼)
- BearPi-HM_ Nano development environment
- C#WinForm中的dataGridView滚动条定位
- RepPoints:可形变卷积的进阶
- 04 products and promotion developed by individuals - data push tool
- How does wechat prevent withdrawal come true?
- MySQL error reporting solution
- 03个人研发的产品及推广-计划服务配置器V3.0
- 自动化运维利器-Ansible-Playbook
猜你喜欢
Spark accumulator and broadcast variables and beginners of sparksql
【Elastic】Elastic缺少xpack无法创建模板 unknown setting index.lifecycle.name index.lifecycle.rollover_alias
关于Selenium启动Chrome浏览器闪退问题
05个人研发的产品及推广-数据同步工具
Solr appears write Lock, solrexception: could not get leader props in the log
Re signal writeup
应用服务配置器(定时,数据库备份,文件备份,异地备份)
[reverse] repair IAT and close ASLR after shelling
Huawei certified cloud computing hica
Flink analysis (II): analysis of backpressure mechanism
随机推荐
MySQL basic addition, deletion, modification and query of SQL statements
C# WinForm中DataGridView单元格显示图片
【Elastic】Elastic缺少xpack无法创建模板 unknown setting index.lifecycle.name index.lifecycle.rollover_alias
EasyCVR电子地图中设备播放器loading样式的居中对齐优化
应用服务配置器(定时,数据库备份,文件备份,异地备份)
[elastic] elastic lacks xpack and cannot create template unknown setting index lifecycle. name index. lifecycle. rollover_ alias
The solution to the left-right sliding conflict caused by nesting Baidu MapView in the fragment of viewpager
华为认证云计算HICA
EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
Openharmony developer documentation open source project
MySQL stored procedure
[reverse] repair IAT and close ASLR after shelling
Case: check the empty field [annotation + reflection + custom exception]
集成开发管理平台
MySQL报错解决
Automatic operation and maintenance sharp weapon ansible Foundation
网络分层概念及基本知识
Final review of information and network security (full version)
Total / statistics function of MySQL
Kernel link script parsing