最近对接开发微信小程序,需要获取用户的openid使用支付,所以记下这篇通用小程序授权笔记。

这里使用到开源工具Wx-Java

此致 致敬 binarywang 大佬

maven引入如下

<!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-miniapp -->
<dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-miniapp</artifactId><version>4.3.3.B</version>
</dependency>

准备参数,application配置文件需配置参数

wx:miniapp:appid: 微信官方申请的appidsecret: 微信官方申请的秘钥msgDataFormat: JSON #数据可以,默认json就ok

编写WxMaProperties.java文件获取配置文件中的字段

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;@Data
@ConfigurationProperties(prefix = "wx.miniapp")
public class WxMaProperties {/*** 设置微信小程序的appid*/private String appid;/*** 设置微信小程序的Secret*/private String secret;/*** 消息格式,XML或者JSON*/private String msgDataFormat;
}

创建配置类WxMaConfiguration配置初始化配置

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableConfigurationProperties(WxMaProperties.class)
public class WxMaConfiguration {@Beanpublic WxMaService wxMaService(WxMaProperties properties) {WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();config.setAppid(properties.getAppid());config.setSecret(properties.getSecret());config.setMsgDataFormat(properties.getMsgDataFormat());WxMaService service = new WxMaServiceImpl();service.setWxMaConfig(config);return service;}
}

controller编写授权代码

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@Api(tags = "微信模块")
@RestController
@RequestMapping("/wx/auth")
@RequiredArgsConstructor
public class WxController extends BaseController {private final WxMaService wxService;@ApiOperation("微信登陆")@GetMapping(value = "/getWxInfo", produces = "application/json")@SneakyThrows(WxErrorException.class)public WxMaJscode2SessionResult getWxInfo(@ApiParam("小程序CODE") String code){WxMaJscode2SessionResult wx = wxService.jsCode2SessionInfo(code);logger.info("请求微信授权完成=>{}",wx);return wx;}
}

非常简单,俩行代码直接搞定授权。

觉得有用的话记得给个赞,谢谢各位大佬了。

Java开发微信小程序授权登录相关推荐

  1. uniapp开发微信小程序授权登录

    最近用uniapp开发小程序,整理了一下授权登录代码.如下: <template><view class="container"><view clas ...

  2. java springboot微信小程序授权登录

    开发前提,注册小程序获取appid和secret 小程序端添加授权按钮 <button class="weui-btn" type="primary" b ...

  3. SpringBoot微信小程序授权登录

    SpringBoot微信小程序授权登录 一.appId 1.1.自己是管理者:微信公众平台,申请或登录自己的微信小程序,在开发者管理中即可看到 2.2.自己是开发者:让管理员将自己加入到小程序开发者管 ...

  4. SpringCloud 微信小程序授权登录 获取openId SessionKey【SpringCloud系列13】

    SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见. 自我提升方法推荐:神奇的早起 早上 5:00 -5:20 起床刷牙 5:30-6:00 晨练(跑步.跳绳.骑自行车.打球等等) ...

  5. 基于Uniapp+SpringBoot实现微信小程序授权登录

    手把手教你做微信小程序授权登录交互 一.uni.login请求临时code 二.uni.request向后台交换数据 三.源代码 前台:在GetUserInfo中添加接口 后台:SpringBoot后 ...

  6. 微信小程序授权登录 组件的封装

    微信小程序授权登录 组件的封装 新建components文件 写wxml文件 wxss部分 js部分 json引用 页面使用 页面js 授权登录 流程如下: 因为多个页面功能需要登录状态 所以做了个组 ...

  7. uniapp微信小程序授权登录和获取微信绑定的手机号码

    uniapp微信小程序授权登录和获取微信绑定的手机号码 弹出授权的弹框 <view class="weixinOk" @tap="getUserProfile&qu ...

  8. 微信小程序授权登录取消授权重新授权处理方法 附可用代码

    微信小程序授权登录基本是小程序的标配了,但是官方的demo,取消授权后,就不能再重新点击登录,除非重新加载小程序才可以,这下怎么办? 我们可以先在首页引导用户点击,然后跳转到一个新的页面,在新的页面进 ...

  9. 新版微信小程序授权登录流程及问题汇总(getUserProfile)

    问题来源:前不久去面试的时候有面试官问我你有自己的博客啥的吗?只能很尴尬的说没有.其实一直想有一个属于自己的博客啥的去记录自己在开发过程中遇到的问题,正好现在微信小程序比较流行,就花了两天自己搞了一个 ...

最新文章

  1. angular2 表单
  2. 腾讯海量存储与CDN的自动化运维
  3. 打印的图片不清晰_如何调节图片kb,但又不改变图片的清晰度?
  4. 计算机视觉论文-2021-09-10
  5. leetcode LRUCache题目
  6. linux网络发包性能优化
  7. flink的watermark简单理解
  8. [转载]MySQL锁机制
  9. ie和火狐的兼容性问题
  10. JavaScript简易文字对战游戏
  11. DFT信道估计步骤及实例
  12. matlab 谐振电路图,rlc串联电路谐振特性图分享
  13. 一步一步安装及配置R及Rstudio(详细图文)
  14. 苹果设备如何进入恢复模式
  15. 心理实验应用计算机的优缺点,发展心理学研究方法发展的新趋势
  16. 幻数java题_关于数组中的幻数?:数组中的幻数? -C ++
  17. 三星S95Z / S90Z OLED 电视 评测
  18. 精读《设计模式 - Adapter 适配器模式》
  19. 【每天读一点英文】叶芝诗歌《当你老了》赏析——特别喜欢的一首诗,水木年华《一生有你》歌词来源
  20. Vue刷新、跳转页面的数据保存方法: sessionStorage + Object.assign()

热门文章

  1. PPT母版怎么应用到每张幻灯片?
  2. html的绝对路径怎么选择,html的相对路径和绝对路径
  3. java 超出 xmx_java进程内存占用超过xmx设置的问题
  4. CSS 自适应布局(flex布局)
  5. Kotlin系列之Lambda编译成字节码过程完全解析
  6. 关于element-ui 中 table 鼠标悬停时高亮背景颜色的修改,添加fixed后颜色又出现问题
  7. leetcode 1005. Maximize Sum Of Array After K Negations(python)
  8. FFmpeg开发XPlay2.0播放器-04 Qt音频及OpenGLShader显示YUV
  9. 到底什么是BFC?一篇就够了
  10. 自编码器AutoEncoder解决异常检测问题