微信,微博,QQ,這是現在目前用的最多的手機 APP,我們做產品哪能不跟他們不沾邊,對于登錄,我想誰也不想要多少個帳號密碼,根本記不住!
為了增加用戶體驗,用戶能夠快速的注冊登錄,第三方賬號進行登錄注冊的的需求也就由此而誕生

1、微信
1) 微信登錄也是最坑人的,需要花300大洋成為開發者賬戶,沒辦法誰讓微信用戶群體大呢所以也就只好認了;然后登錄網站后臺需要創建網站應用,填寫授權回調域(登錄網站的域名)只填寫域名即可
申請地址: https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN
2)查看微信提供的接口,寫java代碼
 public void wx() { try {  response.sendRedirect("https://open.weixin.qq.com/connect/qrconnect?appid="   + ShareLoginDict.WEIXINKEY.getState()   + "&redirect_uri="   + URLEncoder.encode(ShareLoginDict.WEIXINURL.getState())    + "&response_type=code&scope=snsapi_login&state=66666#wechat_redirect"); } catch (IOException e) {  // TODO Auto-generated catch block  e.printStackTrace(); } }@Override public Result userwx(String return_code) { Result result = new Result(); Map<String, Object> token = (Map<String, Object>) WeiXinAPI  .getToken(return_code); if (token != null && token.get("access_token") != null) {  Map<String, Object> user = (Map<String, Object>) WeiXinAPI   .getWxUser(token.get("access_token").toString(),    token.get("openid").toString());  if (user != null) {  result.addModel("openid", user.get("openid"));  result.addModel("nickname", user.get("nickname"));  result.addModel("headimgurl", user.get("headimgurl"));  result.addModel("data", "data_success");  }else{  result.addModel("data", "data_null");  } }else{  result.addModel("data", "data_null"); } return result; }當用戶通過微信登錄時,調用微信接口獲取用戶接口返回微信端的openid,昵稱,頭像;然后將此信息存入到瀏覽器的cookie中,當用戶瀏覽其他信息時,辨別如果是用戶是用微信登錄的,攔截器直接從cookie中獲取用戶的信息顯示昵稱和頭像,并同時判斷該openid是否和數據庫中用戶做綁定,如果沒有綁定提示用戶注冊。
2、qq登錄
1)qq互聯創建應用
接入QQ登錄前,網站需首先進行申請,獲得對應的appid與appkey,以保證后續流程中可正確對網站與用戶進行驗證與授權。
申請appid和appkey的用途
appid :應用的唯一標識。在OAuth2.0認證過程中,appid的值即為oauth_consumer_key的值。
appkey:appid對應的密鑰,訪問用戶資源時用來驗證應用的合法性。在OAuth2.0認證過程中,appkey的值即為oauth_consumer_secret的值。
申請地址: http://connect.qq.com/intro/login/
2)查看QQ提供的接口,寫java代碼
public void qq() { try {  response.sendRedirect("https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id="   + ShareLoginDict.QQKEY.getState()   + "&redirect_uri="   + ShareLoginDict.QQURL.getState() + "&scope=get_user_info"); } catch (IOException e) {  // TODO Auto-generated catch block  e.printStackTrace(); } }@Override public Result userqq(String return_code) { Result result = new Result(); Map<String, Object> token = (Map<String, Object>) QQAPI  .getToken(return_code); if (token != null && token.get("access_token") != null) {  Map<String, Object> tokenme = (Map<String, Object>) QQAPI   .getTokenMeOpenId(token.get("access_token").toString());  if (tokenme != null && tokenme.get("openid") != null) {  Map<String, Object> user = (Map<String, Object>) QQAPI   .getQqUser(token.get("access_token").toString(),    tokenme.get("openid").toString());  if (user != null) {   result.addModel("openid", tokenme.get("openid"));   result.addModel("nickname", user.get("nickname"));   result.addModel("figureurl", user.get("figureurl"));   result.addModel("data", "data_success");  }else{   result.addModel("data", "data_null");  }  }else{  result.addModel("data", "data_null");  } }else{  result.addModel("data", "data_null"); } return result; } 當用戶通過QQ登錄時,調用QQ接口獲取用戶接口返回QQ端的openid,昵稱,頭像;然后將此信息存入到瀏覽器的cookie中,當用戶瀏覽其他信息時,辨別如果是用戶是用微信登錄的,攔截器直接從cookie中獲取用戶的信息顯示昵稱和頭像,并同時判斷該openid是否和數據庫中用戶做綁定,如果沒有綁定提示用戶注冊。
3 微博登錄
1)微博創建應用
申請地址:http://open.weibo.com/authentication
2)查看微博提供的接口,寫java代碼
public void wb() { try {  response.sendRedirect("https://api.weibo.com/oauth2/authorize?client_id="   + ShareLoginDict.WEIBOKEY.getState()   + "&redirect_uri="   + ShareLoginDict.WEIBOURL.getState()   + "&response_type=code"); } catch (IOException e) {  // TODO Auto-generated catch block  e.printStackTrace(); } }@Override public Result userwb(String return_url, String return_code) { Result result = new Result(); Map<String, Object> token = (Map<String, Object>) WeiBoAPI.getToken(  return_url, return_code); if (token != null && token.get("access_token") != null) {  Map<String, Object> user = (Map<String, Object>) WeiBoAPI   .getWbUser(token.get("access_token").toString(),    token.get("uid").toString());  if (user != null) {  result.addModel("name", user.get("screen_name"));  result.addModel("pic", user.get("avatar_large"));  result.addModel("idstr", user.get("idstr"));  result.addModel("data", "data_success");  }else{  result.addModel("data", "data_null");  } }else{  result.addModel("data", "data_null"); } return result; }當用戶通過微博登錄時,調用微博接口獲取用戶接口返回微博端的idstr,昵稱,頭像;然后將此信息存入到瀏覽器的cookie中,當用戶瀏覽其他信息時,辨別如果是用戶是用微信登錄的,攔截器直接從cookie中獲取用戶的信息顯示昵稱和頭像,并同時判斷該openid是否和數據庫中用戶做綁定,如果沒有綁定提示用戶注冊。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答