當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot + 微信小程序——登录凭证校验DEMO
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot + 微信小程序——登录凭证校验DEMO
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
基本概念
微信小程序-登錄憑證校驗:通過?wx.login?接口獲得臨時登錄憑證 code 后傳到開發(fā)者服務器調(diào)用此接口完成登錄流程。
微信小程序API
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
源代碼
package club.zstuca.myzstu.provider;import club.zstuca.myzstu.consts.Consts; import club.zstuca.myzstu.httpclient.HttpClientUtils; import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;import java.util.HashMap; import java.util.Map;/*** @Author ShenTuZhiGang* @Version 1.0.0* @Date 2020-02-10 10:59*/ @Component public class WeChatProvider {@Value("${wechat.login.appid}")private String APPID ;@Value("${wechat.login.secret}")private String SECRET;private final String WX_URL = "https://api.weixin.qq.com/sns/jscode2session";@Value("${wechat.login.grant-type}")private String grantType;public String getOpenId(String code) {String openid = "";Map<String, String> header = new HashMap<>();header.put("User-Agent", Consts.AGENT);Map<String, String> params = new HashMap<>();params.put("appid", APPID);params.put("secret", SECRET);params.put("js_code", code);params.put("grant_type", grantType);String response = HttpClientUtils.doGetRequest(WX_URL, header, params);System.out.println(response);System.out.println(APPID);openid = JSONObject.parseObject(response).getString("openid");System.out.println(openid);return openid;} }常見問題
錯誤代碼:40029
錯誤代碼:40163
參考文章?
https://bbs.csdn.net/topics/392185947
總結
以上是生活随笔為你收集整理的Spring Boot + 微信小程序——登录凭证校验DEMO的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux——Ubuntu 18.04
- 下一篇: Spring Boot——[Spring