亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java實現(xiàn)微信掃碼授權(quán)登錄完整步驟

 更新時間:2025年11月10日 10:29:56   作者:xwhking  
微信掃碼登錄是目前非常流行的第三方登錄方式,這篇文章主要介紹了Java實現(xiàn)微信掃碼授權(quán)登錄的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

一直想實現(xiàn)一下微信的掃碼登錄,現(xiàn)在終于來實踐了。

要想實現(xiàn)微信的掃碼登錄肯定是直接瀏覽微信的官方開發(fā)者文檔,從中就可以獲取一切的用法。

實現(xiàn)步驟

創(chuàng)建服務號的測試號

  • 首先要創(chuàng)建一個測試號,用于微信接口的調(diào)試,創(chuàng)建測試號地址
    創(chuàng)建好了之后是這樣的:

其中的 URL 是需要自己填寫的,這個是接收微信事件的回調(diào)地址,微信會給你推送事件,這個配置接口我們后面寫,token 可以隨便寫,不過后端符合微信的驗證就好了。

創(chuàng)建一個SpringBoot的項目

這里直接通過 IDEA 創(chuàng)建一個后端項目,然后引入,WxJava 的 SDK,方便進行微信的交互,當然也可以自己寫和微信的交互,但是太繁雜了,別人都寫好了,為什么還寫呢???

  • 引入 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下載 ,簡單直接能夠免費使用 24h 的http和https的隨機域名,當然能夠永久,不過需要購買罷了。其他的內(nèi)網(wǎng)穿透推薦ngrok , ftp

進入網(wǎng)頁后點擊相應的機型進行下載就好了。

windows下載好后,直接點開web就好

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

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

掃碼登錄流程介紹

背景知識,如果用戶沒有點擊授權(quán)信息的話,從微信掃碼只能夠獲得用戶的 OpenId,其他信息都獲取不到,但是一般我們登錄流程的話是需要用戶的昵稱,頭像,通過用戶的授權(quán),我們就可以直接獲取到了,不用用戶再次設(shè)置,這里用戶的手機號也是不能獲取到的。

  1. 用戶掃碼(只能或者一個帶有參數(shù)的url,要轉(zhuǎn)化成二維碼,這里沒有用前端了直接用草料二維碼進行轉(zhuǎn)換),如果用戶掃碼了以后沒有關(guān)注公眾號,那么當用戶點擊關(guān)注以后才會推送掃碼關(guān)注事件,如果用戶已經(jīng)關(guān)注了那么手機會直接定位到會話頁面
    • 用測試生成url
    @SpringBootTest
    class WxLoginApplicationTests {
    @Autowired
    private WxMpService wxService;
    @Test
    void contextLoads() throws WxErrorException {
    System.out.println(wxService.getQrcodeService().qrCodeCreateTmpTicket(“user_info”, 30000).getUrl());
    }
    }
  1. 接受微信推送的事件

  2. 返回給用戶信息,消息的響應

  3. 用戶點擊鏈接進行授權(quán)

  4. 用戶打開地址,讓后端獲得權(quán)限,這里如果是域名備案了,那么會直接訪問,否則要自己到瀏覽器打開網(wǎng)址才行。

  5. 此時獲取到了用戶的信息,已經(jīng)成功進行了關(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=[])

代碼實現(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);

            // 可以回復消息
            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實現(xiàn)微信掃碼授權(quán)登錄的文章就介紹到這了,更多相關(guān)Java微信掃碼授權(quán)登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論