赞
踩
微信前台调用后台,传CODE 还有加密的文本
其中unionid 这个是需要在平台绑定的, 一个公司的小程序员,公众号等都 是同一个unionid 需要先绑定
1.需要微信前端调用wx.login接口获取code。 然后再调用wx.getuserInfo接口获取用户的信息。
2. 前端调用服务器接口,将获取到的code,以及encryptedData,和iv一起发送到后端。
3. 服务器在解密encryptedData之前,需要调用微信接口获取sessionkey. 有了encryptedData才能解密。
- wx.login({
- success: function (res_login) {
- if (res_login.code){
- wx.getUserInfo({
- success:function(res){
- console.log(res)
- var jsonData = {
- code: res_login.code,
- encryptedData: res.encryptedData,
- iv: res.iv
- };
- wx.request({
- url: 'http://com.testwechat.comlogin/userinfo',
- header: { 'Content-Type': 'application/json' },
- method:'POST',
- data: jsonData,
- success: res => {
- console.log(res.data)
- wx.hideLoading()
- this.setData({
- projectlist: res.data.content
- })
- }
- })
- }
- })
-
- }
- }
- })
前台参数至少包含这三个参数
- JSONObject dataObj = JSONObject.parseObject(infoData);
- String code = (String) dataObj.get("code");
- String encryptedData = (String) dataObj.get("encryptedData");
- String iv = (String) dataObj.get("iv");
- private static final String APP_SECRET=""; //写上自己的的
- private static final String APP_ID = ""; //写上自己的的APPID
-
- public JSONObject getSessionKey(String code) {
- String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + APP_ID + "&secret="
- + APP_SECRET + "&js_code=" + code + "&grant_type=authorization_code";
- String reusult = GET(url);
- JSONObject oppidObj = JSONObject.parseObject(reusult);
- String openid = (String) oppidObj.get("openid");
- String session_key = (String) oppidObj.get("session_key");
- oppidObj.put("openid", openid);
- oppidObj.put("session_key", session_key);
- if (oppidObj.containsKey("unionid")) {
- oppidObj.put("unionid", (String) oppidObj.get("unionid"));
- }else{
- oppidObj.put("unionid", "");
- }
- return oppidObj;
- }
解密用户传过来的信息
- /**
- * 获取信息
- */
- public JSONObject getUserInfo(String encryptedData,String sessionkey,String iv){
- // 被加密的数据
- byte[] dataByte = Base64.decode(encryptedData);
- // 加密秘钥
- byte[] keyByte = Base64.decode(sessionkey);
- // 偏移量
- byte[] ivByte = Base64.decode(iv);
- try {
- // 如果密钥不足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;
- }
- // 初始化
- Security.addProvider(new BouncyCastleProvider());
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
- SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
- AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
- parameters.init(new IvParameterSpec(ivByte));
- cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
- byte[] resultByte = cipher.doFinal(dataByte);
- if (null != resultByte && resultByte.length > 0) {
- String result = new String(resultByte, "UTF-8");
- return JSONObject.parseObject(result);
- }
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (NoSuchPaddingException e) {
- e.printStackTrace();
- } catch (InvalidParameterSpecException e) {
- e.printStackTrace();
- } catch (IllegalBlockSizeException e) {
- e.printStackTrace();
- } catch (BadPaddingException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (InvalidKeyException e) {
- e.printStackTrace();
- } catch (InvalidAlgorithmParameterException e) {
- e.printStackTrace();
- } catch (NoSuchProviderException e) {
- e.printStackTrace();
- }
- return null;
- }
下面是HPPT请求发送方式了
- //发起get请求的方法。
- public static String GET(String url) {
- String result = "";
- BufferedReader in = null;
- InputStream is = null;
- InputStreamReader isr = null;
- try {
- URL realUrl = new URL(url);
- URLConnection conn = realUrl.openConnection();
- conn.connect();
- Map<String, List<String>> map = conn.getHeaderFields();
- is = conn.getInputStream();
- isr = new InputStreamReader(is);
- in = new BufferedReader(isr);
- String line;
- while ((line = in.readLine()) != null) {
- result += line;
- }
- } catch (Exception e) {
- // 异常记录
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- if (is != null) {
- is.close();
- }
- if (isr != null) {
- isr.close();
- }
- } catch (Exception e2) {
- // 异常记录
- }
- }
- return result;
- }
需要的解密加密数据包
- <!-- 注:微信小程序加密解密包 xfire-core, bcprov-jdk1-->
- <dependency>
- <groupId>org.codehaus.xfire</groupId>
- <artifactId>xfire-core</artifactId>
- <version>1.2.6</version>
- </dependency>
- <dependency>
- <groupId>org.bouncycastle</groupId>
- <artifactId>bcprov-jdk16</artifactId>
- <version>1.46</version>
- </dependency>
这些流程配下来,基本可以走通微信接口登录,然后做自己想做的事情
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。