第三方登录之微信登录

首先我们来了解一下登陆的流程,如图所示

 在写程序之前我们先要做一些配置

打开微信公众平台测试号管理登录入口

http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index

扫码登录后配置如图

 

 appID(系统提供):测试号id,后面会用到

appsecret(系统提供):测试号密码,后面会用到

URL(自备):授权成功后要跳转的页面

授权回调页面域名:全域名,不具体到某个页面

下面开始上代码

//引导用户打开授权登录页面

@RequestMapping("/wechataa")

public void getStartURLToGetCode(HttpServletResponse resp) {
String takenUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
    String APPID="appid";//前面系统给你的appID
    String  REDIRECT_URI="redirect_uri";//前面配置好的要跳转的页面
    takenUrl= takenUrl.replace("APPID", APPID);
    takenUrl= takenUrl.replace("REDIRECT_URI", URLEncoder.encode(REDIRECT_URI));
    takenUrl= takenUrl.replace("SCOPE", "snsapi_userinfo");
    try {
        resp.sendRedirect(takenUrl);//重定向的方式发送请求
    }catch (Exception e){e.printStackTrace();}
}

//跳转后执行的操作

@RequestMapping("/wechat")
public String cc(Model m, HttpServletRequest req, HttpServletResponse resp) throws IOException {
    boolean isGet = req.getMethod().toLowerCase().equals("get");
    PrintWriter print;
    if (isGet) {
    //判断是否用get方法发出请求
        String code = req.getParameter("code");
        String state = req.getParameter("state");
        //authUrl是用于获取access_token和openid的链接
        String authUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code ";
        authUrl= authUrl.replace("APPID", "appID");//前面系统给你的appID
        authUrl = authUrl.replace("SECRET", "appsecret");//前面系统给你的appsecret
        authUrl = authUrl.replace("CODE", code);
        JSONObject jsonob=result(authUrl);
        System.out.println("所有返回值"+jsonob.toString());
        String Accesstoken=jsonob.get("access_token").toString();
        String Openid=jsonob.get("openid").toString();
       //uuul是用于获取用户信息的链接
       String uuul="https://api.weixin.qq.com/sns/userinfo?access_token=Accesstoken&accessopenid=Openid&lang=zh_CN";
        uuul.replace("Accesstoken",Accesstoken);
        uuul.replace("openid", Openid);
        JSONObject userinfo=result(uuul);
        System.out.println("userinfo"+userinfo.toString());//后台以json字符串的方式输出用户信息

    }
    return "success";
}

//http请求函数

public JSONObject result(String https) throws IOException{
    URL url = new URL(https);
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    connection.connect();
   //访问资源数据,使用getInputStream方法获取一个输入流用以读取信息
    BufferedReader bReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
    // 对数据进行访问
    String line = null;
    StringBuilder stringBuilder = new StringBuilder();
    while ((line = bReader.readLine()) != null) {
        stringBuilder.append(line);
    }
    bReader.close();// 关闭流
    connection.disconnect();// 关闭链接
    JSONObject jsonObject = JSONObject.fromObject(stringBuilder.toString());// 将获得的String对象转为JSON格式
    return jsonObject;//返回json对象
}

好了,后台的数据库验证和微信号绑定的操作你应该会自己写了...

另外附上两张手机端的截屏

 

奥对了,测试登录的人必须是关注了你的公众号的人,如图

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章