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

1. 用戶在小程序中選擇使用微信授權登錄功能。
2. 小程序調(diào)用 `wx.login` 接口,向微信服務器發(fā)起登錄請求。
3. 微信服務器驗證小程序的合法性,如果合法,會返回一個臨時登錄憑證 **code** 給小程序。
4. 小程序將收到的 **code** 發(fā)送到后臺服務器。
5. 后臺服務器接收到 **code** 后,使用自己的 **AppID** 和 **AppSecret**,以及收到的 **code**,調(diào)用微信接口向微信服務器發(fā)送請求,獲取用戶的唯一標識 **openid** 和會話密鑰 **session_key**。
6. 后臺服務器根據(jù) **openid** 和 **session_key**,進行用戶身份的驗證和處理,可以將用戶信息存儲在后臺數(shù)據(jù)庫中。
7. 后臺服務器將驗證結果返回給小程序。
8. 小程序根據(jù)收到的驗證結果,進行相應的登錄狀態(tài)處理,如登錄成功后,顯示用戶相關的個性化內(nèi)容。
**需要注意的是**:
- 小程序在獲取到 **code** 后,必須將其發(fā)送到后臺服務器進行二次驗證和處理,因為直接使用 **code** 進行用戶登錄是不安全的。
- 通過后臺服務器的驗證,可以確保用戶的身份和信息安全。
- 同時,后臺服務器也可以擁有更多的靈活性和自定義功能,如用戶信息的持久化存儲和一些業(yè)務邏輯的處理。
具體實現(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 # 小程序微信公眾平臺appId
secret: 039b0b4bde17b9358064fcacc56b7fc3 # 小程序微信公眾平臺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);
//微信號標識
System.out.println("sessionInfo.getOpenid() = " + sessionInfo.getOpenid());
return sessionInfo;
}
}前端核心代碼
// 登錄
wx.login({
success: res => {
console.error(res.code)
// 發(fā)送 res.code 到后臺換取 openId, sessionKey, unionId
console.error(res)
// oauth code
const code = res.code;
// 請求開發(fā)者服務器,獲取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)
}
})
},
})總結
到此這篇關于weixin-java-miniapp微信小程序登陸具體實現(xiàn)的文章就介紹到這了,更多相關weixin-java-miniapp微信小程序登陸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于Google發(fā)布的JavaScript代碼規(guī)范你要知道哪些
代碼規(guī)范并不是一種編寫正確JavaScript代碼的規(guī)則,而是為了保持源代碼編寫模式一致的一種選擇。這篇文章給大家介紹了關于Google發(fā)布的JavaScript代碼規(guī)范你要知道哪些,感興趣的朋友一起看看吧2018-04-04
URLSearchParams快速解析URL查詢參數(shù)實現(xiàn)
這篇文章主要為大家介紹了URLSearchParams快速解析URL查詢參數(shù)實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06

