SpringBoot整合微信小程序登录获取手机号并解密

编程入门 行业动态 更新时间:2024-10-11 11:17:32

SpringBoot整合微信小程序登录获取<a href=https://www.elefans.com/category/jswz/34/1770077.html style=手机号并解密"/>

SpringBoot整合微信小程序登录获取手机号并解密

SpringBoot+微信小程序

 文章目录:

  • 一、小程序登录获取手机号的流程
  • 二、pom导入所需的依赖包
  • 三、接收微信小程序的参数
  • 四、后端发请求的util工具方法
  • 五、获取手机号的接口
  • 六、请求接口获取的phone_info信息

一、小程序登录获取手机号的流程

1.前端请求getPhoneNumber方法获取code传给后端接口;

2.后端接口通过配置的appid、secretKey请求接口;


3.后端通过参数code和参数access_token,去请求接口

实例:  。

二、导入依赖

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.4.0</version>
</dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional>
</dependency>

三、在yml配置微信小程序id和密钥

wx:open:app_id: *********app_secret: *******************

四、接收参数

@Data
public class WeChatPhone{// getPhoneNumber接口返回的codeprivate String code;// 小程序的appid(一般是在程序中配置,不需要前端传参)private String appid;// 小程序的secretKey(一般是在程序中配置,不需要前端传参)private String secretKey;
}

五、请求接口获取的phone_info信息

@Data
public class WeChatPhoneInfo {// 用户绑定的手机号(国外手机号会有区号)private String phoneNumber;// 没有区号的手机号private String purePhoneNumber;// 区号private String countryCode;// 数据水印private String watermark;
}

六、后端发请求的util工具方法

/*** 请求微信接口服务,获取小程序全局唯一后台接口调用凭据(access_token)* .getAccessToken.html** @param appid* @param secretKey* @return*/
public static JSONObject getAccessToken(String appid, String secretKey) {String result = null;try {String baseUrl = "";HashMap<String, Object> requestParam = new HashMap<>();// 小程序 appIdrequestParam.put("grant_type", "client_credential");// 小程序唯一凭证idrequestParam.put("appid", appid);// 小程序 appSecret(小程序的唯一凭证密钥)requestParam.put("secret", secretKey);// 发送GET请求读取调用微信接口获取openid用户唯一标识result = HttpUtil.get(baseUrl, requestParam);} catch (Exception e) {e.printStackTrace();}return JSONUtil.parseObj(result);
}/*** 请求微信接口服务,用code换取用户手机号(每个code只能使用一次,code的有效期为5min)* .getPhoneNumber.html** @param code* @param accessToken* @return*/
public static JSONObject getPhoneNumber(String code, String accessToken) {String result = null;try {// 接口调用凭证:accessTokenString baseUrl = "=" + accessToken;HashMap<String, Object> requestParam = new HashMap<>();// 手机号调用凭证requestParam.put("code", code);// 发送post请求读取调用微信接口获取openid用户唯一标识String jsonStr = JSONUtil.toJsonStr(requestParam);HttpResponse response = HttpRequest.post(baseUrl).header(Header.CONTENT_ENCODING, "UTF-8")// 发送json数据需要设置contentType.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded").body(jsonStr).execute();if (response.getStatus() == HttpStatus.HTTP_OK) {result = response.body();}} catch (Exception e) {e.printStackTrace();}return JSONUtil.parseObj(result);
}

七、获取手机号的接口

@RestController
@RequestMapping("/login")
public class WeChatUserLoginController {/*** 用前端请求接口获取的code换取用户手机号* 前端需要请求的接口:.html* @param weChatPhone* @return*/@PostMapping("/phone")public String getPhone(@RequestBody WeChatPhone weChatPhone){/*设置Appid和SecretKey*/weChatPhone.setAppid(ConstantPropertiesUtil.WX_OPEN_APP_ID);weChatPhone.setSecretKey(ConstantPropertiesUtil.WX_OPEN_APP_SECRET);// 1.请求微信接口服务,获取accessTokenJSONObject accessTokenJson = WeChatUtil.getAccessToken(weChatPhone.getAppid(), weChatPhone.getSecretKey());String accessToken = accessTokenJson.get("access_token",String.class);// 2.请求微信接口服务,获取用户手机号信息JSONObject phoneNumberJson = WeChatUtil.getPhoneNumber(weChatPhone.getCode(), accessToken);WeChatPhoneInfo phoneInfo = phoneNumberJson.get("phone_info", WeChatPhoneInfo.class);System.out.println(phoneInfo);return phoneInfo.getPurePhoneNumber();}

总结:

如果有不足的地方,希望大家积极指正,谢谢!!!

觉得帖子写的不错并能解决你的问题的话 点个赞加关注吧

更多推荐

SpringBoot整合微信小程序登录获取手机号并解密

本文发布于:2024-02-07 06:16:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:手机号   程序   SpringBoot   微信小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!