java-SpringBoot 实现微信扫码登录(网站应用)-前后端分离

1 微信开放平台(https://open.weixin.qq.com/)资质认证并申请创建网站应用,拿到appId和appSecret以及回调url,已有的可跳过

   java-SpringBoot 实现微信扫码登录(网站应用)-前后端分离

2 后端将获取扫码用的二维码地址返回给前台

   

@Override
public Response getWechatQrCode() {
    try {
        LOGGER.info("开始获取微信登录二维码:");
        String oauthUrl = "https://open.weixin.qq.com/connect/qrconnect?            appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
        String redirect_uri = URLEncoder.encode(Constant.WX_OPEN_REDIRECT_URL, "utf-8");
        System.out.println("redirect_uri:"+redirect_uri);
        oauthUrl =  oauthUrl.replace("APPID",Constant.WX_OPEN_APP_ID).replace("REDIRECT_URI",redirect_uri).replace("SCOPE",Constant.WX_OPEN_SCOPE);
        LOGGER.info("oauthUrl:"+oauthUrl);
        return new Response(Constant.RES_SUCCESS, "获取微信登录二维码成功", oauthUrl);
    } catch (Exception e) {
        LOGGER.info("获取微信登录二维码失败:"+e);
        Utils.getExceptionDetail(e);
        return new Response(Constant.RES_ERROR_SERVER,"服务器内部错误");
    }
}

  上面scope的值为snsapi_login,前端拿到oauthUrl就可以直接请求到微信扫码的页面

   java-SpringBoot 实现微信扫码登录(网站应用)-前后端分离

3 用户扫码之后就可以拿到code,使用code获取access_token和openid

   java-SpringBoot 实现微信扫码登录(网站应用)-前后端分离

4 用access_token和openid就可以获取用户的信息了

java-SpringBoot 实现微信扫码登录(网站应用)-前后端分离

5 拿到用户信息(userInfoStr)了接下来就是判断用户是否存在,再做其他自定义代码的事了,如:

  java-SpringBoot 实现微信扫码登录(网站应用)-前后端分离

  至此网页微信扫码登录就算完成了,其中3-4步中的源码如下:

   

//通过code获取access_token
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
url = url.replace("APPID",Constant.WX_OPEN_APP_ID).replace("SECRET",Constant.WX_OPEN_APP_SECRET).replace("CODE",code);
JSONObject tokenInfoObject = HttpUtils.httpGet(url);
LOGGER.info("tokenInfoObject:{}",tokenInfoObject);

//通过access_token和openid获取用户信息
String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
userInfoUrl = userInfoUrl.replace("ACCESS_TOKEN",tokenInfoObject.getString("access_token")).replace("OPENID",tokenInfoObject.getString("openid"));
JSONObject userInfoStr = HttpUtils.httpGet(userInfoUrl);
LOGGER.info("userInfoStr:{}",userInfoStr);

  上面用到的HttpUtils工具类:

   

package com.hy.bjggwhy.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author zhangmy
 * @date 2019-12-26 10:45
 * @description
 */
public class HttpUtils {

    private static Logger logger = LoggerFactory.getLogger(HttpUtils.class);

    private static RequestConfig requestConfig = null;

    static {
        // 设置请求和传输超时时
        requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
    }

    /**
     * post请求传输json参数
     *
     * @param url       url地址
     * @param jsonParam 参数
     * @return
     */
    public static JSONObject httpPost(String url, JSONObject jsonParam) {
        // post请求返回结果
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject jsonResult = null;
        HttpPost httpPost = new HttpPost(url);
        // 设置请求和传输超时时请求
        httpPost.setConfig(requestConfig);
        try {
            System.out.println(jsonParam);
            if (null != jsonParam) {
                // 解决中文乱码问题
                StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
            }
            System.out.println(jsonParam);
            CloseableHttpResponse result = httpClient.execute(httpPost);
            // 请求发请求成功,并得到响应
            if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String str = "";
                try {
                    // 读取服务器返回过来的json字符串数
                    str = EntityUtils.toString(result.getEntity(), "utf-8");
                    // 把json字符串转换成json对象
                    jsonResult = JSONObject.parseObject(str);
                } catch (Exception e) {
                    logger.error("post请求提交失败:" + url, e);
                }
            }
        } catch (IOException e) {
            logger.error("post请求提交失败:" + url, e);
        } finally {
            httpPost.releaseConnection();
        }
        return jsonResult;
    }

    /**
     * post请求传输String参数 例如:name=Jack&sex=1&type=2
     * Content-type:application/x-www-form-urlencoded
     *
     * @param url      url地址
     * @param strParam 参数
     * @return
     */
    public static JSONObject httpPost(String url, String strParam) {
        // post请求返回结果
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject jsonResult = null;
        HttpPost httpPost = new HttpPost(url);
        httpPost.setConfig(requestConfig);
        try {
            if (null != strParam) {
                // 解决中文乱码问题
                StringEntity entity = new StringEntity(strParam, "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/x-www-form-urlencoded");
                httpPost.setEntity(entity);
            }
            CloseableHttpResponse result = httpClient.execute(httpPost);
            // 请求发宋成功,并得到响应
            if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String str = "";
                try {
                    // 读取服务器返回过来的json字符串数据
                    str = EntityUtils.toString(result.getEntity(), "utf-8");
                    // 把json字符串转换成json对象
                    jsonResult = JSONObject.parseObject(str);
                } catch (Exception e) {
                    logger.error("post请求提交失败:" + url, e);
                }
            }
        } catch (IOException e) {
            logger.error("post请求提交失败:" + url, e);
        } finally {
            httpPost.releaseConnection();
        }
        return jsonResult;
    }

    /**
     * 发送get请求
     *
     * @param url 路径
     * @return
     */
    public static JSONObject httpGet(String url) {
        // get请求返回结果
        JSONObject jsonResult = null;
        CloseableHttpClient client = HttpClients.createDefault();
        // 发送get请求
        HttpGet request = new HttpGet(url);
        request.setConfig(requestConfig);
        try {
            CloseableHttpResponse response = client.execute(request);

            // 请求发送成功,并得到响应
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                // 读取服务器返回过来的json字符串数组
                HttpEntity entity = response.getEntity();
                String strResult = EntityUtils.toString(entity, "utf-8");
                // 把json字符串转换成json对象
                jsonResult = JSONObject.parseObject(strResult);
            } else {
                logger.error("get请求提交失败:" + url);
            }
        } catch (IOException e) {
            logger.error("get请求提交失败:" + url, e);
        } finally {
            request.releaseConnection();
        }
        return jsonResult;
    }
}