Java實(shí)現(xiàn)微信掃碼授權(quán)登錄完整步驟
一直想實(shí)現(xiàn)一下微信的掃碼登錄,現(xiàn)在終于來實(shí)踐了。
要想實(shí)現(xiàn)微信的掃碼登錄肯定是直接瀏覽微信的官方開發(fā)者文檔,從中就可以獲取一切的用法。
實(shí)現(xiàn)步驟
創(chuàng)建服務(wù)號的測試號
- 首先要創(chuàng)建一個測試號,用于微信接口的調(diào)試,創(chuàng)建測試號地址
創(chuàng)建好了之后是這樣的:
其中的 URL 是需要自己填寫的,這個是接收微信事件的回調(diào)地址,微信會給你推送事件,這個配置接口我們后面寫,token 可以隨便寫,不過后端符合微信的驗證就好了。
創(chuàng)建一個SpringBoot的項目
這里直接通過 IDEA 創(chuàng)建一個后端項目,然后引入,WxJava 的 SDK,方便進(jìn)行微信的交互,當(dāng)然也可以自己寫和微信的交互,但是太繁雜了,別人都寫好了,為什么還寫呢???
- 引入 WxJava 管理公眾號
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.7.6-20250704.154059</version>
</dependency>
- 填寫測試號的一些密鑰
wx.mp.appId=wx805bf0797a5f2911 # 必填 wx.mp.secret=72256cbeffd04ba3d592e133fe8f1f7a #必填 wx.mp.token=xwhking
- 配置使用的Bean
package com.xwhking.wxlogin.config;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WxConfig {
@Value("${wx.mp.appId}")
private String appId;
@Value("${wx.mp.secret}")
private String secret;
@Value("${wx.mp.token}")
private String token;
@Bean
public WxMpService wxMpService() {
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
config.setAppId(appId);
config.setSecret(secret);
config.setToken(token);
WxMpService service = new WxMpServiceImpl();
service.setWxMpConfigStorage(config);
return service;
}
@Bean
public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
final WxMpMessageRouter router = new WxMpMessageRouter(wxMpService);
// 這里可以添加各種消息處理器
return router;
}
}
- 編寫接口,用于校驗,在測試號頁面,啟動了后端,就可以讓微信校驗成功(前提要配置網(wǎng)絡(luò)地址,需要內(nèi)網(wǎng)穿透)
微信校驗流程圖,如下。
校驗規(guī)格如下

@RestController
@RequestMapping("/wx/callback")
public class WxController {
@Autowired
private WxMpService wxMpService;
@GetMapping
public String verify(String signature, String timestamp, String nonce, String echostr) throws WxErrorException {
boolean b = wxMpService.checkSignature(timestamp, nonce, signature); //是否滿足微信的校驗條件
if(b) {
return echostr;
}
return null;
}
}
內(nèi)網(wǎng)穿透
這里我介紹 cpolar下載 ,簡單直接能夠免費(fèi)使用 24h 的http和https的隨機(jī)域名,當(dāng)然能夠永久,不過需要購買罷了。其他的內(nèi)網(wǎng)穿透推薦ngrok , ftp 等

進(jìn)入網(wǎng)頁后點(diǎn)擊相應(yīng)的機(jī)型進(jìn)行下載就好了。
windows下載好后,直接點(diǎn)開web就好

首先要登錄,在官網(wǎng)注冊就好了,進(jìn)入后是下面的樣子

編輯一下自己網(wǎng)站端口就行了,然后到在線隧道查看地址


掃碼登錄流程介紹
背景知識,如果用戶沒有點(diǎn)擊授權(quán)信息的話,從微信掃碼只能夠獲得用戶的 OpenId,其他信息都獲取不到,但是一般我們登錄流程的話是需要用戶的昵稱,頭像,通過用戶的授權(quán),我們就可以直接獲取到了,不用用戶再次設(shè)置,這里用戶的手機(jī)號也是不能獲取到的。
- 用戶掃碼(只能或者一個帶有參數(shù)的url,要轉(zhuǎn)化成二維碼,這里沒有用前端了直接用草料二維碼進(jìn)行轉(zhuǎn)換),如果用戶掃碼了以后沒有關(guān)注公眾號,那么當(dāng)用戶點(diǎn)擊關(guān)注以后才會推送掃碼關(guān)注事件,如果用戶已經(jīng)關(guān)注了那么手機(jī)會直接定位到會話頁面
- 用測試生成url
@SpringBootTest class WxLoginApplicationTests { @Autowired private WxMpService wxService; @Test void contextLoads() throws WxErrorException { System.out.println(wxService.getQrcodeService().qrCodeCreateTmpTicket(“user_info”, 30000).getUrl()); } }
接受微信推送的事件
返回給用戶信息,消息的響應(yīng)

用戶點(diǎn)擊鏈接進(jìn)行授權(quán)
用戶打開地址,讓后端獲得權(quán)限,這里如果是域名備案了,那么會直接訪問,否則要自己到瀏覽器打開網(wǎng)址才行。
此時獲取到了用戶的信息,已經(jīng)成功進(jìn)行了關(guān)聯(lián)啦!–>即能夠獲得用戶的昵稱,openid,頭像地址,這些信息一般來說也夠了。
獲取到的信息
WxOAuth2UserInfo(openid=owO-dvkozjjzycXgtaCCgFbzxxxx, nickname=DIOIT, sex=0, city=, province=, country=, headImgUrl=https://thirdwx.qlogo.cn/mmopen/vi_32/GcAobrQM3uzlReTKGs7KtuPUibUrYXzt3hluJCXHr7DCMibYoB2viabQ2PsVPq8BfbsDwIozz1vpdAx4oib4gnTCdQ/132, unionId=null, privileges=[])
代碼實(shí)現(xiàn)
package com.xwhking.wxlogin.controller;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.crypto.SHA1;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/wx/callback")
public class WxController {
@Autowired
private WxMpService wxMpService;
@Autowired
private WxMpMessageRouter wxMpMessageRouter;
@GetMapping
public String verify(String signature, String timestamp, String nonce, String echostr) throws WxErrorException {
boolean b = wxMpService.checkSignature(timestamp, nonce, signature); //是否滿足微信的校驗條件
if(b) {
return echostr;
}
return null;
}
@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(
@RequestBody String requestBody,
@RequestParam("signature") String signature,
@RequestParam("timestamp") String timestamp,
@RequestParam("nonce") String nonce,
@RequestParam(name = "encrypt_type", required = false) String encType,
@RequestParam(name = "msg_signature", required = false) String msgSignature) throws WxErrorException {
if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
throw new IllegalArgumentException("非法請求");
}
// 處理消息
WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
System.out.println("inMessage"+inMessage);
// 如果是二維碼掃描事件
if (inMessage.getEvent().equals(WxConsts.EventType.SCAN) ||
inMessage.getEvent().equals(WxConsts.EventType.SUBSCRIBE)) {
String redirectUri = "https://519e7b4c.r9.cpolar.cn/wx/callback/auth-callback"; // 你的回調(diào)地址
String scope = "snsapi_userinfo"; // 需要獲取用戶信息
String state = "random_state_123"; // 防CSRF攻擊
// 構(gòu)造授權(quán)URL
String authUrl = wxMpService.getOAuth2Service().buildAuthorizationUrl(redirectUri, scope, state);
// 可以回復(fù)消息
String APPID = wxMpService.getWxMpConfigStorage().getAppId();
String SECRET = wxMpService.getWxMpConfigStorage().getSecret();
String accessToken = wxMpService.getAccessToken();
WxMpUser wxMpUser = wxMpService.getUserService().userInfo(inMessage.getFromUser());
System.out.println(wxMpUser);
WxMpXmlOutMessage outMessage = WxMpXmlOutMessage.TEXT()
.content("<a href=\'"+authUrl+"\'>登錄</a>")
.fromUser(inMessage.getToUser())
.toUser(inMessage.getFromUser())
.build();
return outMessage.toXml();
}
// 其他消息交給路由器處理
WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
if (outMessage != null) {
return outMessage.toXml();
}
return "";
}
@GetMapping("/auth-callback")
public String callback(
@RequestParam("code") String code,
@RequestParam(value = "state", required = false) String state) {
// 驗證state防止CSRF攻擊
try {
// 1. 用code換取access_token
WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
// 2. 獲取用戶信息
WxOAuth2UserInfo userInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);
System.out.println(userInfo);
return "success";
} catch (WxErrorException e) {
return "授權(quán)失敗: " + e.getMessage();
}
}
}
總結(jié)
到此這篇關(guān)于Java實(shí)現(xiàn)微信掃碼授權(quán)登錄的文章就介紹到這了,更多相關(guān)Java微信掃碼授權(quán)登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 中的 DataInputStream 介紹_動力節(jié)點(diǎn)Java學(xué)院整理
DataInputStream 是數(shù)據(jù)輸入流。它繼承于FilterInputStream。接下來通過本文給大家介紹Java 中的 DataInputStream的相關(guān)知識,需要的朋友參考下吧2017-05-05
解決springboot中@DynamicUpdate注解無效的問題
這篇文章主要介紹了解決springboot中@DynamicUpdate注解無效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
一文帶你搞懂Java中Synchronized和Lock的原理與使用
這篇文章主要為大家詳細(xì)介紹了Java中Synchronized和Lock的原理與使用,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2023-04-04
一篇文章帶你認(rèn)識Java8接口的默認(rèn)方法
這篇文章主要給大家介紹了如何通過一篇文章帶你認(rèn)識Java8接口的默認(rèn)方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Java8具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Spring?Cloud?Eureka基礎(chǔ)應(yīng)用及原理
這篇文章主要介紹了Spring?Cloud?Eureka基礎(chǔ)應(yīng)用,Eureka?Client中內(nèi)置一個負(fù)載均衡器,用來進(jìn)行基本的負(fù)載均衡,下面我們將通過搭建一個簡單的Eureka例子來了解Eureka的運(yùn)作原理,感興趣的朋友一起看看吧2022-05-05
SpringMVC+MyBatis 事務(wù)管理(實(shí)例)
本文先分析編程式注解事務(wù)和基于注解的聲明式事務(wù)。對SpringMVC+MyBatis 事務(wù)管理的相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2017-08-08

