微信掃碼登錄(OAuth2)


一、OAuth2是什么?

       OAuth2(開放授權)是一個開放標准,允許用戶授權第三方移動應用訪問他們存儲在另外的服務提供者上的信息,而不需要將用戶名和密碼提供給第三方移動應用或分享他們數據的所有內容。

二、OAtuth2主要解決了兩個問題

1.開放系統間授權

2.分布式訪問問題

OAuth2解決方案:令牌機制,按照一定的規則生成字符串,字符串包含用戶信息。

三、微信登錄實現步驟

在配置文件中,添加微信id,秘鑰,域名地址。

1.添加配置:application.properties添加相關配置信息

# 微信開放平台 appid

wx.open.app_id=你的appid

# 微信開放平台 appsecret

wx.open.app_secret=你的appsecret

# 微信開放平台 重定向url

wx.open.redirect_url=http://你的服務器名稱/api/ucenter/wx/callback

2.編寫讀取配置信息的工具類

package com.atguigu.educenter.utils;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * author LiQinZhen
 * date 2020/10/30
 * description: 讀取微信配置的類
 */
@Component
public class ConstantWxUtils implements InitializingBean {

    @Value("${wx.open.app_id}")
    private String appId;

    @Value("${wx.open.app_secret}")
    private String appSecret;

    @Value("${wx.open.redirect_url}")
    private String redirectUrl;

    //定義常量
    public static String WX_OPEN_APP_ID;
    public static String WX_OPEN_APP_SECRET;
    public static String WX_OPEN_REDIRECT_URL;

    @Override
    public void afterPropertiesSet() throws Exception {
        WX_OPEN_APP_ID = appId;
        WX_OPEN_APP_SECRET = appSecret;
        WX_OPEN_APP_SECRET = redirectUrl;

    }
}

3.生成微信掃碼二維碼

     直接使用微信提供的固定地址,地址后面拼接參數即可。

4.編寫controller

package com.atguigu.educenter.controller;

import com.atguigu.educenter.utils.ConstantWxUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * author LiQinZhen
 * date 2020/10/30
 * description: 微信登錄controller
 */
@Controller
@CrossOrigin
@RequestMapping("/api/ucenter/wx")
public class WxApiController {
    //1.生成微信掃碼二維碼
     @GetMapping("login")
    public String getWxCode(){
        //固定地址后面拼接參數
        //String url =  "https://open.weixin.qq.com/connect/qrconnect";

        // 微信開放平台授權baseUrl,%S相當於占位符
        String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +
                "?appid=%s" +
                "&redirect_uri=%s" +
                "&response_type=code" +
                "&scope=snsapi_login" +
                "&state=%s" +
                "#wechat_redirect";
        //對url_redirect進行UrlEncoder編碼
        String redirectUrl = ConstantWxUtils.WX_OPEN_REDIRECT_URL;
        try {
            redirectUrl = URLEncoder.encode(redirectUrl,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //向上面的%s中設置值
        String url = String.format(
                baseUrl,
                ConstantWxUtils.WX_OPEN_APP_ID,
                redirectUrl,
                "atguigu"
        );
        //重定向到請求微信的地址里面
        return "redirect:"+url;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM