第三方登錄之微信登錄

首先我們來了解一下登陸的流程,如圖所示

 在寫程序之前我們先要做一些配置

打開微信公衆平臺測試號管理登錄入口

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對象
}

好了,後臺的數據庫驗證和微信號綁定的操作你應該會自己寫了...

另外附上兩張手機端的截屏

 

奧對了,測試登錄的人必須是關注了你的公衆號的人,如圖

 

 

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