亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

微信小程序獲取手機號,后端JAVA解密流程代碼

瀏覽:106日期:2022-05-26 11:52:55

小程序獲取手機號,后端JAVA解密流程代碼

微信官方文檔獲取手機號流程地址,先看下最好方便理解下面步驟實現思路,步驟如下

1.前端需先調用官方wx.login接口獲取登錄憑證code。2.后端接收code 調用官方接口地址獲取用戶秘鑰 sessionKey。3.前端通過官方getPhoneNumber獲取encryptedData,iv4.前端通過參數**【encryptedData】 、【iv】 、【sessionKey】** 發送請求后端接口,解密用戶手機號

小程序獲取sessionkey詳細接口文檔

后端工作如下,

1.參數code 解密出sessionKey {“session_key”:“eF9PAi5P7ZbSaQqkGzEY5g==”,“openid”:“otJ1I4zMSFGDtk7C33O_h6U3IRK8”} 2.參數sessionKey,iv,encryptedData 解密出手機號

代碼如下:

下面工具類很全,放心代碼必須全,良心教程。

業務代碼Controller

package com.df.detection.controller;import com.df.detection.base.entity.ResultBean;import io.swagger.annotations.Api;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import org.apache.commons.codec.binary.Base64;import org.json.JSONException;import org.springframework.web.bind.annotation.*;import java.io.UnsupportedEncodingException;import java.security.InvalidAlgorithmParameterException;import org.json.JSONObject;/** * @Author Songzhongjin * @Date 2020/7/15 10:09 * @Version 1.0 */@Api(value = '小程序登錄授權 Controller',tags = {'小程序登錄授權接口'})@RestController@RequestMapping('/app')public class APPController { /** * 微信小程序登錄獲取 * 獲取session_key * @param * @return */ @ResponseBody @PostMapping('/initWxLogin') @ApiImplicitParams({ @ApiImplicitParam(name = 'js_code', value = '登錄時獲取的code',paramType = 'form', dataType = 'string', required = true) }) public ResultBeaninitWxLogin(@RequestParam(value = 'js_code', required = true) String js_code) throws JSONException { //測試數據code// js_code = '081ZQ3f91fr9VM1HYdb91y93f91ZQ3fU'; //微信獲取session_key接口地址 String wxLoginUrl = 'https://api.weixin.qq.com/sns/jscode2session'; //接口參數 String param = 'appid=小程序id&secret=小程序secret&js_code=' + js_code + '&grant_type=authorization_code'; //調用獲取session_key接口 請求方式get String jsonString = GetPostUntil.sendGet(wxLoginUrl, param); System.out.println(jsonString); //因為json字符串是大括號包圍,所以用JSONObject解析 JSONObject json = new JSONObject(jsonString); //json解析session_key值 String session_key = json.getString('session_key'); System.out.println('session_key:' + session_key); //返回給前端 return ResultBean.success('session_key',session_key); } /** * 解密小程序用戶敏感數據 * * @param encryptedData 明文 * @param iv 加密算法的初始向量 * @param sessionKey 用戶秘鑰 * @return */ @ResponseBody @PostMapping(value = '/decodeUserInfo') @ApiImplicitParams({ @ApiImplicitParam(name = 'encryptedData', value = '包括敏感數據在內的完整用戶信息的加密數據',paramType = 'form', dataType = 'string', required = true), @ApiImplicitParam(name = 'iv', value = '加密算法的初始向量',paramType = 'form', dataType = 'string', required = true), @ApiImplicitParam(name = 'sessionKey', value = '用戶秘鑰',paramType = 'form', dataType = 'string', required = true) }) public ResultBean decodeUserInfo(@RequestParam(required = true, value = 'encryptedData') String encryptedData, @RequestParam(required = true, value = 'iv') String iv, @RequestParam(required = true, value = 'sessionKey') String sessionKey ) throws UnsupportedEncodingException, InvalidAlgorithmParameterException, JSONException { //AESUtils微信獲取手機號解密工具類 AESUtils aes = new AESUtils(); //調用AESUtils工具類decrypt方法解密獲取json串 byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData), Base64.decodeBase64(sessionKey), Base64.decodeBase64(iv)); //判斷返回參數是否為空 if (null != resultByte && resultByte.length > 0) { String jsons = new String(resultByte, 'UTF-8'); System.out.println(jsons); JSONObject json = new JSONObject(jsons); //json解析phoneNumber值 String phoneNumber = json.getString('phoneNumber'); System.out.println('phoneNumber:' + phoneNumber); return ResultBean.success('手機號', phoneNumber); } return ResultBean.error(500,'session_key:失敗'); }}

工具類代碼如下

package com.df.detection.controller;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import java.net.URLConnection;import java.util.List;import java.util.Map;/** * @Author Songzhongjin * @Date 2020/7/15 10:37 * @Version 1.0 */public class GetPostUntil { /** * 向指定URL發送GET方法的請求 * * @param url * 發送請求的URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return URL 所代表遠程資源的響應結果 */ public static String sendGet(String url, String param) { String result = ''; BufferedReader in = null; try {String urlNameString = url + '?' + param;URL realUrl = new URL(urlNameString);// 打開和URL之間的連接URLConnection connection = realUrl.openConnection();// 設置通用的請求屬性connection.setRequestProperty('accept', '*/*');connection.setRequestProperty('connection', 'Keep-Alive');connection.setRequestProperty('user-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)');// 建立實際的連接connection.connect();// 獲取所有響應頭字段Map<String, List<String>> map = connection.getHeaderFields();// 遍歷所有的響應頭字段for (String key : map.keySet()) { System.out.println(key + '--->' + map.get(key));}// 定義 BufferedReader輸入流來讀取URL的響應in = new BufferedReader(new InputStreamReader( connection.getInputStream()));String line;while ((line = in.readLine()) != null) { result += line;} } catch (Exception e) {System.out.println('發送GET請求出現異常!' + e);e.printStackTrace(); } // 使用finally塊來關閉輸入流 finally {try { if (in != null) { in.close(); }} catch (Exception e2) { e2.printStackTrace();} } return result; } /** * 向指定 URL 發送POST方法的請求 * * @param url * 發送請求的 URL * @param param * 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。 * @return 所代表遠程資源的響應結果 */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ''; try {URL realUrl = new URL(url);// 打開和URL之間的連接URLConnection conn = realUrl.openConnection();// 設置通用的請求屬性conn.setRequestProperty('accept', '*/*');conn.setRequestProperty('connection', 'Keep-Alive');conn.setRequestProperty('user-agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)');// 發送POST請求必須設置如下兩行conn.setDoOutput(true);conn.setDoInput(true);// 獲取URLConnection對象對應的輸出流out = new PrintWriter(conn.getOutputStream());// 發送請求參數out.print(param);// flush輸出流的緩沖out.flush();// 定義BufferedReader輸入流來讀取URL的響應in = new BufferedReader( new InputStreamReader(conn.getInputStream()));String line;while ((line = in.readLine()) != null) { result += line;} } catch (Exception e) {System.out.println('發送 POST 請求出現異常!'+e);e.printStackTrace(); } //使用finally塊來關閉輸出流、輸入流 finally{try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); }}catch(IOException ex){ ex.printStackTrace();} } return result; } }

AESUtils工具類 解密手機號

package com.df.detection.controller;import org.apache.tomcat.util.codec.binary.Base64;import org.bouncycastle.jce.provider.BouncyCastleProvider;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;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 javax.xml.transform.Result;import java.security.*;/** * @Author Songzhongjin * @Date 2020/7/15 11:46 * @Version 1.0 */public class AESUtils { public static boolean initialized = false; /** * AES解密 * @param content 密文 * @return * @throws InvalidAlgorithmParameterException * @throws NoSuchProviderException */ public byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException { initialize(); try { Cipher cipher = Cipher.getInstance('AES/CBC/PKCS7Padding'); Key sKeySpec = new SecretKeySpec(keyByte, 'AES'); cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));// 初始化 byte[] result = cipher.doFinal(content); return result; } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchProviderException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void initialize(){ if (initialized) { return; } Security.addProvider(new BouncyCastleProvider()); initialized = true; } //生成iv public static AlgorithmParameters generateIV(byte[] iv) throws Exception { AlgorithmParameters params = AlgorithmParameters.getInstance('AES'); params.init(new IvParameterSpec(iv)); return params; }}

接口返回對象ResultBean定義工具類 防止有些朋友發現沒有這個類

package com.df.detection.base.entity;import io.swagger.annotations.ApiModelProperty;/** * @author Liu Yaoguang * @Classname aaa * @Description * @Date 2019/12/06 09:22 */public class ResultBean<T> { @ApiModelProperty(value = '返回碼',dataType = 'int') private int code; @ApiModelProperty(value = '返回描述信息',dataType = 'string') private String message; @ApiModelProperty(value = '返回數據') private T data; @ApiModelProperty(value = '口令',dataType = 'string') private String token; private ResultBean() { } public static ResultBean error(int code, String message) { ResultBean resultBean = new ResultBean(); resultBean.setCode(code); resultBean.setMessage(message); return resultBean; } public static<T> ResultBean error(int code, String message,T data) { ResultBean resultBean = new ResultBean(); resultBean.setCode(code); resultBean.setMessage(message); resultBean.setData(data); return resultBean; } public static ResultBean success(String message) { ResultBean resultBean = new ResultBean(); resultBean.setCode(200); resultBean.setMessage(message); return resultBean; } public static<T> ResultBean success(String message,T data) { ResultBean resultBean = new ResultBean(); resultBean.setCode(200); resultBean.setMessage(message); resultBean.setData(data); return resultBean; } public static ResultBean success(String message,Object data,String token) { ResultBean resultBean = new ResultBean(); resultBean.setCode(200); resultBean.setMessage(message); resultBean.setData(data); resultBean.setToken(token); return resultBean; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } public String getToken() { return token; } public void setToken(String token) { this.token = token; }}

以上就是微信小程序獲取手機號,后端JAVA解密流程代碼的詳細內容,更多關于微信小程序獲取手機號的資料請關注好吧啦網其它相關文章!

標簽: 微信
主站蜘蛛池模板: 国产私拍精品88福利视频 | 日本一区二区三区精品视频 | 欧美伦理三级在线播放影院 | 欧美a级在线观看 | 青青草手机在线观看 | jizz免费软件| 国产一区美女视频 | 成年黄网站免费大全毛片 | 婷婷精品视频 | 成年日韩免费大片黄在线观看 | 韩国xxxxxxxx69| 一本久道久久综合婷婷五 | 激情一区二区三区成人 | 一级毛片大全免费播放 | 色综色 | 成年人污视频 | 国产精品久久久久毛片真精品 | 久久a视频 | 欧美成人高清乱码 | 国产精品高清视亚洲精品 | 特黄a大片免费视频 | 成人精品一区二区三区 | 中文字幕永久在线观看 | 香蕉视频色板 | 免费人成黄页在线观看69 | 国产在线成人一区二区 | 色涩网站 | 女人被狂躁的免费视频高清 | 在线精品国内视频秒播 | 亚洲午夜精品aaa级久久久久 | 中国国产一国产一级毛片视频 | 香蕉精品 | 国产日韩欧美在线视频免费观看 | 久久国产免费一区二区三区 | 九九九色视频在线观看免费 | 亚洲午夜视频在线观看 | 久久香蕉精品视频 | 成人综合色站 | 久久精品国产半推半就 | 国产一区二区三区在线看 | 国产精品久久在线观看 |