1. WeChat Applet <br /> ขั้นตอนที่ 1: โทร wx.login เพื่อรับรหัสที่อยู่เอกสารขั้นตอนที่ 2: พิจารณาว่าผู้ใช้อนุญาตให้อ่านข้อมูลผู้ใช้ที่อยู่เอกสารที่อยู่ขั้นตอนที่ 3: โทร wx.getUserInfo เพื่ออ่านข้อมูลที่อยู่ ดังนั้นเราจะผ่าน EncryptedData และ IV ที่ได้รับจาก wx.login เพื่อรับรหัสและ wx.getuserinfo ไปยังพื้นหลังผ่านคำขอ wx.request
ข้อมูลที่ส่งคืนโดยเซิร์ฟเวอร์:
รหัสโปรแกรมมินิ:
// การเรียกอินเตอร์เฟสเข้าสู่ระบบเพื่อรับรหัส wx.login ({ความสำเร็จ: ฟังก์ชั่น (res) {wx.getSetting ({ความสำเร็จ (setres) {// กำหนดว่าได้รับอนุญาตถ้า (! setRes.authetting ['scope.userinfo']) {// wx.getUserInfo ({lang: "zh_cn", ความสำเร็จ: ฟังก์ชั่น (userres) {// คำขอเครือข่ายสแต็ก wx.request ({url: config.loginwxurl, ข้อมูล: {code: res.code, encrypteddata: userres.encrypteddata "แอปพลิเคชัน/x-www-form-urlencoded"}, วิธี: post ', // ความสำเร็จในการดึงข้อมูลของเซิร์ฟเวอร์: ฟังก์ชั่น (ผลลัพธ์) {var data = result.data.result; {// รับข้อมูลผู้ใช้ wx.getUserInfo ({lang: "zh_cn", ความสำเร็จ: ฟังก์ชั่น (userres) {// คำขอเครือข่ายสแต็ก wx.request ({url: config.loginwxurl, ข้อมูล: {code: res.code, EncryptedData "ประเภทเนื้อหา": "แอปพลิเคชัน/x-www-form-urlencoded"}, วิธีการ: 'โพสต์', ความสำเร็จ: ฟังก์ชั่น (ผลลัพธ์) {var data = result.data.result; - 2. เซิร์ฟเวอร์ Java
รับรหัสข้อมูลผู้ใช้ OpenID และถอดรหัสตามรหัส
แพ็คเกจขวดที่จำเป็น
<การพึ่งพา> <roupId> org.codehaus.xfire </groupId> <ratifactid> xfire-core </artifactid> <persion> 1.2.6 </version> </การพึ่งพา> <การพึ่งพา
/ ** * รับข้อมูล Applet WeChat * @author Zhy */ คลาสสาธารณะ wxappletuserinfo {logger แบบคงที่ส่วนตัว = logger.getLogger (wxappletuserinfo.class); /** * รับ WeChat Applet Session_key และ OpenId * * @author Zhy * @param รหัสโทรรหัสที่ส่งคืนโดย WeChat Login * @return */สาธารณะ JsonObject GetSessionKeyOropenid (รหัสสตริง) {// WeChat Login Code ResourceBundle Resource = ResourceBundle.getBundle ("Weixin"); // อ่านสตริงไฟล์คุณสมบัติ requestUrl = resource.getString ("url"); // ที่อยู่คำขอ https://api.weixin.qq.com/sns/jscode2session Map <String, String> requesturlparam = new hashmap <string, string> (); requesturlparam.put ("appid", resource.getString ("appid")); // appid ในการตั้งค่านักพัฒนา requesturlparam.put ("ลับ", Resource.getString ("AppSecret")); // appsecret requesturlparam.put ("js_code", wxCode); // รหัสที่ส่งคืนโดย Applet Call Wx.login RequesturlParam.put ("Grant_type", "Authorization_Code"); // พารามิเตอร์เริ่มต้น // ส่งคำขอโพสต์เพื่ออ่านและเรียก weChat https://api.weixin.qq.com/sns/jscode2session อินเตอร์เฟสเพื่อรับตัวระบุผู้ใช้ที่ไม่ซ้ำกัน jsonobject jsonobject = json.parseObject (urlutil.sendpost กลับ JsonObject; } / *** DECRYPT ข้อมูลที่ละเอียดอ่อนของผู้ใช้เพื่อรับข้อมูลผู้ใช้** @author Zhy* @param SessionKey คีย์สำหรับลายเซ็นที่เข้ารหัสของข้อมูล* @param EncryptedData ข้อมูลที่เข้ารหัสรวมถึงข้อมูลที่ละเอียดอ่อน iv) {// ข้อมูลที่เข้ารหัสไบต์ [] databyte = base64.decode (EncryptedData); // รหัสที่เข้ารหัสไบต์ [] keyByte = base64.decode (sessionKey); // ออฟเซ็ตไบต์ [] ivbyte = base64.decode (iv); ลอง {// ถ้าคีย์น้อยกว่า 16 บิตจากนั้นทำขึ้นมา เนื้อหาในนี้หากเป็นฐาน int สำคัญมาก = 16; if (keyByte.Length % ฐาน! = 0) {int Groups = keyByte.length / base + (keyByte.length % ฐาน! = 0? 1: 0); ไบต์ [] temp = ไบต์ใหม่ [กลุ่ม * ฐาน]; array.fill (temp, (byte) 0); System.ArrayCopy (KeyByte, 0, Temp, 0, KeyByte.Length); keybyte = temp; } // เริ่มต้น Security.addProvider (ใหม่ bouncycastleprovider ()); Cipher Cipher = Cipher.GetInstance ("AES/CBC/PKCS7PADDING", "BC"); secretkeyspec spec = new SecretKeySpec (KeyByte, "AES"); อัลกอริทึมพารามิเตอร์พารามิเตอร์ = อัลกอริทึมพารามิเตอร์ GetInstance ("AES"); parameters.init (IVParameterspec ใหม่ (ivbyte)); cipher.init (cipher.decrypt_mode, สเป็ค, พารามิเตอร์); // เริ่มต้นไบต์ [] resultbyte = cipher.dofinal (databyte); if (null! = resultByte && resultByte.Length> 0) {string result = สตริงใหม่ (resultByte, "UTF-8"); return json.parseObject (ผลลัพธ์); }} catch (nosuchalgorithmexception e) {log.error (e.getMessage (), e); } catch (nosuchpaddingexception e) {log.error (e.getMessage (), e); } catch (invalidParameterspecexception e) {log.error (e.getMessage (), e); } catch (unlueLblocksizeException e) {log.error (e.getMessage (), e); } catch (badpaddingexception e) {log.error (e.getMessage (), e); } catch (unsupportencodingexception e) {log.error (e.getMessage (), e); } catch (unsupportencodingexception e) {log.error (e.getMessage (), e); } catch (InvalidKeyException e) {log.error (e.getMessage (), e); } catch (invalidalgorithmparameterexception e) {log.error (e.getMessage (), e); } catch (nosuchproviderexception e) {log.error (e.getMessage (), e); } return null; - รหัสเพื่อส่งคำขอ
/ *** ส่งคำขอไปยัง URL ที่ระบุสำหรับวิธีการโพสต์** @param url URL สำหรับการส่งคำขอ* @param พารามิเตอร์การร้องขอพารามิเตอร์* @ @กลับมาผลการตอบกลับของทรัพยากรระยะไกลที่แสดงโดยชื่อ*/ ublic string sendpost (url สตริง, แผนที่ <string,?> parammap) bufferedReader ใน = null; สตริงผลลัพธ์ = ""; String param = ""; ตัววนซ้ำ <String> it = parammap.keyset (). iterator (); ในขณะที่ (it.hasnext ()) {string key = it.next (); param + = key + "=" + parammap.get (คีย์) + "&"; } ลอง {url realurl = url ใหม่ (url); // เปิดการเชื่อมต่อระหว่าง urlConnection conn = realurl.openconnection (); // ตั้งค่าแอตทริบิวต์คำขอทั่วไป conn.setRequestProperty ("ยอมรับ", "*/*"); conn.setRequestProperty ("การเชื่อมต่อ", "Keep-Alive"); conn.setRequestProperty ("Accept-Charset", "UTF-8"); conn.setRequestProperty ("ผู้ใช้ตัวแทน", "Mozilla/4.0 (เข้ากันได้; MSIE 6.0; Windows NT 5.1; SV1)"); // ในการส่งคำขอโพสต์คุณต้องตั้งค่าสองบรรทัดต่อไปนี้ conn.SetDoOutput (จริง); conn.setDoInput (จริง); // รับกระแสเอาต์พุตที่สอดคล้องกับวัตถุ urlConnection out = printwriter ใหม่ (conn.getOutputStream ()); // ส่งพารามิเตอร์การร้องขอ out.print (param); // บัฟเฟอร์ out.flush () ของสตรีมเอาต์พุตฟลัช; // กำหนดสตรีมอินพุต bufferedReader เพื่ออ่านการตอบสนอง URL ใน = ใหม่ bufferedReader (ใหม่ inputStreamReader (conn.getInputStream (), "UTF-8")); สายสตริง; ในขณะที่ ((line = in.readline ())! = null) {result += line; }} catch (exception e) {log.error (e.getMessage (), e); } // ใช้บล็อกในที่สุดเพื่อปิดสตรีมเอาต์พุตและสตรีมอินพุตในที่สุด {ลอง {ถ้า (ออก! = null) {out.close (); } if (in! = null) {in.close (); }} catch (ioexception ex) {ex.printstacktrace (); }} ผลการส่งคืน; -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น