- 浏览: 5104054 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
silence19841230:
先拿走看看
SpringBoot2.0开发WebSocket应用完整示例 -
wallimn:
masuweng 写道发下源码下载地址吧!三个相关文件打了个包 ...
SpringBoot2.0开发WebSocket应用完整示例 -
masuweng:
发下源码下载地址吧!
SpringBoot2.0开发WebSocket应用完整示例 -
masuweng:
SpringBoot2.0开发WebSocket应用完整示例 -
wallimn:
水淼火 写道你好,我使用以后,图标不显示,应该怎么引用呢,谢谢 ...
前端框架iviewui使用示例之菜单+多Tab页布局
微信小程序序的代码中提示,使用code换取openid,但官方文档中没有给出进一步的说明。
换取openid的要点:由于安全的原因,必须由自己小程序的服务器端完成。知道了这个要点,实现起来就简单了,服务器端写一个RestController,接收code参数,使用httpclient向微信的服务端换openid就行了。
代码使用了SpringBoot,不会也不难理解。主要代码如下:
用到了一个httpclient封闭的工具类,代码如下:
小程序中,使用wx.request(url:'https://域名/api/wx/session',....),就可以拿到一个JSON对象,其中有openid。
换取openid的要点:由于安全的原因,必须由自己小程序的服务器端完成。知道了这个要点,实现起来就简单了,服务器端写一个RestController,接收code参数,使用httpclient向微信的服务端换openid就行了。
代码使用了SpringBoot,不会也不难理解。主要代码如下:
package com.wallimn.iteye.sp.asset.common.controller; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.fasterxml.jackson.databind.ObjectMapper; import com.wallimn.iteye.sp.asset.common.util.AesUtil; import com.wallimn.iteye.sp.asset.common.util.HttpUtil; @RestController @RequestMapping("/api/wx") public class WeixinController { private static Logger log = LoggerFactory.getLogger(WeixinController.class); @Value("${wx.appId}") private String appId; @Value("${wx.appSecret}") private String appSecret; @Value("${wx.grantType}") private String grantType; // wx.grantType=authorization_code @Value("${wx.requestUrl}") private String requestUrl; // wx.requestUrl=https://api.weixin.qq.com/sns/jscode2session @RequestMapping("/session") public Map<String, Object> getSession(@RequestParam(required = true) String code) { return this.getSessionByCode(code); } @SuppressWarnings("unchecked") private Map<String, Object> getSessionByCode(String code) { String url = this.requestUrl + "?appid=" + appId + "&secret=" + appSecret + "&js_code=" + code + "&grant_type=" + grantType; // 发送请求 String data = HttpUtil.get(url); log.debug("请求地址:{}", url); log.debug("请求结果:{}", data); ObjectMapper mapper = new ObjectMapper(); Map<String, Object> json = null; try { json = mapper.readValue(data, Map.class); } catch (Exception e) { e.printStackTrace(); } // 形如{"session_key":"6w7Br3JsRQzBiGZwvlZAiA==","openid":"oQO565cXXXXXEvc4Q_YChUE8PqB60Y"}的字符串 return json; } }
用到了一个httpclient封闭的工具类,代码如下:
package com.wallimn.iteye.sp.asset.common.util; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; public class HttpUtil { private static final String Charset = "utf-8"; /** * 发送请求,如果失败,会返回null * @param url * @param map * @return */ public static String post(String url, Map<String, String> map) { // 处理请求地址 try { HttpClient client = HttpClientBuilder.create().build(); URI uri = new URI(url); HttpPost post = new HttpPost(uri); // 添加参数 List<NameValuePair> params = new ArrayList<NameValuePair>(); for (String str : map.keySet()) { params.add(new BasicNameValuePair(str, map.get(str))); } post.setEntity(new UrlEncodedFormEntity(params, Charset)); // 执行请求 HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() == 200) { // 处理请求结果 StringBuffer buffer = new StringBuffer(); InputStream in = null; try { in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in,Charset)); String line = null; while ((line = reader.readLine()) != null) { buffer.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭流 if (in != null) try { in.close(); } catch (Exception e) { e.printStackTrace(); } } return buffer.toString(); } else { return null; } } catch (Exception e1) { e1.printStackTrace(); } return null; } /** * 发送请求,如果失败会返回null * @param url * @param str * @return */ public static String post(String url, String str) { // 处理请求地址 try { HttpClient client = HttpClientBuilder.create().build(); URI uri = new URI(url); HttpPost post = new HttpPost(uri); post.setEntity(new StringEntity(str, Charset)); // 执行请求 HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() == 200) { // 处理请求结果 StringBuffer buffer = new StringBuffer(); InputStream in = null; try { in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8")); String line = null; while ((line = reader.readLine()) != null) { buffer.append(line); } } finally { // 关闭流 if (in != null) in.close(); } return buffer.toString(); } else { return null; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 发送GET方式的请求,并返回结果字符串。 * <br> * 时间:2017年2月27日,作者:http://wallimn.iteye.com * @param url * @return 如果失败,返回为null */ public static String get(String url) { try { HttpClient client = HttpClientBuilder.create().build(); URI uri = new URI(url); HttpGet get = new HttpGet(uri); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == 200) { StringBuffer buffer = new StringBuffer(); InputStream in = null; try { in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in,Charset)); String line = null; while ((line = reader.readLine()) != null) { buffer.append(line); } } finally { if (in != null) in.close(); } return buffer.toString(); } else { return null; } } catch (Exception e) { e.printStackTrace(); } return null; } }
小程序中,使用wx.request(url:'https://域名/api/wx/session',....),就可以拿到一个JSON对象,其中有openid。
发表评论
-
gradle编译错误:Could not find method compile() for arguments
2020-09-19 10:50 18189编译(IDEA+Gradle)一个别人的工程,出现一个 ... -
netty心跳检查之UDP篇
2019-09-15 08:50 2131部分UDP通信场景中,需要客户端定期发送心跳信息,以获取终 ... -
解决tomcat部署两个SpringBoot应用提示InstanceAlreadyExistsException
2019-06-30 11:49 3175两个SpringBoot应用部署在一个Tomcat中,单独 ... -
Eclipse配置MyBatis代码自动化功能
2019-06-29 10:16 16441.安装插件 Eclipse中,Help->Ecli ... -
vue.js中使用qrcode生成二维码
2019-05-20 00:00 7562一、安装包 npm install qrcodejs2 --s ... -
MySQL插入数据报错: Incorrect string value: '\xFD\xDE'
2019-03-31 23:19 1174我MySQL数据库用的uft-8字符集,插入数据一直很正常 ... -
vue自定义组件并双向绑定属性
2019-03-08 22:46 3209做了两个子组件,原理基本一样,一个是使用原生的select ... -
vue-router简单示例
2019-03-05 00:32 1090写个基本完整、稍有借鉴意义的示例,防止自己忘记。 &l ... -
“联通充值系统繁忙”轻松应对
2019-02-06 11:03 3907大过年的,联通充个值一直报“充值系统繁忙”。昨天晚上试了几 ... -
electron.js数据库应用---导航菜单(element-ui+mysql)
2019-02-05 21:33 2296一、环境搭建 略, ... -
electron.js数据库应用---入门(mysql+element-ui)
2019-01-27 23:19 7314我的机器:Windows10,64 ... -
SpringMVC 在controller层中注入成员变量request,是否线程安全
2018-12-17 21:17 2694@RestController public class ... -
VueJS 组件参数名命名与组件属性转化
2018-12-03 00:00 2007转自:https://www.cnblogs.com/meiy ... -
vue-resource拦截器实现token发送及检验自动化
2018-11-16 22:38 3042用了很长时间vue-resource,最近思考$http发 ... -
element-ui试用手记
2018-10-29 20:25 1687element-ui、iviewui都以vue.js为基础 ... -
iviewui中表格控件中render的使用示例
2018-07-07 16:46 9718示例了如何在表格中显示按钮,如何将代码转化为文字。 i ... -
Tomcat错误“Alias name tomcat does not identify a key entry”解决
2018-07-05 21:39 6190申请到了阿里云的证书后,下载、按照说明生成jks格式证书、 ... -
阿里云免费证书“fileauth.txt内容配置错误”解决
2018-07-05 20:43 5201最近研究微信小程序开发,上阿里云申请了个证书,使用文件验证 ... -
springboot2.0跨域配置
2018-07-04 22:11 5239springboot2.0跨域配置: 一、代码 ... -
SpringBoot2.0启用https协议
2018-06-28 23:00 7688SpringBoot2.0之后,启用https协议的方式与 ...
相关推荐
java解析的源码和小程序的源码,自己从项目中剥离出来的,肯定能用,有疑问私信,微信小程序获取各类信息(openid、session_key以及用户基本信息、解密手机号)
微信小程序根据wx.login得到的code从Csharp后台获取openid和session_key(后台实现了4种方式调用jscode2session)
springboot+微信小程序用codeid换取openid。所用方法简单,单独进行封装,方便二次使用
微信小程序开发获取用户openid,服务端代码。前端返回json格式解析。
微信小程序获取用户openId的实现方法 前端: wx.login({ success: function (res) { res.code }) 获取到code后,传到后台, 然后请求微信接口 ...
基于微信小程序+springboot的社区疫情防控管理系统项目源码 软件架构 采用微信开发者工具+Idea开发 jdk1.8 Mysql版本至少为 5.7.36 安装教程 将Cm_system导入到Idea中 将wx_project导入到微信开发者工具中 新建...
java获取微信小程序openid,包括其他很多小程序操作,下载即可运行
1.在小程序中获取用户的登录信息,成功后可以获取到用户的code值 2.在用户自己的服务端请求微信获取用户openid接口,成功后可以获取用户的openid值 微信官方api地址:点击打开链接 3.在用户自己的服务器上面请求...
我们知道openid是微信用户验证的重要标识,支付功能严重依赖这个东西,之前我们做微信支付的时候是通过在微信客户端直接调用官方接口,通过传code参数来调用,下面这样 getOpenId(){ //获取用户的openid let _this=...
主要介绍了微信小程序 springboot后台如何获取用户的openid,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了微信公众平台实现获取用户OpenID的方法,需要开发人员经过微信授权后获取高级接口才能使用此功能,用户OpenID对于微信公众平台建设有着非常广泛的用途,需要的朋友可以参考下
微信小程序提醒与获取openId.docx
微信h5授权获取用户openId的方法和步骤,用于用户登录和注册,代码已经封装好,下载就能用
微信小程序获取OpenID +php后端完整源代码。里面包含完整的PHP后台程序和微信小程序端程序,简单修改里面的配置信息就可以运行。
如何将code变成openid和session_key 抛出错误异常和派发令牌 一:微信登录换取token的流程 多说无益,直接上图 小程序获取token.png 这里介绍的主要是后端开发的流程,前端的不是本文介绍的重点 二:如何将cod
如何获取小程序的用户openid和unionid,以及后台C#代码
主要介绍了 C#微信小程序服务端获取用户解密信息实例代码的相关资料,需要的朋友可以参考下
主要介绍了微信小程序授权获取用户详细信息openid的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
WXService Java根据code换取微信小程序openId,accessToken以及微信小程序码二维码 String param = "appid=" + ydAppId + "&secret=" + ydSecret + "&js_code=" + code + "&grant_type=authorization_code"; // ...