1。WECHATAPPLET <BR />ステップ1:WX.Loginを呼び出してコードドキュメントアドレスを取得するステップ2:ユーザー情報ドキュメントアドレスを読むことを許可するかどうかを判断します。したがって、wx.loginが取得した暗号化とIVを渡して、wx.requestリクエストを介してコードとwx.getuserinfoを背景に取得します。
サーバーによって返されるデータ:
ミニプログラムコード:
//ログインインターフェイスを呼び出してコードwx.login({success:function(res){wx.getSetting({success(setres){//承認されたif(!setres.authsetting ['scope.userinfo']){// wx.authorize({scope.userinfo 'edurinfo' 'scope:' scope.userinfo '' scope: 'scope.userinfo' eduthorizeへのアクセス){// wx.getuserInfo({lang: "zh_cn"、success:function(userres){// stack network request wx.request({url:config.loginwxurl、data:{code:res.code、encrypteddata:userres.encrypteddata、inters.iv:userres.ivpepe: " 「アプリケーション/x-www-form-urlencoded: 'post'、// server-side retireval(result){result.data.expiretime = nowdate + expiretime}; {//ユーザー情報wx.getuserinfo({lang: "zh_cn"、success:function(userres){// stack network request wx.request({url:config.loginwxurl、data:{code:code:res.code、res.code、rasrypteddata:userers.encrysteddata、useres.iv:futedta、usereres. 「コンテンツタイプ」:「アプリケーション/x-www-form-urlencoded}、 'post'(result){result.data.recime = nowdate + expiretime( " }}})}}) 2。Javaサーバー
openIDを取得し、コードに従ってユーザー情報コードをデコードします
必要な瓶パッケージ
<Dependency> groupId> org.codehaus.xfire </groupid> <artifactid> xfire-core </artifactid> <version> 1.2.6 </version> </dependency> <dependency> <groupid> org.bouncycastle </groupid> <artifactid> </依存関係>
/ ** * wechatアプレット情報を取得 * @author zhy */ public class wxappletuserinfo {private static logger log = logger.getLogger(wxappletuserinfo.class); /** * get wechat applet session_key and openid * * @author zhy * @paramコードwechat login * @return */public static jsonobject getsessionkeyoropenid(string code){// wechat login code value wxcode = code = code; ResourceBundle resource = resourcebundle.getBundle( "weixin"); //プロパティファイルを読み取る文字列requesturl = resource.getString( "url"); //アドレスアドレスhttps://api.weixin.qq.com/sns/jscode2sessionマップ<文字列、文字列> requesturlparam = new hashmap <string、string>(); requesturlparam.put( "appid"、resource.getString( "appid")); //開発者設定のappid requesturlparam.put( "secret"、resource.getString( "appsecret")); // appsecret requesturlparam.put( "js_code"、wxcode); //アプレットによって返されたコードwx.login requesturlparam.put( "grant_type"、 "authorization_code"); //デフォルトのパラメーター// wechat https://api.weixin.qqc.com/sns/jscode2sessionインターフェイスを読み取るために投稿リクエストを送信して、openidユーザー一意の識別子jsonobject jsonobject = json.parseobject(urlutil.sendpost(requesturl、requesturlparam); jsonobjectを返します。 } / ***ユーザーに敏感なデータを復号化してユーザー情報を取得する** @author zhy* @param sessionキーデータの暗号化された署名のキーiv){//暗号化されたデータバイト[] databyte = base64.decode(encryptedData); //暗号化キーバイト[] keybyte = base64.decode(sessionkey); // byte [] ivbyte = base64.decode(iv); {//キーが16ビット未満の場合は、それを補います。このIFのコンテンツは非常に重要なint base = 16です。 if(keybyte.length%base!= 0){int groups = keybyte.length / base +(keybyte.length%base!= 0?1:0); byte [] temp = new byte [groups * base]; arrays.fill(temp、(byte)0); System.ArrayCopy(keybyte、0、temp、0、keybyte.length); keybyte = temp; } // sitence.addprovider(new bouncycastleprovider());を初期化します。 cipher cipher = cipher.getInstance( "aes/cbc/pkcs7padding"、 "bc"); SecretKeysPec spec = new SecretKeyspec(keybyte、 "aes"); algorithmparametersパラメーター= algorithmparameters.getInstance( "aes"); parameters.init(new ivparameterspec(ivbyte)); cipher.init(cipher.decrypt_mode、spec、パラメーター); // initialize byte [] resultbyte = cipher.dofinal(databyte); if(null!= resultbyte && resultbyte.length> 0){string result = new String(resultbyte、 "utf-8"); json.parseobject(result)を返します。 }} 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(Illegalblocksizeexception e){log.Error(e.getMessage()、e); } catch(badPaddingException e){log.Error(e.getMessage()、e); } catch(unsupportedencodingexception e){log.error(e.getmessage()、e); } catch(unsupportedencodingexception 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); } nullを返します。 }}リクエストを送信するコード
/ *** POSTメソッドの指定されたURLにリクエストを送信** @Param URLリクエストを送信するためのURL* @Param Param Requestパラメーター* @return名で表されるリモートリソースの応答結果*/ ublic static string sendpost(string url、map <string、?> parammap){printwriter out = null; bufferedreader in = null;文字列結果= "";文字列param = ""; iterator <string> it = parammap.keyset()。iterator(); while(it.hasnext()){string key = it.next(); param + = key + "=" + parammap.get(key) + "&"; } try {url realurl = new url(url); // urlconnection conn = realur.openconnection()の間の接続を開きます。 //一般リクエスト属性conn.setRequestProperty( "Accept"、 "*/*"); conn.setRequestProperty( "Connection"、 "Keep-Alive"); conn.setRequestProperty( "Accept-charset"、 "utf-8"); conn.setRequestProperty( "user-agent"、 "mozilla/4.0(互換; msie 6.0; windows nt 5.1; sv1)"); // POSTリクエストを送信するには、次の2行を設定する必要がありますconn.setDoOutput(true); conn.setDoinput(true); // urlConnectionオブジェクトに対応する出力ストリームを取得= new PrintWriter(conn.getOutputStream()); //リクエストパラメーターを送信してください。print(param); //フラッシュ出力ストリームのbuffering.flush()。 // bufferedReader入力ストリームを定義してurl応答を読み取ります= new bufferedReader(new inputStreamReader(conn.getInputStream()、 "utf-8"));文字列線; while((line = in.readline())!= null){result += line; }} catch(例外e){log.error(e.getmessage()、e); } //最終的にブロックを使用して出力ストリームと入力ストリームを閉じて{if(out!= null){out.close(); } if(in!= null){in.close(); }} catch(ioException ex){ex.printstacktrace(); }} return result; }上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。