微信扫码登录(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;
    }
}
原文地址:https://www.cnblogs.com/liqinzhen/p/13903038.html