微信开发微信授权登录 (微信授权登录网站嵌入自己页面)

应用场景:

test.com已经可以通过微信登录。此时另一个站点dev.test.com也想通过微信登录,但是dev.test.com在微信端没有登录权限

1、在主站引入

https://github.com/HADB/GetWeixinCode项目的资源,将此文件放置在一个合适的位置

2、 配置授权URL

authorize_url=http://test.com/static/html/get-weixin-code.html

不直接通过微信授权,而是通过此URL进行授权

3、配置回调地址

redirect_uri=/oauth/authc/weixin

自定义回调URL

4、需要授权时访问的URL

1)开放平台(扫码登录)

url = authorize_url + "?" + "appid=" + appid + "&redirect_uri="

+ URLEncoder.encode(request.getScheme()+"://"+request.getServerName()+redirect_uri, "UTF-8") + "&response_type=" + response_type + "&scope=" + scope

+ "&state=" + state;

2)公众平台(微信客户端静默登录)

url = authorize_url + "?" + "appid=" + weixinAppid + "&redirect_uri="

+ URLEncoder.encode(request.getScheme()+"://"+request.getServerName()+redirect_uri.replace("weixin", "weixin_mp"), "UTF-8") + "&response_type=" + response_type + "&scope=snsapi_userinfo"

+ "&state=" + state+"#wechat_redirect";

备注:weixinAppid!=appid,appid属于开发平台,weixinAppid属于公众平台。