资讯专栏INFORMATION COLUMN

【微信开发】SpringBoot 集成微信小程序授权登录

whinc / 3922人阅读

【微信开发】SpringBoot 集成微信小程序授权登录

我这里采用了第三方的依赖,目前是最火的微信开发工具吧,WxJava

1、SprinBoot 后端

(1)准备工作

引入相关依赖

</>复制代码

  1. <dependency> <groupId>com.github.binarywanggroupId> <artifactId>weixin-java-payartifactId> <version>4.1.0version> dependency>

配置application.yml

</>复制代码

  1. # ----------------------系统配置# 业务配置pay-platform: # 微信 wx: pay: appId: secret: mchId: mchKey: keyPath: notifyUrl:

(2)相关配置类

属性类

</>复制代码

  1. import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;/** * wxpay pay properties. * * @author Binary Wang */@Data@ConfigurationProperties(prefix = "pay-platform.wx.pay")public class WxPayProperties {
  2. /**
  3. * 设置微信公众号或者小程序等的appid
  4. */
  5. private String appId;
  6. private String secret;
  7. /**
  8. * 微信支付商户号
  9. */
  10. private String mchId;
  11. /**
  12. * 微信支付商户密钥
  13. */
  14. private String mchKey;
  15. /**
  16. * 服务商模式下的子商户公众账号ID,普通模式请不要配置,请在配置文件中将对应项删除
  17. */
  18. private String subAppId;
  19. /**
  20. * 服务商模式下的子商户号,普通模式请不要配置,最好是请在配置文件中将对应项删除
  21. */
  22. private String subMchId;
  23. /**
  24. * apiclient_cert.p12文件的绝对路径,或者如果放在项目中,请以classpath:开头指定
  25. */
  26. private String keyPath;
  27. /**
  28. * 支付回调地址
  29. */
  30. private String notifyUrl;}

属性配置类

</>复制代码

  1. import com.github.binarywang.wxpay.config.WxPayConfig;import com.github.binarywang.wxpay.service.WxPayService;import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;import lombok.AllArgsConstructor;import org.apache.commons.lang3.StringUtils;import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;import org.springframework.boot.context.properties.EnableConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @author Binary Wang */@Configuration@ConditionalOnClass(WxPayService.class)@EnableConfigurationProperties(WxPayProperties.class)@AllArgsConstructorpublic class WxPayConfiguration {
  2. private WxPayProperties properties;
  3. @Bean
  4. @ConditionalOnMissingBean
  5. public WxPayService wxService() {
  6. WxPayConfig payConfig = new WxPayConfig();
  7. payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
  8. payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
  9. payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
  10. payConfig.setSubAppId(StringUtils.trimToNull(this.properties.getSubAppId()));
  11. payConfig.setSubMchId(StringUtils.trimToNull(this.properties.getSubMchId()));
  12. payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
  13. // 可以指定是否使用沙箱环境
  14. payConfig.setUseSandboxEnv(false);
  15. WxPayService wxPayService = new WxPayServiceImpl();
  16. wxPayService.setConfig(payConfig);
  17. return wxPayService;
  18. }}

(3)相关实体类

相关实体类,都可以使用json的map格式来处理,我这里是个人习惯

</>复制代码

  1. import lombok.Data;import lombok.experimental.Accessors;/** * 接口调用凭证 * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html * * @author Tellsea * @date 2021/05/20 */@Data@Accessors(chain = true)public class AccessToken {
  2. private String accessToken;
  3. /**
  4. * 凭证有效时间,单位:秒。目前是7200秒之内的值。
  5. */
  6. private Integer expiresIn;
  7. private Integer errCode;
  8. private String errMsg;}

</>复制代码

  1. import lombok.Data;import lombok.experimental.Accessors;/** * code换取openId * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html * * @author Tellsea * @date 2021/05/20 */@Data@Accessors(chain = true)public class Code2Session {
  2. private String openId;
  3. private String sessionKey;
  4. private String unionId;
  5. private Integer errCode;
  6. private String errMsg;}

</>复制代码

  1. import lombok.Data;import lombok.experimental.Accessors;/** * 微信登录 * * @author Tellsea * @date 2021/05/19 */@Data@Accessors(chain = true)public class WeiXinLogin { private String code; private String encryptedData; private String iv; private String nickName; private String avatarUrl; private Integer gender;}

</>复制代码

  1. lombok.Data;import lombok.experimental.Accessors;/** * 微信 token * * @author Tellsea * @date 2021/05/20 */@Data@Accessors(chain = true)public class WeiXinToken { public static String token;}

(4)处理后端逻辑

控制层

</>复制代码

  1. package com.ruoyi.business.appuser.controller;import cn.hutool.core.lang.Validator;import com.alibaba.fastjson.JSONObject;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.ruoyi.business.appuser.service.WeiXinService;import com.ruoyi.business.appuser.vo.wx.Code2Session;import com.ruoyi.business.appuser.vo.wx.OrderInfo;import com.ruoyi.business.appuser.vo.wx.WeiXinLogin;import com.ruoyi.common.constant.Constants;import com.ruoyi.common.constant.UserConstants;import com.ruoyi.common.core.domain.AjaxResult;import com.ruoyi.common.core.domain.entity.SysUser;import com.ruoyi.common.core.domain.model.LoginUser;import com.ruoyi.common.utils.SecurityUtils;import com.ruoyi.common.utils.ServletUtils;import com.ruoyi.framework.web.service.SysLoginService;import com.ruoyi.framework.web.service.SysPermissionService;import com.ruoyi.framework.web.service.TokenService;import com.ruoyi.system.service.ISysUserService;import com.zhhy.tool.utils.IntegerUtils;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import lombok.extern.slf4j.Slf4j;import org.apache.commons.codec.binary.Base64;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.util.CollectionUtils;import org.springframework.web.bind.annotation.*;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import java.nio.charset.StandardCharsets;import java.security.spec.AlgorithmParameterSpec;import java.util.List;import java.util.Set;/** * @author Tellsea * @date 2021/11/09 */@Slf4j@Api(value = "微信API", tags = {"微信API"})@RestController@RequestMapping("/au/weiXin")public class AuWeiXinController {
  2. @Autowired
  3. private ISysUserService userService;
  4. @Autowired
  5. private SysLoginService loginService;
  6. @Autowired
  7. private WeiXinService weiXinService;
  8. @Autowired
  9. private SysPermissionService permissionService;
  10. @Autowired
  11. private TokenService tokenService;
  12. @ApiOperation("微信用户登录")
  13. @PostMapping("login")
  14. public AjaxResult login(@RequestBody WeiXinLogin dto) {
  15. Code2Session code2Session = weiXinService.code2Session(dto.getCode());
  16. if (StringUtils.isNotEmpty(code2Session.getOpenId())) {
  17. // 解析电话号码
  18. String phoneNumber;
  19. byte[] byEncrypdata = Base64.decodeBase64(dto.getEncryptedData());
  20. byte[] byIvdata = Base64.decodeBase64(dto.getIv());
  21. byte[] bySessionkey = Base64.decodeBase64(code2Session.getSessionKey());
  22. AlgorithmParameterSpec ivSpec = new IvParameterSpec(byIvdata);
  23. try {
  24. SecretKeySpec keySpec = new SecretKeySpec(bySessionkey, "AES");
  25. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  26. cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
  27. String phoneResult = new String(cipher.doFinal(byEncrypdata), StandardCharsets.UTF_8);
  28. JSONObject phoneObject = JSONObject.parseObject(phoneResult);
  29. phoneNumber = phoneObject.getString("phoneNumber");
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. return AjaxResult.error("手机号码解密失败");
  33. }
  34. // 根据openId查询是否存在这个用户
  35. List<SysUser> list = userService.list(new LambdaQueryWrapper<SysUser>().eq(SysUser::getOpenId, code2Session.getOpenId())
  36. .or().eq(SysUser::getUserName, phoneNumber).or().eq(SysUser::getPhonenumber, phoneNumber));
  37. AjaxResult ajax = AjaxResult.success();
  38. if (CollectionUtils.isEmpty(list)) {
  39. // 添加新用户
  40. String defaultPassword = "111111";
  41. SysUser user = new SysUser()
  42. .setOpenId(code2Session.getOpenId())
  43. .setUserName(phoneNumber)
  44. .setNickName(dto.getNickName())
  45. .setDeptId(0L)
  46. .setPassword(defaultPassword)
  47. .setPhonenumber(phoneNumber)
  48. .setAvatar(dto.getAvatarUrl());
  49. if (IntegerUtils.eq(dto.getGender(), 0)) {
  50. user.setSex("2");
  51. } else if (IntegerUtils.eq(dto.getGender(), 1)) {
  52. user.setSex("0");
  53. } else if (IntegerUtils.eq(dto.getGender(), 2)) {
  54. user.setSex("1");
  55. }
  56. if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))) {
  57. return AjaxResult.error("手机号已被注册");
  58. } else if (Validator.isNotEmpty(user.getPhonenumber())
  59. && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user))) {
  60. return AjaxResult.error("手机号已被使用");
  61. }
  62. user.setCreateBy(SecurityUtils.getUsername());
  63. user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
  64. // 默认给角色用户
  65. user.setRoleIds(new Long[]{1L});
  66. userService.insertUser(user);
  67. String token = loginService.login(user.getUserName(), defaultPassword);
  68. ajax.put(Constants.TOKEN, token);
  69. return ajax;
  70. } else if (list.size() == 1) {
  71. // 更新用户信息:这里查询出的一个信息,可能是openId、userName、phonenumber三个字段其中某个查出来的
  72. SysUser sysUser = list.get(0);
  73. sysUser.setNickName(dto.getNickName());
  74. sysUser.setAvatar(dto.getAvatarUrl());
  75. if (IntegerUtils.eq(dto.getGender(), 0)) {
  76. sysUser.setSex("2");
  77. } else if (IntegerUtils.eq(dto.getGender(), 1)) {
  78. sysUser.setSex("0");
  79. } else if (IntegerUtils.eq(dto.getGender(), 2)) {
  80. sysUser
  81. 云服务器
  82. GPU云服务器
  83. 微信小程序免费开发
  84. 微信小程序怎样开发
  85. 微信小程序的开发
  86. 微信小程序怎么开发
  87. 文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

  88. 转载请注明本文地址:https://www.ucloud.cn/yun/123142.html

相关文章

  • 优雅解决微信程序授权登录需要button触发

    摘要:优雅解决微信小程序授权登录需要触发聊一聊最近的一个项目,这个项目是一个收书售书的小程序,有商城专栏信息发布论坛等功能。微信不会把的有效期告知开发者。 优雅解决微信小程序授权登录需要button触发 聊一聊最近的一个项目,这个项目是一个收书、售书的小程序,有商城、专栏、信息发布论坛等功能。虽然不是面向所有用户,但要求无论用户是否授权都皆可使用,但同时也要求部分功能对不授权的用户限制开放。...

    plus2047 评论0 收藏0
  • 微信程序开发中的二三事之网易云信IMSDK DEMO

    摘要:传统的网页编程采用的三剑客来实现,在微信小程序中同样有三剑客。观察者模式不难实现,重点是如何在微信小程序中搭配其特有的生命周期来使用。交互事件传统的事件传递类型有冒泡型与捕获型,微信小程序中自然也有。 本文由作者邹永胜授权网易云社区发布。 简介为了更好的展示我们即时通讯SDK强悍的能力,网易云信IM SDK微信小程序DEMO的开发就提上了日程。用产品的话说就是: 云信 IM 小程序 S...

    weij 评论0 收藏0
  • 微信程序授权登录、解密unionId出错

    摘要:注没有在微信开放平台做开发者资质认证的就不要浪费时间了,没认证无法获取,认证费用元年,微信授权登录流程第一步获取用户临时登录凭证第二步获取加密过的数据和解密参数第三步把步骤一二中的传到开发者自己服务端第三步服务端获取到之后用方法请求如下微信 注:没有在微信开放平台做开发者资质认证的就不要浪费时间了,没认证无法获取unionId,认证费用300元/年,emmmm.... 微信授权登录流程...

    tinysun1234 评论0 收藏0
  • 微信程序集成 Jenkins

    摘要:总结本文以微信小程序常规的发布流程为切入点,循序渐进地介绍了如何集成实现微信小程序预览上传功能。 showImg(https://raw.githubusercontent.com/yingye/Blog/master/images/wechat-jenkins.png); 本文首发于 https://github.com/yingye/Blo... ,欢迎各位关注我的Blog,正文以...

    young.li 评论0 收藏0

发表评论

0条评论

whinc

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<