网页客服,欢迎咨询

OAuth2.0网页授权只能设置一个回调域名的问题

2020-12-29 10:12:19 472
  • 收藏
  • 管理

    get-weixin-code.html

    <!DOCTYPE html><html lang="en">
    
        <head>
            <meta charset="UTF-8">
            <title>微信登录</title>
        </head>
    
        <body>
            <script>
                var GWC = {
                    version: '1.1.1',
                    urlParams: {},
                    appendParams: function(url, params) {
                        if (params) {                        var baseWithSearch = url.split('#')[0];                        var hash = url.split('#')[1];                        for (var key in params) {                            var attrValue = params[key];                            if (attrValue !== undefined) {                                var newParam = key + "=" + attrValue;                                if (baseWithSearch.indexOf('?') > 0) {                                    var oldParamReg = new RegExp('^' + key + '=[-%.!~*\'\(\)\\w]*', 'g');                                    if (oldParamReg.test(baseWithSearch)) {
                                            baseWithSearch = baseWithSearch.replace(oldParamReg, newParam);
                                        } else {
                                            baseWithSearch += "&" + newParam;
                                        }
                                    } else {
                                        baseWithSearch += "?" + newParam;
                                    }
                                }
                            }                        if (hash) {
                                url = baseWithSearch + '#' + hash;
                            } else {
                                url = baseWithSearch;
                            }
                        }                    return url;
                    },
                    getUrlParams: function() {
                        var pairs = location.search.substring(1).split('&');                    for (var i = 0; i < pairs.length; i++) {                        var pos = pairs[i].indexOf('=');                        if (pos === -1) {                            continue;
                            }
                            GWC.urlParams[pairs[i].substring(0, pos)] = decodeURIComponent(pairs[i].substring(pos + 1));
                        }
                    },
                    doRedirect: function() {
                        var code = GWC.urlParams['code'];                    var appId = GWC.urlParams['appid'];                    var scope = GWC.urlParams['scope'] || 'snsapi_base';                    var state = GWC.urlParams['state'];                    var redirectUri;                    if (!code) {                        //第一步,没有拿到code,跳转至微信授权页面获取code
                            redirectUri = GWC.appendParams('https://open.weixin.qq.com/connect/oauth2/authorize#wechat_redirect';, {                            'appid': appId,                            'redirect_uri': encodeURIComponent(location.href),                            'response_type': 'code',                            'scope': scope,                            'state': state,
                            });
                        } else {                        //第二步,从微信授权页面跳转回来,已经获取到了code,再次跳转到实际所需页面
                            redirectUri = GWC.appendParams(GWC.urlParams['redirect_uri'], {                            'code': code,                            'state': state
                            });
                        }
                        location.href = redirectUri;
                    }
                };
                GWC.getUrlParams();
                GWC.doRedirect();        </script>
        </body></html>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182

    使用方法

    1. 部署get-weixin-code.html至你的微信授权回调域名的目录下

    2. 使用方式类似于直接通过微信回调的方式,只是将回调地址改成了get-weixin-code.html所在的地址,另外省去了response_type参数(因为它只能为code)以及#wechat_redirect(它是固定的),它们会在get-weixin-code.html里面自己加上

    3. get-weixin-code.html页面从微信那里拿到code之后会重新跳转回redirect_uri里面填写的url,并且在url后面带上code和state

    详细示例

    1. 前往微信公众平台->接口权限->网页授权获取用户基本信息->修改,填写授权回调页面域名,例如www.abc.com

    2. 在www.abc.com域名下部署get-weixin-code.html,不一定是根目录,例如:http://www.abc.com/xxx/get-weixin-code.html

    3. 假设你的http://www.xyz.com/hello-world.html这个页面需要获取微信授权,那么你应该使用以下地址来获取授权:http://www.abc.com/xxx/get-weixin-code.html?appid=XXXX&scope=snsapi_base&state=hello-world&redirect_uri=http%3A%2F%2Fwww.xyz.com%2Fhello-world.html

    4. 这样最终就会跳转到这样一个地址:http://www.xyz.com/hello-world.html?code=XXXXXXXXXXXXXXXXX&state=hello-world,从而你就拿到了授权code以及自定义的state参数了

    其他说明

    • 通过多一次的跳转,解决了微信限制回调域名只能设置一个的问题

    • 牺牲了一点用户体验,换来了项目部署的美感,不需要将各种项目都部署到一个域名下

    • 建议先弄懂微信授权回调的流程再使用本项目

    • 很多朋友问我怎么支持第三方微信平台,这个需要对不同的第三方平台的授权方式有所了解,熟悉他们的授权方式,请求参数等。如果他们是通过在网站入口处的URL上进行授权的,那么可以使用本项目,将入口的URL改成上述的方式,如果他们是在流程中的某些页面去获取授权,那么是没法改变他们的获取地址的,所以本项目就不适用了


    OAuth2.0网页授权只能设置一个回调域名的问题
    上一页:grid栅格布局的使用方法(真正的row和column都自己控制) 下一页:网页授权多域名解决方案
    全部评论(0)