微信小程序用戶數據解密


概述

通過微信web開發者工具創建登錄,獲取用戶信息,發送至后台,進行用戶數據解密

詳細

一、准備工作

1、由於用到了 Redis 緩存,需要安裝 Redis(安裝略過)

2、后台開發環境Idea 15,java開發環境(安裝略過),由於是 Maven 構建的項目,在 Eclipse 下也可以運行

3、下載安裝微信Web開發者工具

4、本實例通過客戶端登錄請求后台,進行微信用戶信息解密

二、程序實現

1、原理

001.jpg

 

 

2、程序包括后台和客戶端,后台使用 Idea 15 進行開發實現,客戶端通過微信 web 開發者工具開發實現

后台截圖:

image.png

 

客戶端截圖:

image.png

 

3、思路

a、在web開發者工具上掃描二維碼登錄成功后

b、發送獲取 3rd_session 請求,獲取 3rd_session 數據

c、然后調用解密地址請求,對用戶信息進行解密

 

4、涉及到的關鍵代碼

a、客戶端獲取code

onLoad: function (options) {
    // 頁面初始化 options為頁面跳轉所帶來的參數
    let that = this
    wx.login({
      success: function (res) {
        // success
        let code = res.code
        that.setData({ code: code })
        wx.getUserInfo({
          success: function (res) {
            // success
            that.setData({ userInfo: res.userInfo })
            that.setData({ iv: res.iv })
            that.setData({ encryptedData: res.encryptedData })
            that.get3rdSession()
          }
        })
      }
  })
}

b、 客戶端發送 code 到服務端,獲取 3rd_session

get3rdSession:function(){
    let that = this
    wx.request({
      url: 'https://localhost:8443/get3rdSession',
      data: {
        code: this.data.code
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 設置請求的 header
      success: function (res) {
        // success
        var sessionId = res.data.session;
        that.setData({ sessionId: sessionId })
        wx.setStorageSync('sessionId', sessionId)
        that.decodeUserInfo()
      }
    })
  }

c、服務端發送 appid、appsecret、code 到微信服務器換取 session_key 和 openid

public void get3rdSession() {    //獲取名為userInfo的Redis Cache對象
    Cache userInfoCache = Redis.use("userInfo");
    String sessionId = "";
    JSONObject json = new JSONObject();
    String code = getPara("code");
    String url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx7560b8008e2c445d&secret=f1af3312b7038513fd17dd9cbc3b357c&js_code=" + code + "&grant_type=authorization_code";    //執行命令生成3rd_session
    String session = ExecLinuxCMDUtil.instance.exec("cat /dev/urandom |od -x | tr -d ' '| head -n 1").toString();
    json.put("session", session);    //創建默認的httpClient實例
    CloseableHttpClient httpClient = getHttpClient();    try {        //用get方法發送http請求
        HttpGet get = new HttpGet(url);
        System.out.println("執行get請求:...." + get.getURI());
        CloseableHttpResponse httpResponse = null;        //發送get請求
        httpResponse = httpClient.execute(get);        try {            //response實體
            HttpEntity entity = httpResponse.getEntity();            if (null != entity) {
                String result = EntityUtils.toString(entity);
                System.out.println(result);
                JSONObject resultJson = JSONObject.fromObject(result);
                String session_key = resultJson.getString("session_key");
                String openid = resultJson.getString("openid");                //session存儲
                userInfoCache.set(session,session_key+","+openid);
                }
            } finally {
                httpResponse.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {            try {
                closeHttpClient(httpClient);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        renderJson(json);
}private CloseableHttpClient getHttpClient() {    return HttpClients.createDefault();
}private void closeHttpClient(CloseableHttpClient client) throws IOException {    if (client != null) {
        client.close();
    }
}

 

d、客戶端發送請求解密用戶數據

decodeUserInfo:function(){
    let that = this
    wx.request({
      url: 'https://localhost:8443/decodeUserInfo',
      data: {
        encryptedData: that.data.encryptedData,
        iv: that.data.iv,
        session: wx.getStorageSync('sessionId')
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 設置請求的 header
      success: function (res) {
        // success
        console.log(res)
      }
    })
}

 

5、配置文件

 

a、由於微信小程序使用的是https請求,所以需要生成ssl請求,即 keystore,生成自己的 keystore 后替換 pom.xml 文件里

<keystore> /Users/LJaer/Workspaces/tomcat.keystore</keystore>

b、由於使用了 Redis 緩存,需要對 Redis 地址進行修改,AppConfig 類中,替換 redis 服務地址

RedisPlugin userInfoRedis = new RedisPlugin("userInfo","192.168.99.100");

 

三、運行效果

1、服務端啟動,使用命令 jetty:run 進行服務端的啟動

002.jpg

 

2、客戶端啟動,點擊編譯即可運行

image.png

 

3、用戶數據解密截圖

003.jpg

四、其他補充

官網文檔地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-login.html?t=20161222#wxloginobject

 

注:本文著作權歸作者,由demo大師發表,拒絕轉載,轉載需要作者授權


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM