微信授权

项目写完记录一下微信授权的步骤。欢迎指正交流。
如果有什么不一样以官方文档为主 微信授权官方文档

授权之前别急,要先在微信端配置网页授权回调域名,需要先到公众平台官网中的“开发 - 接口权限 - 网页服务 - 网页帐号 - 网页授权获取用户基本信息”的配置选项中,修改授权回调域名。请注意,这里填写的是域名(是一个字符串),而不是URL,因此请勿加 http:// 等协议头;
这里写图片描述

  • 第一步:用户同意授权,获取code
    引导用户进入授权页面,代码如下
@RequestMapping("/login")
    public void  login(HttpServletRequest request,HttpServletResponse response) {
        //业务参数
        String backUrl = "http://xxxxxxxx/xxxx/xxxx/xxx/index.html";
        try {
        //设置回调地址
            String redirect_url ="http://xxx.xxx.com/yxx/xxx/callBack";
            String url="https://open.weixin.qq.com/connect/oauth2/authorize?"
                    + "appid="+HttpClientUtils.appid
                    + "&redirect_uri="+redirect_url
                    + "&response_type=code"
                    + "&scope=snsapi_userinfo"
                    + "&state="+backUrl+"#wechat_redirect";
            response.sendRedirect(url);
        } catch (Exception e) {
            logger.error("error------"+e.getMessage());
        }
    }

发送参数的说明见文档
这里写图片描述
参数说明中redirect_uri标明需要编码,但我的代码中没有进行编码也正确跳转到了我的回调controller里了。

  • 第二步:通过code换取网页授权access_token
    在用户点击同意授权之后,页面将页面跳转至 redirect_uri/?code=CODE&state=STATE。
    这时需要通过code来获取access_token,注意这里的access_token和基础支持中的access_token不同,不需要进行缓存,没有获取次数限制
    详细代码如下
    @RequestMapping("/callBack")
    public void  callBack1(HttpServletRequest request,HttpServletResponse response){
        logger.info("-----------callBack--start-------------");
        try {
            String code = request.getParameter("code");
            String backUrl = request.getParameter("state");
            String url="https://api.weixin.qq.com/sns/oauth2/access_token"
                    + "?appid="+HttpClientUtils.appid
                    + "&secret="+HttpClientUtils.AppSecret
                    + "&code="+code
                    + "&grant_type=authorization_code";
            JSONObject json = HttpClientUtils.doGetJson(url);
            String openid = json.getString("openid");
            String token = json.getString("access_token");
            //获取到openid(每个用户的唯一标识)开始处理业务逻辑
            UserPo userPo = userService.findUserByToken(openid);
            if(userPo != null){
                response.sendRedirect(backUrl+"?token="+openid);
                return ;
            }
            //如果用户为空
            String infoUrl = "https://api.weixin.qq.com/sns/userinfo"
                    + "?access_token="+token
                    + "&openid="+openid
                    + "&lang=zh_CN";
            JSONObject userInfo = HttpClientUtils.doGetJson(infoUrl);
            //将微信信息和用户信息绑定
            UserPo user  = new UserPo();
            //处理微信昵称中有表情的情况(将表情过滤)
            String name =StringUtil.getName(new String(userInfo.getString("nickname").getBytes("ISO-8859-1"),"UTF-8"));
            name.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "");
            user.setName(name);
            user.setToken(userInfo.getString("openid"));
            user.setImg(userInfo.getString("headimgurl"));
            user.setSex(userInfo.getInt("sex"));
            user.setDelMark("0");
            userService.insertPO(user);
            logger.info("----------user is create -----------------"+user.toString());
            //新授权用户未绑定手机号,去绑定手机号页面
            response.sendRedirect("http://xxxxxxxx/xxxxx/xxxx/xxxx/blind.html?token="+openid);
        } catch (Exception e) {
            try {
                logger.error("----e--"+e.getLocalizedMessage());
                request.getRequestDispatcher("/share.jsp").forward(request, response);
            } catch (Exception ee) {
                ee.printStackTrace();
            }
            e.printStackTrace();

        }
        return ;
    }
  • 第三步:刷新access_token(如果需要)
    这里不需要
  • 第四步:拉取用户信息(需scope为 snsapi_userinfo)
    通过openId获取用户信息,代码在第二步中已经完成。
    好了授权就写好了~

每次看官方文档的时候都是,刚开始看觉得这写的是什么啊,然后找博客看别人怎么写的,各种改,写完之后恍然大悟,奥是这个意思,我好像对官方文档有一种抵触心理。。。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值