当前位置:网站首页>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)
- 02 personal developed products and promotion - SMS platform
- [mmdetection] solves the installation problem
- Selenium test of automatic answer runs directly in the browser, just like real users.
- Integrated development management platform
- MySQL Advanced (index, view, stored procedures, functions, Change password)
- Precipitated database operation class - version C (SQL Server)
- Solr appears write Lock, solrexception: could not get leader props in the log
- Flink analysis (II): analysis of backpressure mechanism
- C# NanoFramework 点灯和按键 之 ESP32
猜你喜欢
![[translation] principle analysis of X Window Manager (I)](/img/40/6e15e1acebb47061d6e0e4c8ff82ea.jpg)
[translation] principle analysis of X Window Manager (I)

How does wechat prevent withdrawal come true?

EasyCVR授权到期页面无法登录,该如何解决?

The most complete tcpdump and Wireshark packet capturing practice in the whole network

EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?

EasyCVR平台通过接口编辑通道出现报错“ID不能为空”,是什么原因?

The problem of "syntax error" when uipath executes insert statement is solved

Grafana 9 is officially released, which is easier to use and more cool!

C# NanoFramework 点灯和按键 之 ESP32

C#版Selenium操作Chrome全屏模式显示(F11)
随机推荐
C WinForm series button easy to use
Xin'an Second Edition: Chapter 24 industrial control safety demand analysis and safety protection engineering learning notes
Sqoop I have everything you want
The solution that flutterweb browser cannot be rolled back after refreshing
The art of Engineering (3): do not rely on each other between functions of code robustness
The NTFS format converter (convert.exe) is missing from the current system
在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
EasyCVR电子地图中设备播放器loading样式的居中对齐优化
07 personal R & D products and promotion - human resources information management system
Start job: operation returned an invalid status code 'badrequst' or 'forbidden‘
Hongmeng introduction and development environment construction
自动化运维利器-Ansible-Playbook
Yarn: unable to load file d:\programfiles\nodejs\yarn PS1, because running scripts is prohibited on this system
OpenCV中如何使用滚动条动态调整参数
EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
Summary of study notes for 2022 soft exam information security engineer preparation
Debug xv6
Development and practice of lightweight planning service tools
Compile and build, from the bottom to the top
06个人研发的产品及推广-代码统计工具