admin管理员组

文章数量:1559110

微信官方文档:https://open.weixin.qq/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_cn

本文是根据阅读官方文档进行的

前提: ,在微信开放平台注册开发者帐号,并拥有一个已审核通过的网站应用,并获得相应的appid和appsecret,申请微信登录且通过审核后,可开始接入流程

如果没有开放平台的账号,且微信登录权限是不能实现本功能的(微信登录权限有公司的资质才能获得,不然申请账号也获取不到权限我们还必须有一个可以访问外网的地址,将你的项目部署上去,如果没有可以去natapp上买一个隧道和域名

我们这里用的是一个github的开源sdk   springboot框架

1.pom 添加依赖


    com.github.binarywang
    weixin-java-mp
    2.7.0

2.阅读官方微信开放平台文档  上面链接

根据文档可知:请求授权登录   --> 获取code --> 获取access_token

2.1 请求授权 获取code 

请求链接

https://open.weixin.qq/connect/qrconnect?appid=appid&redirect_uri=redirect_uri&response_type=code&scope=scope&state=state#wechat_redirect

 

其中的参数说明:

参数是否必须说明
appid应用唯一标识
redirect_uri请使用urlencode对链接进行处理
response_type填code
scope应用授权作用域,拥有多个作用域用逗号(,)分隔,网页应用目前仅填写snsapi_login
state用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验

appid 是微信开放平台的appid  state 参数我们写 返回的url returnurl

我们这里使用第三方封装的sdk 进行访问

在访问前我们需要先进行一下配置

创建 

wechataccountconfig 类 进行配置
@component
public class wxchatopenconfig {
    @autowired
    wechataccountconfig wechataccountconfig;
    @bean
    public wxmpservice wxopenservice()
    {
        wxmpservice wxopenservice = new wxmpserviceimpl();
        wxopenservice.setwxmpconfigstorage(wxmpinmemoryconfigstorage());
        return  wxopenservice;
    }
    @bean
    public wxmpinmemoryconfigstorage wxmpinmemoryconfigstorage()
    {
        wxmpinmemoryconfigstorage wxmpinmemoryconfigstorage = new wxmpinmemoryconfigstorage();
        wxmpinmemoryconfigstorage.setappid(wechataccountconfig.getopenappid());
        wxmpinmemoryconfigstorage.setsecret(wechataccountconfig.getopenappsecret());
        return  wxmpinmemoryconfigstorage;
    }
}

wechataccountconfig 使我们配置前将 appid recert 两个属性封装的对象
@data
@component
@configurationproperties(prefix = "wechat")
public class wechataccountconfig {
   
    /*微信开放平台id*/
    private string openappid;
    /*微信开放平台密匙*/
    private string openappsecret;
}

 

这里我们在application.properties文件中配置你的openappid 和 appsecret

配置如下

wechat.openappid=xxxxx
wechat.openappsecret=xxxxxx

 

wxmpservice 是第三方sdk 封装的一个服务对象,进行微信的相关操作

这样我们就可以在controller 里面使用wxopenservice了

新建wechatcontroller

@controller
@requestmapping("/wechat")
@slf4j
public class wechatcontroller {
    
    @autowired
    private wxmpservice wxopenservice;
   
    /**
     * 微信网页扫码登录
     * @param returnurl
     * @return
     */
    @requestmapping("/qrauthorize")
    public string qrauthorize(@requestparam("returnurl") string returnurl)
    {
        //1.配置
        //2.调用方法
        //   3.这里是返回code和state 参数后要访问的地址,也是我们一会的第二步操作
        string url = 外网地址 "/sell/wechat/qruserinfo";
        //返回之后从定向的url,调用方法
        string  redirecturl = wxopenservice.buildqrconnecturl(/uploads/image/dianzi/url, wxconsts.qrconnect_scope_snsapi_login, urlencoder.encode(returnurl));
        log.info("【微信网页扫码登录】返回redirecturl = {}",redirecturl  );
        return "redirect:" redirecturl;
    }
   
}

state 参数 就是我们在第一步访问后台传的returnurl

这样就可以获取到code

2.2 获取access_token 

官方提供的网址

https://api.weixin.qq/sns/oauth2/access_token?appid=appid&secret=secret&code=code&grant_type=authorization_code

参数

参数是否必须说明
appid应用唯一标识,在微信开放平台提交应用审核通过后获得
secret应用密钥appsecret,在微信开放平台提交应用审核通过后获得
code填写第一步获取的code参数
grant_type填authorization_code

 相信大家都可以找到了吧

我们第三方的sdk 这样写

刚才的controller方法里面有一个第二部访问的后台地址

我们写qruserinfo方法

 /**
     * 微信网页登录获取openid
     * @param code
     * @param returnurl
     * @return
     */
    @requestmapping("/qruserinfo")
    public string qruserinfo(@requestparam("code") string code,
                             @requestparam("state") string returnurl)
    {
        wxmpoauth2accesstoken wxmpoauth2accesstoken = new wxmpoauth2accesstoken();
        try {
            wxmpoauth2accesstoken = wxopenservice.oauth2getaccesstoken(code);
        } catch (wxerrorexception e) {
            log.error("【微信网页扫码登录】获取accesstoken失败");
            throw new resultexception(resultenum.wxcaht_open_error.getcode(),e.geterror().geterrormsg());
        }
        string openid = wxmpoauth2accesstoken.getopenid();
        log.info("【微信网页扫码登录】openid={}",openid);
        return "redirect:" returnurl "?openid="  openid;
    }

记住这里我们使用的是wxopenservice.oauth2getaccesstoken(code); 获取opneid 和access_token

配置完成 启动项目  访问 外网地址 项目地址 wechat/qrauthorize?returnurl 就可以使用了   ,这里的url是我写的请求地址,如果你们controller层进行了改变就用你们自己的

 

如有疑问欢迎私信!

 

本文标签: 第三方网页