weixin-java-miniapp微信小程序登陸具體實(shí)現(xiàn)

1. 用戶在小程序中選擇使用微信授權(quán)登錄功能。
2. 小程序調(diào)用 `wx.login` 接口,向微信服務(wù)器發(fā)起登錄請(qǐng)求。
3. 微信服務(wù)器驗(yàn)證小程序的合法性,如果合法,會(huì)返回一個(gè)臨時(shí)登錄憑證 **code** 給小程序。
4. 小程序將收到的 **code** 發(fā)送到后臺(tái)服務(wù)器。
5. 后臺(tái)服務(wù)器接收到 **code** 后,使用自己的 **AppID** 和 **AppSecret**,以及收到的 **code**,調(diào)用微信接口向微信服務(wù)器發(fā)送請(qǐng)求,獲取用戶的唯一標(biāo)識(shí) **openid** 和會(huì)話密鑰 **session_key**。
6. 后臺(tái)服務(wù)器根據(jù) **openid** 和 **session_key**,進(jìn)行用戶身份的驗(yàn)證和處理,可以將用戶信息存儲(chǔ)在后臺(tái)數(shù)據(jù)庫(kù)中。
7. 后臺(tái)服務(wù)器將驗(yàn)證結(jié)果返回給小程序。
8. 小程序根據(jù)收到的驗(yàn)證結(jié)果,進(jìn)行相應(yīng)的登錄狀態(tài)處理,如登錄成功后,顯示用戶相關(guān)的個(gè)性化內(nèi)容。
**需要注意的是**:
- 小程序在獲取到 **code** 后,必須將其發(fā)送到后臺(tái)服務(wù)器進(jìn)行二次驗(yàn)證和處理,因?yàn)橹苯邮褂?**code** 進(jìn)行用戶登錄是不安全的。
- 通過后臺(tái)服務(wù)器的驗(yàn)證,可以確保用戶的身份和信息安全。
- 同時(shí),后臺(tái)服務(wù)器也可以擁有更多的靈活性和自定義功能,如用戶信息的持久化存儲(chǔ)和一些業(yè)務(wù)邏輯的處理。
具體實(shí)現(xiàn)
后端
1 pom.xml,yml文件配置
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>4.5.5.B</version>
</dependency>
---------------------------------------
wx:
miniapp:
appId: wxcb96214be4598129 # 小程序微信公眾平臺(tái)appId
secret: 039b0b4bde17b9358064fcacc56b7fc3 # 小程序微信公眾平臺(tái)api秘鑰
---------------------------------------2 屬性注入,配置文件編寫
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@Data
@ConfigurationProperties(prefix = "wx.miniapp")
public class WxConfigProperties {
private String appId;
private String secret;
}
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class WxConfigOperator {
@Autowired
private WxConfigProperties wxConfigProperties;
@Bean
public WxMaService wxMaService() {
//微信小程序id和秘鑰
WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
wxMaConfig.setAppid(wxConfigProperties.getAppId());
wxMaConfig.setSecret(wxConfigProperties.getSecret());
WxMaService service = new WxMaServiceImpl();
service.setWxMaConfig(wxMaConfig);
return service;
}
}3 小程序登陸接口
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@CrossOrigin
public class CustomerInfoController {
@Autowired
private WxMaService wxMaService;
// 微信小程序登錄接口
@RequestMapping("/wx_login/[code]")
public Object login(@PathVariable String code) throws WxErrorException {
WxMaJscode2SessionResult sessionInfo =
wxMaService.getUserService().getSessionInfo(code);
//微信號(hào)標(biāo)識(shí)
System.out.println("sessionInfo.getOpenid() = " + sessionInfo.getOpenid());
return sessionInfo;
}
}前端核心代碼
// 登錄
wx.login({
success: res => {
console.error(res.code)
// 發(fā)送 res.code 到后臺(tái)換取 openId, sessionKey, unionId
console.error(res)
// oauth code
const code = res.code;
// 請(qǐng)求開發(fā)者服務(wù)器,獲取oauth token信息
wx.request({
url: `http://localhost:8080/wx_login/$[code]`,
header:{'content-type':'application/json'},
method: 'POST',
success(res){
const data = res.data;
console.error(data)
}
})
},
})總結(jié)
到此這篇關(guān)于weixin-java-miniapp微信小程序登陸具體實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)weixin-java-miniapp微信小程序登陸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript取消文本選定的實(shí)現(xiàn)代碼
最近在做拖動(dòng)布局. 發(fā)現(xiàn)有文本選定的時(shí)候, 進(jìn)行拖動(dòng)很不好看.2010-11-11
JS時(shí)間戳轉(zhuǎn)換為常用時(shí)間格式的三種方式
我們?cè)陂_發(fā)中經(jīng)常需要把時(shí)間戳轉(zhuǎn)化成日期格式,下面這篇文章主要給大家介紹了關(guān)于JS時(shí)間戳轉(zhuǎn)換為常用時(shí)間格式的三種主要方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
JavaScript實(shí)現(xiàn)放大鏡效果代碼示例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)放大鏡效果代碼示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
關(guān)于Google發(fā)布的JavaScript代碼規(guī)范你要知道哪些
代碼規(guī)范并不是一種編寫正確JavaScript代碼的規(guī)則,而是為了保持源代碼編寫模式一致的一種選擇。這篇文章給大家介紹了關(guān)于Google發(fā)布的JavaScript代碼規(guī)范你要知道哪些,感興趣的朋友一起看看吧2018-04-04
URLSearchParams快速解析URL查詢參數(shù)實(shí)現(xiàn)
這篇文章主要為大家介紹了URLSearchParams快速解析URL查詢參數(shù)實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
js+html實(shí)現(xiàn)點(diǎn)名系統(tǒng)功能
這篇文章主要為大家詳細(xì)介紹了js+html實(shí)現(xiàn)點(diǎn)名系統(tǒng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

