微信小程序与服务器理解例子,微信小程序调用微信登陆获取openid及java做为服务端示例...

调用 wx.login获取code 文档地址

第二步:判断用户是否授权读取用户信息 @L_502_1@

第三步:调用wx.getUserInfo读取用户数据 文档地址

第四步:由于小程序后台授权域名无法授权微信的域名,所以我们只能通过我们自己的服务器去调用微信服务器去获取用户信息,故我们将wx.login获取code 和 wx.getUserInfo 获取的encryptedData与iv 通过wx.request 请求传入后台

15178213711.png?2018029112540

服务器返回的数据:

15178213722.png?201802911262

//调用登录接口,获取 code

wx.login({

success: function (res) {

wx.getSetting({

success(setRes) {

// 判断是否已授权

if (!setRes.authSetting['scope.userInfo']) {

// 授权访问

wx.authorize({

scope: 'scope.userInfo',success() {

//获取用户信息

wx.getUserInfo({

lang: "zh_CN",success: function (userRes) {

//发起网络请求

wx.request({

url: config.loginWXUrl,data: {

code: res.code,encryptedData: userRes.encryptedData,iv: userRes.iv

},header: {

"Content-Type": "application/x-www-form-urlencoded"

},method: 'POST',//服务端的回掉

success: function (result) {

var data = result.data.result;

data.expireTime = nowDate + EXPIRETIME;

wx.setStorageSync("userInfo",data);

userInfo = data;

}

})

}

})

}

})

} else {

//获取用户信息

wx.getUserInfo({

lang: "zh_CN",success: function (userRes) {

//发起网络请求

wx.request({

url: config.loginWXUrl,data: {

code: res.code,iv: userRes.iv

},header: {

"Content-Type": "application/x-www-form-urlencoded"

},success: function (result) {

var data = result.data.result;

data.expireTime = nowDate + EXPIRETIME;

wx.setStorageSync("userInfo",data);

userInfo = data;

}

})

}

})

}

}

})

}

})

二、java服务端

根据code获取openid与解码用户信息 代码

所需要的jar包

@H_301_38@

org.codehaus.xfire

xfire-core

1.2.6

org.bouncycastle

bcprov-jdk16

1.46

/**

* 微信小程序信息获取

*

* @author zhy

*/

public class WXAppletUserInfo {

private static Logger log = Logger.getLogger(WXAppletUserInfo.class);

/**

* 获取微信小程序 session_key 和 openid

*

* @author zhy

* @param code 调用微信登陆返回的Code

* @return

*/

public static JSONObject getSessionKeyOropenid(String code){

//微信端登录code值

String wxCode = code;

ResourceBundle resource = ResourceBundle.getBundle("weixin"); //读取属性文件

String requestUrl = resource.getString("url"); //请求地址 https://api.weixin.qq.com/sns/jscode2session

Map requestUrlParam = new HashMap();

requestUrlParam.put("appid",resource.getString("appId")); //开发者设置中的appId

requestUrlParam.put("secret",resource.getString("appSecret")); //开发者设置中的appSecret

requestUrlParam.put("js_code",wxCode); //小程序调用wx.login返回的code

requestUrlParam.put("grant_type","authorization_code"); //默认参数

//发送post请求读取调用微信 https://api.weixin.qq.com/sns/jscode2session 接口获取openid用户唯一标识

JSONObject jsonObject = JSON.parSEObject(UrlUtil.sendPost(requestUrl,requestUrlParam));

return jsonObject;

}

/**

* 解密用户敏感数据获取用户信息

*

* @author zhy

* @param sessionKey 数据进行加密签名的密钥

* @param encryptedData 包括敏感数据在内的完整用户信息的加密数据

* @param iv 加密算法的初始向量

* @return

*/

public static 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,temp,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 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 (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 发送POST方法的请求

*

* @param url 发送请求的 URL

* @param param 请求参数

* @return 所代表远程资源的响应结果

*/

ublic static String sendPost(String url,Map paramMap) {

PrintWriter out = null;

BufferedReader in = null;

String result = "";

String param = "";

Iterator it = paramMap.keySet().iterator();

while(it.hasNext()) {

String key = it.next();

param += key + "=" + paramMap.get(key) + "&";

}

try {

URL realUrl = new URL(url);

// 打开和URL之间的连接

URLConnection conn = realUrl.openConnection();

// 设置通用的请求属性

conn.setRequestProperty("accept","*/*");

conn.setRequestProperty("connection","Keep-Alive");

conn.setRequestProperty("Accept-Charset","utf-8");

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(),"UTF-8"));

String line;

while ((line = in.readLine()) != null) {

result += line;

}

} catch (Exception e) {

log.error(e.getMessage(),e);

}

//使用finally块来关闭输出流、输入流

finally{

try{

if(out!=null){

out.close();

}

if(in!=null){

in.close();

}

}

catch(IOException ex){

ex.printStackTrace();

}

}

return result;

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢寻找一群志同道合、互帮互助的学习伙伴,可以点击下方链接加入:

编程之家官方1群

编程之家官方2群

编程之家官方3群

编程之家官方4群

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值