SpringBoot中,使用WxJava SDK 实现微信小程序登录
概述
WxJava SDK是一个比较实用的第三方微信开发 Java SDK
github地址:https://github.com/Wechat-Group/WxJava
SpringBoot项目中使用WxJava SDK中的weixin-java-miniapp
- pom文件中加入依赖
<dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-miniapp</artifactId></dependency>
- 配置文件yml中加入配置信息
demo:# 开发者应该设置成自己的wx相关信息wx:app-id: wx60fac1f18be01481app-secret: 318ceca0f27ffeae6e6baafd3a5730bdmch-id: 123123mch-key: xxxxxxnotify-url: http://www.example.com/wx/order/pay-notify# 商户证书文件路径# 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3key-path: xxxxx
- 创建WxProperties.java
@Configuration
@ConfigurationProperties(prefix = "demo.wx")
public class WxProperties {private String appId;private String appSecret;private String mchId;private String mchKey;private String notifyUrl;private String keyPath;public String getNotifyUrl() {return notifyUrl;}public void setNotifyUrl(String notifyUrl) {this.notifyUrl = notifyUrl;}public String getMchKey() {return mchKey;}public void setMchKey(String mchKey) {this.mchKey = mchKey;}public String getAppId() {return this.appId;}public void setAppId(String appId) {this.appId = appId;}public String getAppSecret() {return appSecret;}public void setAppSecret(String appSecret) {this.appSecret = appSecret;}public String getMchId() {return mchId;}public void setMchId(String mchId) {this.mchId = mchId;}public String getKeyPath() {return keyPath;}public void setKeyPath(String keyPath) {this.keyPath = keyPath;}
}
- 创建WxConfig配置类
@Configuration
public class WxConfig {@Autowiredprivate WxProperties properties;@Beanpublic WxMaConfig wxMaConfig() {WxMaInMemoryConfig config = new WxMaInMemoryConfig();config.setAppid(properties.getAppId());config.setSecret(properties.getAppSecret());return config;}@Beanpublic WxMaService wxMaService(WxMaConfig maConfig) {WxMaService service = new WxMaServiceImpl();service.setWxMaConfig(maConfig);return service;}}
5.做完上述准备后,在接口层调试一下,创建WxAuthController.java
/*** 鉴权服务*/
@RestController
@RequestMapping("/wx/auth")
@Validated
public class WxAuthController {@Autowiredprivate WxMaService wxService;@PostMapping("login_by_weixin")public Object loginByWeixin(@RequestBody WxLoginInfo wxLoginInfo, HttpServletRequest request) {String code = wxLoginInfo.getCode();UserInfo userInfo = wxLoginInfo.getUserInfo();if (code == null || userInfo == null) {return ResponseUtil.badArgument();}String sessionKey = null;String openId = null;try {WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(code);sessionKey = result.getSessionKey();openId = result.getOpenid();} catch (Exception e) {e.printStackTrace();}if (sessionKey == null || openId == null) {return ResponseUtil.fail();}// TODO openId 获取后的业务逻辑实现,如获取用户信息或者未注册用户创建新账号等等Map<Object, Object> result = new HashMap<Object, Object>();// TODO返回数据填充return ResponseUtil.ok(result);}
}
总结
OK,以上就是SpringBoot中,使用WxJava SDK 实现微信小程序登录的方法,学会了么?在微信小程序开发中用得比较多的。
SpringBoot中,使用WxJava SDK 实现微信小程序登录相关推荐
- SpringBoot实现微信小程序登录功能
SpringBoot实现微信小程序登录 微信小程序登录流程 登录流程图 前端代码 后端代码 微信小程序登录流程 微信小程序官方文档:微信小程序官方文档 第一次学习微信小程序的登录,以前也好奇微信小程序 ...
- springBoot+微信小程序登录
微信小程序登录流程 微信小程序登录流程涉及到三个角色:小程序.开发者服务器.微信服务器 三者交互步骤如下: 第一步:小程序通过wx.login()获取code. 第二步:小程序通过wx.request ...
- 小程序---365笔记第11天---微信小程序登录接入
接入前的准备工作参考文档:微信小程序登录接入 (必做) 登录逻辑:小程序登录逻辑梳理 使用插件参考文档:https://developers.weixin.qq.com/miniprogram/dev ...
- 用Spring Boot完成微信小程序登录
使用Spring Boot完成微信小程序登录 由于微信最近的版本更新,wx.getUserInfo()的这个接口即将失效,将用wx.getUserProfile()替换,所以近期我也对自己的登录进行更 ...
- 微信小程序登录 + 基于token的身份验证
官方时序图如下: https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 图里其实说的很清 ...
- access突然需要登录_早知道早好,微信小程序登录开发需要注意的事项
最近公司要做一个企业微信的小程序,方便企业内的成员来登录,以便一些公司内的业务,只限于公司内的成员来操作,因为有微信小程序的开发经验,所以先当作微信小程序来开发了! 首先来讲一下这个企业微信小程序与微 ...
- Taro -- 微信小程序登录
Taro微信小程序登录 1.调用Taro.login()获取登录凭证code: 2.调用Taro.request()将code传到服务器: 3.服务器端调用微信登录校验接口(appid+appsecr ...
- 微信小程序登录-利用Oenid实现白名单和黑名单
微信小程序登录-利用Oenid实现白名单和黑名单 1.通过获取openid 存储到数据库中 2.结合数据库中额用户资料实现白名单和黑名单.
- php与ipa接口登录验证失败,thinkPHP5.0开发微信小程序登录接口signature验证失败
我用TP5开发微信小程序登录接口的时候,在校验签名signature那遇到了个坑,一直校验失败,找了很久才发现 原因是:tp的input方法会自动转移html字符,比如'"'转成了 & ...
最新文章
- R语言嵌套的ifelse语距:将一条If语句放在另一条If语句中,该语句作为嵌套的If else调用。If else语句允许我们根据表达式结果(TRUE或FALSE)打印不同的语句,执行不同的语句块
- 用户'NT AUTHORITY\NETWORK SERVICE' 登录失败
- 凯兑换系统服务器角色,王者荣耀新英雄凯怎么兑换
- 「递归」第8集 | 当敲代码的手开始写歌,玩跨界的程序员有多野?
- 日常问题——hadoop启动后发现namenode没有启动,但是排除了格式化过度的问题
- 13商软 《软件工程》课程设计
- win10 python 3.7 pip install tensorflow
- POJ-英语数字转化器
- 【python|opencv】cv2.imread返回None,无法正确读取图片
- Z=X+Y型概率密度的求解
- 高中计算机课程网页修改,高中信息技术教学贯彻新课改理念
- 笔记本电脑只能外放,插上耳机还是外放
- vs2022 c#调用interop.word 12.0版本也就是word2007实现首行缩进两个字符
- Xception实现动物识别(TensorFlow)
- EditText属性和一些小技巧
- 【源码】Set集合源码剖析
- DOS 61条圣经完整版
- Java实现OPC UA Client直接与PLC通讯
- chrome浏览器的默认超时时间
- DL: Basic of C/C++(to be continued)