第三方登陆--微信登陆

微信公众号

如果是自己测试:申请一个测试账号就行了。
具体开发流程参考:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

定义httpclient用于请求微信接口

public static JSONObject doGetJson(String url) {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            //创建HttpClients对象
            client = HttpClients.createDefault();
            // 创建http GET请求
            HttpGet httpGet = new HttpGet(url);
            // 执行请求
            response = client.execute(httpGet);
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                //转成String
                String result =  entity != null ? EntityUtils.toString(entity,"UTF-8") : null;
                //String转json
                jsonObject = JSON.parseObject(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (client != null) client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

用户同意授权,获取code

@GetMapping("/applogin")
    public void applogin(HttpServletResponse response) throws IOException {
        //回调地址
        String backUrl = "http://19278m1w03.imwork.net/weixin/appCallBack";
        /**
         * 参考官网:
         * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
         */
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + AuthUtil.appID
                + "&redirect_uri=" + URLEncoder.encode(backUrl)
                + "&response_type=code&"
                + "scope=snsapi_userinfo"
                + "&state=STATE"
                + "#wechat_redirect";
        response.sendRedirect(url);
    }

注意:上面的回调地址的域名19278m1w03.imwork.net必须与图片一致
第三方登陆--微信登陆_第1张图片

注意:appid,secret在这取
第三方登陆--微信登陆_第2张图片

回调地址接口,获取用户信息:

@GetMapping("/appCallBack")
    public String appCallBack(HttpServletRequest request) throws IOException {
        String code = request.getParameter("code");
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + AuthUtil.appID
                + "&secret=" + AuthUtil.appSecret
                + "&code=" + code
                + "&grant_type=authorization_code";
        JSONObject json = AuthUtil.doGetJson(url);
        System.err.println("--------------------------------------------");
        System.out.println(json);
        System.err.println("---------------------------------------------");
        String openid = json.getString("openid");
        String token = json.getString("access_token");
        //拉取用户信息
        String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token
                + "&openid=OPENID" + openid
                + "&lang=zh_CN";
        JSONObject infoJson = AuthUtil.doGetJson(infoUrl);
        System.err.println("=============================================");
        System.out.println(infoJson);
        System.err.println("=============================================");
        return "文文是猪";
    }

取到unionid确定用户,注意微信开发平台和微信公众号绑定才会返回unionid。


微信开发平台

微信开发平台开发和公众号完全一致,可能请求接口有变,参考官网:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN

你可能感兴趣的:(第三方登陆)