SpringBoot 实现支持多个微信小程序的登录

在实际业务场景中,需要一个后台同时支持多个微信小程序的登录。例如,企业有多个不同业务的小程序,但希望统一在同一个后台系统里进行用户认证和数据处理。这时候,我们就需要一个灵活的方式来管理多个小程序的 appidsecret,并根据前端传递的 appidcode 获取对应的 openid 实现登录。

本文将基于 weixin-java-miniapp SDK 实现一个通用的多小程序登录模块。

一、依赖引入

pom.xml 中加入微信小程序 SDK 依赖:

xml 复制代码
<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-miniapp</artifactId>
    <version>4.7.7-20250808.182223</version>
</dependency>

二、配置文件

通过 application.yml 配置多个小程序的 appidsecret,提供给后续动态获取。

yaml 复制代码
wx:
  miniapp:
    configs:
      - appid: wxxxxxxxxxxxxxxxxxx
        secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
      - appid: wyyyyyyyyyyyyyyyyyy
        secret: yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy

三、属性类

定义配置属性类,读取配置文件中多个小程序的信息:

less 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "wx.miniapp")
public class WxMiniAppProperties {
    private List<Config> configs;
​
    @Data
    public static class Config {
        private String appid;
        private String secret;
    }
}

这样就能自动绑定 application.yml 中的配置到 WxMiniAppProperties

四、服务工厂类

编写一个工厂类 WxMiniAppServiceFactory,用来存储和获取 WxMaService 实例。 每个小程序对应一个 WxMaService,通过 appid 来区分。

后台可以根据传入的 appid 动态选择对应的 WxMaService

arduino 复制代码
@Component
public class WxMiniAppServiceFactory {
    private final Map<String, WxMaService> services = new HashMap<>();
​
    public WxMiniAppServiceFactory(WxMiniAppProperties properties) {
        for (WxMiniAppProperties.Config config : properties.getConfigs()) {
            WxMaDefaultConfigImpl wxConfig = new WxMaDefaultConfigImpl();
            wxConfig.setAppid(config.getAppid());
            wxConfig.setSecret(config.getSecret());
​
            WxMaService wxService = new WxMaServiceImpl();
            wxService.setWxMaConfig(wxConfig);
​
            services.put(config.getAppid(), wxService);
        }
    }
​
    public WxMaService getWxMaService(String appid) {
        WxMaService service = services.get(appid);
        if (service == null) {
            throw new IllegalArgumentException("未找到对应appid的配置: " + appid);
        }
        return service;
    }
}

五、登录逻辑

定义登录服务,根据前端传递的 appidcode 获取用户 openid

java 复制代码
@Service
public class WxMiniAppLoginService {
    private final WxMiniAppServiceFactory serviceFactory;
​
    public WxMiniAppLoginService(WxMiniAppServiceFactory serviceFactory) {
        this.serviceFactory = serviceFactory;
    }
​
    public String getOpenId(String appid, String code) throws Exception {
        WxMaService wxService = serviceFactory.getWxMaService(appid);
        WxMaJscode2SessionResult session = wxService.jsCode2SessionInfo(code);
        return session.getOpenid();
    }
}

调用时,只需要传入 appid 和前端 wx.login 获取的 code,就能拿到用户的唯一标识 openid

相关推荐
一 乐6 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
码事漫谈7 小时前
Protocol Buffers 编码原理深度解析
后端
码事漫谈7 小时前
gRPC源码剖析:高性能RPC的实现原理与工程实践
后端
踏浪无痕8 小时前
AI 时代架构师如何有效成长?
人工智能·后端·架构
程序员小假9 小时前
我们来说一下无锁队列 Disruptor 的原理
java·后端
武子康10 小时前
大数据-209 深度理解逻辑回归(Logistic Regression)与梯度下降优化算法
大数据·后端·机器学习
maozexijr10 小时前
Rabbit MQ中@Exchange(durable = “true“) 和 @Queue(durable = “true“) 有什么区别
开发语言·后端·ruby
源码获取_wx:Fegn089510 小时前
基于 vue智慧养老院系统
开发语言·前端·javascript·vue.js·spring boot·后端·课程设计
独断万古他化10 小时前
【Spring 核心: IoC&DI】从原理到注解使用、注入方式全攻略
java·后端·spring·java-ee
毕设源码_郑学姐10 小时前
计算机毕业设计springboot基于HTML5的酒店预订管理系统 基于Spring Boot框架的HTML5酒店预订管理平台设计与实现 HTML5与Spring Boot技术驱动的酒店预订管理系统开
spring boot·后端·课程设计