springboot使用wx-java-miniapp实现微信登录

 导入Maven:

        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>wx-java-miniapp-spring-boot-starter</artifactId>
            <version>4.6.1.B</version>
        </dependency>

yaml和properties配置文件:

appid查看:
自己的在微信小程序管理后台,设置》基本设置》账号信息。 或 开发》开发管理》开发设置》开发者ID(可重置 AppSecret 小程序密钥

properties:

wx.miniapp.appid=你的小程序appid
wx.miniapp.secret=你的小程序密钥

yaml:

wx:
  miniapp:
    appid: xxx
    secret: xxx

 现在能获取的用户信息就只有呢称(nickName)和头像(avatarUrl),根据自己需求看需不要保存用户信息。openid这个建议保存,不要暴露在前端。

{
    "nickName": "xxxx",
    "gender": 0,
    "language": "zh_CN",
    "city": "",
    "province": "",
    "country": "",
    "avatarUrl": "头像"
}
    /**
     *
     * @param appid 小程序appid
     * @param code 前端wx.login获取的code
     * @return
     */
   
    @GetMapping("/login")
    public R<Object> login(@PathVariable String appid, String code) {
        if (StrUtil.isBlank(code)) {
            return R.error(RC112.getCode(), "登录凭证不能为空");
        }
        if (!wxMaService.switchover(appid)) {
            return R.error(RC112.getCode(), String.format("未找到对应appid=[%s]的配置,请核实!", appid));
        }
        try {
            WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
         //下面做自己的业务逻辑,大概就是去数据库查询信息存不存在,不存在创建用户信息,保存openid
            log.info(session.getSessionKey());
            log.info(session.getOpenid());
        } catch (WxErrorException e) {
            log.error(e.getMessage(), e);
            return R.error(200,e.getMessage());
        } finally {
            WxMaConfigHolder.remove();//清理ThreadLocal
        }
        return R.error(RC500.getCode(),RC500.getMsg());
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 是一个开源的 Java 开发框架,它简化了基于 Java 的应用程序的开发和部署过程。而 wx-java 是一个基于 Spring Framework 开发的微信公众号开发工具包,它提供了一系列简化了微信公众号开发的功能。 在 Spring Boot 中集成 wx-java 插件实例主要包括以下几个步骤: 1. 创建一个新的 Spring Boot 项目。 2. 在项目的 pom.xml 文件中添加依赖,引入 wx-java 插件的相关包,例如: ```xml <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>x.x.x</version> </dependency> ``` 注意要将 x.x.x 替换为最新的 wx-java 版本号。 3. 创建一个配置类,用于配置 wx-java 的相关配置信息,例如: ```java @Configuration public class WxConfig { @Value("${wx.mp.appId}") // 从配置文件中读取相关配置 private String appId; @Value("${wx.mp.appSecret}") private String appSecret; @Bean public WxMpInMemoryConfigStorage wxMpConfigStorage() { WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage(); config.setAppId(appId); config.setSecret(appSecret); // 设置其他配置信息,例如 Token、AES Key 等 return config; } @Bean public WxMpService wxMpService(WxMpInMemoryConfigStorage config) { WxMpServiceImpl service = new WxMpServiceImpl(); service.setWxMpConfigStorage(config); return service; } } ``` 4. 编写业务逻辑代码,使用 wx-java 提供的功能进行微信公众号开发,例如: ```java @RestController @RequestMapping("/wechat") public class WechatController { @Autowired private WxMpService wxMpService; @GetMapping("/accessToken") public String getAccessToken() throws WxErrorException { // 调用 wx-java 的相关方法获取 AccessToken String accessToken = wxMpService.getAccessToken(); return accessToken; } // 其他相关业务逻辑代码... } ``` 通过以上步骤,我们成功在 Spring Boot 项目中集成了 wx-java 插件,可以方便地进行微信公众号开发。当然,具体的实例还需要根据实际需求进行进一步的配置和开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值