java微信公众号授权登录

微信授权登录

注意:微信授权登录测试用户必须已经关注测试号,否则会获取不到用户信息

相关配置

使用内网穿透,配置相关端口,在JS接口安全域名和网页授权登录处配置穿透的网址,

注意:配置域名去掉http://

在这里插入图片描述

在这里插入图片描述

获取code值

在前台界面访问的url,判断是否有code值,

if (this.wxcode == null || this.wxcode == '') {
            let url =
                "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + this.$common.wx.appID +
                "&redirect_uri=" + 'http://ffktu3.natappfree.cc/user_signup'  +
                "&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect";
            window.location.href = url;
          }
//this.$common.wx.appID这个appId使用的是自己微信测试号里的appId
//&redirect_uri=后面拼接的是自己的重定向路径,注意这里要用到内网穿透的地址

注意:&redirect_uri=后面拼接的路径要使用可以访问的路径,本地接口locahost:8080的话会出出错,"&redirect_uri"错误,必须使用本地测试的话,推荐使用内网穿透

关于appId:参考https://blog.csdn.net/qq_45587153/article/details/116428667

内网穿透:https://blog.csdn.net/qq_45587153/article/details/116587789

截取url中code的方法

//截取code值
    getUrlParam(name) {
      var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
      let url = window.location.href.split('#')[0]
      let search = url.split('?')[1]
      if (search) {
        var r = search.substr(0).match(reg)
        if (r !== null)
          return unescape(r[2])
        return null
      } else
        return null
    }

//调用:
this.wxcode = this.getUrlParam('code')

拉取用户信息

后台java代码

@GetMapping("/getUserInfo/{code}")
public  ResultJson getUserInfo(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值