700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > SpringBoot中 使用WxJava SDK 实现微信小程序登录

SpringBoot中 使用WxJava SDK 实现微信小程序登录

时间:2019-03-08 17:51:19

相关推荐

SpringBoot中 使用WxJava SDK 实现微信小程序登录

概述

WxJava SDK是一个比较实用的第三方微信开发 Java SDK

github地址:/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: /wx/order/pay-notify# 商户证书文件路径# 请参考“商户证书”一节 https://pay./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配置类

@Configurationpublic 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")@Validatedpublic 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 实现微信小程序登录的方法,学会了么?在微信小程序开发中用得比较多的。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。