当前位置:网站首页>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;
}
}
边栏推荐
- Connect to LAN MySQL
- Guidelines for preparing for the 2022 soft exam information security engineer exam
- MySQL error reporting solution
- How does wechat prevent withdrawal come true?
- Solrcloud related commands
- [rapid environment construction] openharmony 10 minute tutorial (cub pie)
- mysql高级(索引,视图,存储过程,函数,修改密码)
- Sqoop I have everything you want
- Development and practice of lightweight planning service tools
- Integrated development management platform
猜你喜欢

C WinForm series button easy to use

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

Vscode replaces commas, or specific characters with newlines

06 products and promotion developed by individuals - code statistical tools

2022年大厂Android面试题汇总(一)(含答案)

【MySQL入门】第四话 · 和kiko一起探索MySQL中的运算符

全网最全tcpdump和Wireshark抓包实践
![[translation] principle analysis of X Window Manager (I)](/img/40/6e15e1acebb47061d6e0e4c8ff82ea.jpg)
[translation] principle analysis of X Window Manager (I)

Development and practice of lightweight planning service tools

Pyspark operator processing spatial data full parsing (4): let's talk about spatial operations first
随机推荐
Flink parsing (IV): recovery mechanism
Quick start of Hongmeng system
C # nanoframework lighting and key esp32
Spark calculation operator and some small details in liunx
06 products and promotion developed by individuals - code statistical tools
Solr appears write Lock, solrexception: could not get leader props in the log
07个人研发的产品及推广-人力资源信息管理系统
The art of Engineering (1): try to package things that do not need to be exposed
学 SQL 必须了解的 10 个高级概念
[VNCTF 2022]ezmath wp
[reverse] repair IAT and close ASLR after shelling
Sqoop I have everything you want
04 products and promotion developed by individuals - data push tool
Virtual machine startup prompt probing EDD (edd=off to disable) error
Grafana 9 is officially released, which is easier to use and more cool!
Xin'an Second Edition; Chapter 11 learning notes on the principle and application of network physical isolation technology
远程代码执行渗透测试——B模块测试
Total / statistics function of MySQL
EasyRE WriteUp
微信小程序获取手机号