微信开发网页授权认证

微信网页授权有两种方式,分别是:

静默授权:以snsapi_base为scope发起的网页授权,是用来获取进入页面的用户的openid的,并且是静默授权并自动跳转到回调页的。用户感知的就是直接进入了回调页(往往是业务页面)

用户授权:以snsapi_userinfo为scope发起的网页授权,是用来获取用户的基本信息的。但这种授权需要用户手动同意,并且由于用户同意过,所以无须关注,就可在授权后获取该用户的基本信息。

关于网页授权access_token和普通access_token的区别

1、微信网页授权是通过OAuth2.0机制实现的,在用户授权给公众号后,公众号可以获取到一个网页授权特有的接口调用凭证(网页授权access_token),通过网页授权access_token可以进行授权后接口调用,如获取用户基本信息;
2、其他微信接口,需要通过基础支持中的“获取access_token”接口来获取到的普通access_token调用。

静默授权调用的接口:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdap
ter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_bas
e&state=123#wechat_redirect

用户授权接口:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=
code&scope=snsapi_userinfo&state=STATE#wechat_redirect

由于静默授权比较容易,这边就不陈述了,就来讲讲用户授权方式的:
首先在测试号页面中找到如下链接:
微信开发网页授权认证_第1张图片
点击修改按钮:
微信开发网页授权认证_第2张图片
这里配置了域名,后面啥也别加,不然会找不到,点击确定按钮即可。
下面是我的后台服务

@RequestMapping(value = "/hello")
    public String index(){
        return "index";
    }

    @RequestMapping(value = "/detail")
    public String detail(){
        return "detailpage";
    }


    @RequestMapping(value = "/home",method= RequestMethod.GET)
    @ResponseBody
    public void valid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean isGet = request.getMethod().toLowerCase().equals("get");
        String code = request.getParameter("code");
        if (isGet) {
            // 微信加密签名
            String signature = request.getParameter("signature");
            // 时间戳
            String timestamp = request.getParameter("timestamp");
            // 随机数
            String nonce = request.getParameter("nonce");
            // 随机字符串
            String echostr = request.getParameter("echostr");

            PrintWriter out = response.getWriter();
            // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,表示接入成功,否则接入失败
            if (SignUtil.checkSignature(signature, timestamp, nonce)) {
                out.print(echostr);
            }
            out.close();
            out = null;
        }else{
            userpost(request,response);
        }
    }

    @RequestMapping(value = "/home",method= RequestMethod.POST)
    public void userpost(HttpServletRequest request, HttpServletResponse response) throws IOException   {
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String str = null;
        String code = request.getParameter("code");
        try {
            Map message = MessageUtil.xmlToMap(request);
            String fromUser = message.get("FromUserName");
            String toUser = message.get("ToUserName");
            String content = message.get("Content");
            String msgType = message.get("MsgType");
            String msgId = message.get("MsgId");
            Message ms = new Message();
            ms.setContent(content);
            ms.setFromUserName(toUser);
            ms.setToUserName(fromUser);
            ms.setMsgType(msgType);
            str = MessageUtil.textMessageToXml(ms).replace("\n","").replace(" ","");
            out.print(str);
            out.close();
            out = null;
        }catch (Exception e){
            e.printStackTrace();
        }
    }

按钮菜单:

 private static Button getMenu() {

        String APPID = "wxbc359ccb5e797e11";
        String REDIRECT_URI = "http://gky2jr.natappfree.cc/WeChatProject/wechat/hello";
        String urlscope = "snsapi_userinfo";
        String state = "1";

        BaseButton btn11 = new BaseButton();
        btn11.setName("学霸笔记本");
        btn11.setType("click");
        btn11.setKey("11");

        BaseButton btn12 = new BaseButton();
        btn12.setName("记忆宫殿");
        btn12.setType("click");
        btn12.setKey("12");

        BaseButton btn13 = new BaseButton();
        btn13.setName("逻辑思维");
        btn13.setType("view");

        StringBuilder url = new StringBuilder();
        url.append("https://open.weixin.qq.com/connect/oauth2/authorize?");
        url.append("appid="+APPID);
        url.append("&");
        url.append("redirect_uri="+REDIRECT_URI);
        url.append("&");
        url.append("response_type=code");
        url.append("&");
        url.append("scope="+urlscope);
        url.append("&state=1#wechat_redirect");
        btn13.setUrl(url.toString());

        Sub_Button sub1 = new Sub_Button();
        sub1.setName("菜单一");
        sub1.getSub_button().add(btn11);

        Sub_Button sub2 = new Sub_Button();
        sub2.setName("菜单二");
        sub2.getSub_button().add(btn12);

        Sub_Button sub3 = new Sub_Button();
        sub3.setName("xinbuxing");
        sub3.getSub_button().add(btn13);


        Button menu = new Button();
        menu.getButton().add(sub1);
        menu.getButton().add(sub2);
        menu.getButton().add(sub3);

        return  menu;
    }

当我单击逻辑思维按钮时
微信开发网页授权认证_第3张图片

便会弹出该验证页面
微信开发网页授权认证_第4张图片
值得注意的是我在手机上没有显示该验证页面,本人怀疑是测试号的问题
参考文档
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842

你可能感兴趣的:(微信开发,微信)