微信网页授权步骤说明

总览

  1. 引导用户进入授权页面同意授权,获取code
  2. 通过code换取网页授权access_token(与基础支持中的access_token不同)
  3. 如果需要,开发者可以刷新网页授权access_token,避免过期(一般不需要)
  4. 通过网页授权access_token和openid获取用户基本信息(支持UnionID机制)

获取code

解释:让用户跳转到拼接后的指定地址,进行授权获取code,然后重定向到redirect_uri,重定向后的地址带有code和state参数

拼接地址:https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect,其中appid和redirect_uri需要替换,且redirect_uri需要encodeURI处理,state为可选值用于用户自定义传参。

通过code换取access_token和用户信息

这里只写Java示例,且使用weixin-java-mp依赖

引入以下依赖

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-mp</artifactId>
    <version>4.5.0</version>
</dependency>

在application.yml中添加如下参数

# 自定义微信相关配置信息
wx:
  mp:
    # 微信公众号的appid
    appId: wxajhlksfnjls777777
    # 信公众号的app secret
    secret: sds56dg4fd5sd54s5555555

创建WxMpProperties如下

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@Data
@ConfigurationProperties(prefix = "wx.mp")
public class WxMpProperties {
   

    /**
     * 设置微信公众号的appid
     */
    private String appId;

    /**
     * 设置微信公众号的app secret
     */
    private String secret;
}

创建WxMpConfiguration如下

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WxMpConfiguration {
   

    @Autowired
    private WxMpProperties wxMpProperties;


    /**
     * 微信客户端配置存储
     */
    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
   
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        // 设置微信公众号appId
        configStorage.setAppId(wxMpProperties.getAppId());
        // 设置微信公众号appSecret
        configStorage.setSecret(wxMpProperties.getSecret());
        return configStorage;
    }

    /**
     * WxMpService多个实现类 声明一个实例
     */
    @Bean
    public WxMpService wxMpService() {
   
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }
}

然后在Controller中引入WxMpService

@RestController
public class LoginController {
   
    
    @Autowired
    private WxMpService wxMpService;
    
    @PostMapping("/wx/login")
    public SysAjaxResult login(@RequestBody Map<String, String> params) {
   
        String code = MapUtils.getString(params, "code");
        if (StringUtils.isBlank(code)) {
   
            return SysAjaxResult.error("code不能为空");
        }
        try {
   
            // 获取accessToken,获取用户信息(据官方文档,下面两个接口调用频率为每分钟5万次)
            WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
            WxOAuth2UserInfo userInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);

            // 通过openid校验有没有当前用户,没有则新增
            String openid = userInfo.getOpenid();
            return AjaxResult.success();
        } catch (WxErrorException e) {
   
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
}

时效

接口名 频率限制
通过 code 换取 access_token 5 万/分钟
获取用户基本信息 5 万/分钟
刷新 access_token 10 万/分钟

相关推荐

  1. 网页授权步骤说明

    2023-12-13 21:44:03       238 阅读
  2. 授权登录获取用户openid

    2023-12-13 21:44:03       40 阅读
  3. 小程序订阅消息授权弹窗事件

    2023-12-13 21:44:03       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-13 21:44:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-13 21:44:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-13 21:44:03       82 阅读
  4. Python语言-面向对象

    2023-12-13 21:44:03       91 阅读

热门阅读

  1. 【Python】函数的定义和函数的处理

    2023-12-13 21:44:03       60 阅读
  2. NoSQL

    2023-12-13 21:44:03       33 阅读
  3. 02.jsp复习

    2023-12-13 21:44:03       47 阅读
  4. 【Android开发-27】Android中位置服务GPS的用法详解

    2023-12-13 21:44:03       51 阅读
  5. 数据库工具:AI 拍档,最佳的矢量数据库

    2023-12-13 21:44:03       66 阅读
  6. 什么是CSharp?c#?

    2023-12-13 21:44:03       58 阅读
  7. Oracle 库恢复删除数据

    2023-12-13 21:44:03       53 阅读
  8. 神经网络以及深度学习案例分析

    2023-12-13 21:44:03       59 阅读