当前位置:网站首页>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;
}
}
边栏推荐
- [getting started with MySQL] fourth, explore operators in MySQL with Kiko
- EasyCVR接入设备开启音频后,视频无法正常播放是什么原因?
- Wordcloud colormap color set and custom colors
- Basic configuration and use of spark
- Binary search strategy
- 在一台服务器上部署多个EasyCVR出现报错“Press any to exit”,如何解决?
- 轻量级计划服务工具研发与实践
- MySQL stored procedure
- Kali2021 installation and basic configuration
- Yarn: unable to load file d:\programfiles\nodejs\yarn PS1, because running scripts is prohibited on this system
猜你喜欢
Vscode matches and replaces the brackets
EasyCVR电子地图中设备播放器loading样式的居中对齐优化
【ASM】字节码操作 ClassWriter 类介绍与使用
How does wechat prevent withdrawal come true?
02 personal developed products and promotion - SMS platform
EasyRE WriteUp
Pyspark operator processing spatial data full parsing (4): let's talk about spatial operations first
FlutterWeb瀏覽器刷新後無法回退的解决方案
Huawei certified cloud computing hica
Development and practice of lightweight planning service tools
随机推荐
Vscode replaces commas, or specific characters with newlines
当前系统缺少NTFS格式转换器(convert.exe)
04个人研发的产品及推广-数据推送工具
C WinForm series button easy to use
Flink analysis (I): basic concept analysis
【ASM】字节码操作 ClassWriter 类介绍与使用
Automatic operation and maintenance sharp weapon ansible Foundation
Compile and build, from the bottom to the top
Example of batch update statement combining update and inner join in SQL Server
2022年大厂Android面试题汇总(一)(含答案)
03 products and promotion developed by individuals - plan service configurator v3.0
C # nanoframework lighting and key esp32
灵活报表v1.0(简单版)
Concept and basic knowledge of network layering
Flink parsing (IV): recovery mechanism
List set data removal (list.sublist.clear)
Pyspark operator processing spatial data full parsing (4): let's talk about spatial operations first
Basic configuration and use of spark
Automatic operation and maintenance sharp weapon ansible Playbook
Kali2021 installation and basic configuration