当前位置:网站首页>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;
}
}
边栏推荐
- Flink parsing (V): state and state backend
- Hongmeng introduction and development environment construction
- Akamai anti confusion
- About selenium starting Chrome browser flash back
- 2022年大厂Android面试题汇总(二)(含答案)
- Optimization of middle alignment of loading style of device player in easycvr electronic map
- Solr appears write Lock, solrexception: could not get leader props in the log
- EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?
- Summary of study notes for 2022 soft exam information security engineer preparation
- Kali2021 installation and basic configuration
猜你喜欢
C# WinForm系列-Button简单使用
Spark calculation operator and some small details in liunx
Development and practice of lightweight planning service tools
华为认证云计算HICA
PySpark算子处理空间数据全解析(4): 先说说空间运算
EasyCVR授权到期页面无法登录,该如何解决?
03 products and promotion developed by individuals - plan service configurator v3.0
Basic configuration and use of spark
Distributed (consistency protocol) leader election (dotnext.net.cluster implements raft election)
【MySQL入门】第三话 · MySQL中常见的数据类型
随机推荐
[reverse] repair IAT and close ASLR after shelling
C version selenium operation chrome full screen mode display (F11)
Redis quick start
Application service configurator (regular, database backup, file backup, remote backup)
03个人研发的产品及推广-计划服务配置器V3.0
【Elastic】Elastic缺少xpack无法创建模板 unknown setting index.lifecycle.name index.lifecycle.rollover_alias
List set data removal (list.sublist.clear)
基于Infragistics.Document.Excel导出表格的类
How uipath determines that an object is null
[elastic] elastic lacks xpack and cannot create template unknown setting index lifecycle. name index. lifecycle. rollover_ alias
C WinForm series button easy to use
2022年大厂Android面试题汇总(一)(含答案)
华为认证云计算HICA
06 products and promotion developed by individuals - code statistical tools
Automatic operation and maintenance sharp weapon ansible Playbook
集成开发管理平台
[rapid environment construction] openharmony 10 minute tutorial (cub pie)
The art of Engineering (1): try to package things that do not need to be exposed
2021-03-22 "display login screen during recovery" can't be canceled. The appearance of lock screen interface leads to the solution that the remotely connected virtual machine can't work normally
[VNCTF 2022]ezmath wp