微信小程序微信登錄的實(shí)現(xiàn)方法詳解(JAVA后臺)
官方文檔:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
本文主要記錄小程序?qū)崿F(xiàn)微信登陸功能,后端為Java開發(fā)。
在開發(fā)之前我們先看一下官方提供的時序圖,了解一下我們的大致開發(fā)流程:
大致了解流程之后,我們便可以著手開發(fā)了。
1. 前提
一個可以測試的微信小程序
此微信小程序的APPID和APPscret(至開發(fā)者后臺獲?。?/p>
2. 開發(fā)流程
從時序圖我們可以了解到流程大致分為兩步:
- 小程序端獲取code后傳給Java后臺
- Java后臺獲取code后向微信后臺接口獲取open_id
2.1 小程序端
在微信小程序的前端調(diào)用wx.login()獲取一個code,這個code就像是我們?nèi)ノ⑿藕笈_服務(wù)器獲取用戶信息的一個鑰匙,微信通過獲取這個code的過程給用戶一個選擇是否授權(quán)的選擇,如果用戶選擇了授權(quán)就會返回一個code。這個code是一次性的,也是有時限的。由于我在Java后臺進(jìn)行了一次數(shù)據(jù)校驗(yàn),所以我也會從getUserInfo接口中獲取相關(guān)數(shù)據(jù)。代碼如下:
2.2 Java后端接口
后端的流程我將其大致分為如下幾點(diǎn):
- 接收小程序發(fā)送的code
- 開發(fā)者服務(wù)器 登錄憑證校驗(yàn)接口 appi + appsecret + code
- 接收微信接口服務(wù) 獲取返回的參數(shù)
- 校驗(yàn)簽名 小程序發(fā)送的簽名signature與服務(wù)器端生成的簽名signature2 = sha1(rawData + sessionKey)
- 根據(jù)返回的User實(shí)體類,判斷用戶是否是新用戶,是的話,將用戶信息存到數(shù)據(jù)庫;
獲取openId
后臺接受了code以后通過建立一個http請求去訪問微信后臺服務(wù)器拉取這個用戶的openid,如果一切正常就會得到這個用戶對應(yīng)這個小程序的openid。
請求的地址:
https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
通過GET方式訪問,其中的參數(shù)分別是:
- appid:小程序的appid
- secret:小程序的appsecret
- js:小程序前端傳來的code
- grant_type:這個不用修改,表示授權(quán)的類型
請求工具類代碼如下:(APPID自行替換)
public class WechatUtil { public static JSONObject getSessionKeyOrOpenId(String code) { String requestUrl = "https://api.weixin.qq.com/sns/jscode2session"; Map<String, String> requestUrlParam = new HashMap<>(); // https://mp.weixin.qq.com/wxopen/devprofile?action=get_profile&token=164113089&lang=zh_CN //小程序appId requestUrlParam.put("appid", WXConstant.APPID); //小程序secret requestUrlParam.put("secret", WXConstant.SECRET); //小程序端返回的code requestUrlParam.put("js_code", code); //默認(rèn)參數(shù) requestUrlParam.put("grant_type", "authorization_code"); //發(fā)送post請求讀取調(diào)用微信接口獲取openid用戶唯一標(biāo)識 JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doPost(requestUrl, requestUrlParam)); return jsonObject; } }
HTTP工具類如下:
需要添加相關(guān)依賴。
<!-- http請求工具包依賴 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
public class HttpClientUtil { public static String doGet(String url, Map<String, String> param) { // 創(chuàng)建Httpclient對象 CloseableHttpClient httpclient = HttpClients.createDefault(); String resultString = ""; CloseableHttpResponse response = null; try { // 創(chuàng)建uri URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : param.keySet()) { builder.addParameter(key, param.get(key)); } } URI uri = builder.build(); // 創(chuàng)建http GET請求 HttpGet httpGet = new HttpGet(uri); // 執(zhí)行請求 response = httpclient.execute(httpGet); // 判斷返回狀態(tài)是否為200 if (response.getStatusLine().getStatusCode() == 200) { resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (response != null) { response.close(); } httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } public static String doGet(String url) { return doGet(url, null); } public static String doPost(String url, Map<String, String> param) { // 創(chuàng)建Httpclient對象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 創(chuàng)建Http Post請求 HttpPost httpPost = new HttpPost(url); // 創(chuàng)建參數(shù)列表 if (param != null) { List<NameValuePair> paramList = new ArrayList<>(); for (String key : param.keySet()) { paramList.add(new BasicNameValuePair(key, param.get(key))); } // 模擬表單 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); httpPost.setEntity(entity); } // 執(zhí)行http請求 response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } public static String doPost(String url) { return doPost(url, null); } public static String doPostJson(String url, String json) { // 創(chuàng)建Httpclient對象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String resultString = ""; try { // 創(chuàng)建Http Post請求 HttpPost httpPost = new HttpPost(url); // 創(chuàng)建請求內(nèi)容 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); // 執(zhí)行http請求 response = httpClient.execute(httpPost); resultString = EntityUtils.toString(response.getEntity(), "utf-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return resultString; } }
接口代碼
具體代碼如下所示:
判斷用戶是否存在后的代碼根據(jù)自己的業(yè)務(wù)邏輯進(jìn)行修改即可。
@PostMapping("/wx/login") public R user_login(@RequestParam(value = "code", required = false) String code, @RequestParam(value = "rawData", required = false) String rawData, @RequestParam(value = "signature", required = false) String signature) { // 用戶非敏感信息:rawData // 簽名:signature JSONObject rawDataJson = JSON.parseObject(rawData); // 1.接收小程序發(fā)送的code // 2.開發(fā)者服務(wù)器 登錄憑證校驗(yàn)接口 appi + appsecret + code JSONObject SessionKeyOpenId = WechatUtil.getSessionKeyOrOpenId(code); // 3.接收微信接口服務(wù) 獲取返回的參數(shù) String openid = SessionKeyOpenId.getString("openid"); String sessionKey = SessionKeyOpenId.getString("session_key"); // 4.校驗(yàn)簽名 小程序發(fā)送的簽名signature與服務(wù)器端生成的簽名signature2 = sha1(rawData + sessionKey) String signature2 = DigestUtils.sha1Hex(rawData + sessionKey); if (!signature.equals(signature2)) { return R.error().message("簽名校驗(yàn)失敗"); } // 5.根據(jù)返回的User實(shí)體類,判斷用戶是否是新用戶,是的話,將用戶信息存到數(shù)據(jù)庫; LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery(); lqw.eq(User::getOpenId, openid); User user = userService.getOne(lqw); if (user == null) { // 用戶信息入庫 String nickName = rawDataJson.getString("nickName"); String avatarUrl = rawDataJson.getString("avatarUrl"); user = new User(); user.setOpenId(openid); user.setAvatar(avatarUrl); user.setNickName(nickName); userService.save(user); } return R.ok().data(user); }
總結(jié)
到此這篇關(guān)于微信小程序微信登錄實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)微信小程序微信登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java后端接入微信小程序登錄功能(登錄流程)
- java實(shí)現(xiàn)微信掃碼登錄第三方網(wǎng)站功能(原理和代碼)
- 詳解java實(shí)現(xiàn)簡單掃碼登錄功能(模仿微信網(wǎng)頁版掃碼)
- Java中基于Shiro,JWT實(shí)現(xiàn)微信小程序登錄完整例子及實(shí)現(xiàn)過程
- 使用weixin-java-tools完成微信授權(quán)登錄、微信支付的示例
- java實(shí)現(xiàn)微信小程序登錄態(tài)維護(hù)的示例代碼
- 第三方網(wǎng)站微信登錄java代碼實(shí)現(xiàn)
- java實(shí)現(xiàn) 微博登錄、微信登錄、qq登錄實(shí)現(xiàn)代碼
- Java實(shí)現(xiàn)微信登錄并獲取用戶信息功能(開發(fā)流程)
相關(guān)文章
SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決
這篇文章主要介紹了SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09SpringBoot 整合 Netty 多端口監(jiān)聽的操作方法
Netty提供異步的、基于事件驅(qū)動的網(wǎng)絡(luò)應(yīng)用程序框架,用以快速開發(fā)高性能、高可靠性的網(wǎng)絡(luò) IO 程序,是目前最流行的 NIO 框架,這篇文章主要介紹了SpringBoot 整和 Netty 并監(jiān)聽多端口,需要的朋友可以參考下2023-10-10Java_int、double型數(shù)組常用操作工具類(分享)
下面小編就為大家?guī)硪黄狫ava_int、double型數(shù)組常用操作工具類(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08nacos使用占位符${}進(jìn)行參數(shù)配置的方法
這篇文章主要介紹了nacos如何使用占位符${}進(jìn)行參數(shù)配置,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12java程序設(shè)計語言的優(yōu)勢及特點(diǎn)
在本篇文章里小編給大家分享的是一篇關(guān)于java程序設(shè)計語言的優(yōu)勢及特點(diǎn)的內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-02-02