麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

第三方網站微信登錄java代碼實現

2019-11-26 12:33:01
字體:
來源:轉載
供稿:網友

前兩個星期在公司中的項目加上了微信登錄、綁定的功能,在這里做個記錄!

一、開發前知識

1、微信開放平臺與微信公眾平臺的區別

 1.1 微信公眾平臺:  

 ① 地址:https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN

 ② 微信公眾平臺面向的是普通的用戶,比如自媒體和媒體,企業官方微信公眾賬號運營人員使用,當然你所在的團隊或者公司有實力去開發一些內容,也可以調用公眾平臺里面的接口,比如自定義菜單,自動回復,查詢功能。

 1.2 微信開放平臺: 

 ① 地址:https://open.weixin.qq.com/

 微信開放平臺:面向的是開發者和第三方獨立軟件開發商。開放平臺的文檔似乎包含了微信開放平臺文檔里面的接口。

2、微信公眾平臺目前只支持80端口,且項目上傳到服務器上面不容易debug啊?

解決方法:ngrok 工具,可以根據本地項目映射成外網可以訪問的地址、端口,默認映射為80端口。

官網: https://dashboard.ngrok.com

存在問題: 每次重啟,域名會變化,都需要項目中、微信公眾平臺配置,付費可以固定域名。。。

使用方法: ① 注冊后獲得一個授權碼,下載 ngrok

      ② 》CMD 進入 ngrok 目錄

       》ngrok authtoken 授權碼

       》ngrok http 8080

      ③看到這個頁面,ngrok就為你分配了臨時訪問通道成功了^^

二、配置

(每個微信號可以申請成為開發者測試賬號進行微信功能開發的。)

1、在公眾平臺中進行配置:

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

 ①:配置“接口配置信息” (Token 在項目中我是配置為"handsomeKing",沒錯,我就是King ^^)

 url 改為自己映射的域名

 ② 配置“功能服務” 的 “網頁帳號”。注意,是域名

好了,可以愉快的coding 了!!!

三、可以開發啦

1、上面的 “接口配置信息” 配置得 wechat/ownerCheck 方法是驗證 這個url 為我自己的,handsomeKing 就是這個方法中的一個參數。微信這個小朋友會用Get方法帶上4個參數給我服務器: signature(簽名)、timestamp(時間戳)、nonce(隨機數)、echostr(隨機字符串),再加上咱們項目中自己配置的 token ,通過檢驗signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,否則接入失敗:(talk is cheap, show me the code *&)

 /** * 微信消息接收和token驗證 * @param model * @param request * @param response * @throws IOException */ @RequestMapping("/ownerCheck") public void ownerCheck(Model model, HttpServletRequest request,HttpServletResponse response) throws IOException { System.out.println(111); boolean isGet = request.getMethod().toLowerCase().equals("get"); PrintWriter print; if (isGet) {  // 微信加密簽名  String signature = request.getParameter("signature");  // 時間戳  String timestamp = request.getParameter("timestamp");  // 隨機數  String nonce = request.getParameter("nonce");  // 隨機字符串  String echostr = request.getParameter("echostr");  // 通過檢驗signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,否則接入失敗  if (signature != null && CheckoutUtil.checkSignature(signature, timestamp, nonce)) {  try {   print = response.getWriter();   print.write(echostr);   print.flush();  } catch (IOException e) {   e.printStackTrace();  }  } } }
import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class CheckoutUtil { // 與接口配置信息中的Token要一致 private static String token = "handsomeKing"; /** * 驗證簽名 *  * @param signature * @param timestamp * @param nonce * @return */ public static boolean checkSignature(String signature, String timestamp, String nonce) { String[] arr = new String[] { token, timestamp, nonce }; // 將token、timestamp、nonce三個參數進行字典序排序 // Arrays.sort(arr); sort(arr); StringBuilder content = new StringBuilder(); for (int i = 0; i < arr.length; i++) {  content.append(arr[i]); } MessageDigest md = null; String tmpStr = null; try {  md = MessageDigest.getInstance("SHA-1");  // 將三個參數字符串拼接成一個字符串進行sha1加密  byte[] digest = md.digest(content.toString().getBytes());  tmpStr = byteToStr(digest); } catch (NoSuchAlgorithmException e) {  e.printStackTrace(); } content = null; // 將sha1加密后的字符串可與signature對比,標識該請求來源于微信 return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false; } /** * 將字節數組轉換為十六進制字符串 *  * @param byteArray * @return */ private static String byteToStr(byte[] byteArray) { String strDigest = ""; for (int i = 0; i < byteArray.length; i++) {  strDigest += byteToHexStr(byteArray[i]); } return strDigest; } /** * 將字節轉換為十六進制字符串 *  * @param mByte * @return */ private static String byteToHexStr(byte mByte) { char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char[] tempArr = new char[2]; tempArr[0] = Digit[(mByte >>> 4) & 0X0F]; tempArr[1] = Digit[mByte & 0X0F]; String s = new String(tempArr); return s; } public static void sort(String a[]) { for (int i = 0; i < a.length - 1; i++) {  for (int j = i + 1; j < a.length; j++) {  if (a[j].compareTo(a[i]) < 0) {   String temp = a[i];   a[i] = a[j];   a[j] = temp;  }  } } }}

2、用戶用戶同意授權,獲取微信服務器傳過來的倆參數: code、state。其中:

APPID: 微信開發者測試賬號
REDIRECT_URI: 同意授權后跳轉的 URL

/** * 第一步:用戶同意授權,獲取code(引導關注者打開如下頁面:) * 獲取 code、state */ public static String getStartURLToGetCode() {  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"; takenUrl= takenUrl.replace("APPID", Param.APPID); takenUrl= takenUrl.replace("REDIRECT_URI", URL.encode(Param.REDIRECT_URI)); //FIXME : snsapi_userinfo takenUrl= takenUrl.replace("SCOPE", "snsapi_userinfo"); System.out.println(takenUrl); return takenUrl; }

3、獲取微信用戶的 access_token、openid

APPID:微信開發者測試賬號
secret:微信開發者測試賬號密碼
code::上一步的 code

/** * 獲取access_token、openid * 第二步:通過code獲取access_token * @param code url = "https://api.weixin.qq.com/sns/oauth2/access_token *   ?appid=APPID *   &secret=SECRET *   &code=CODE *   &grant_type=authorization_code" * */ public static OAuthInfo getAccess_token(String code){   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", Param.APPID); authUrl = authUrl.replace("SECRET", Param.SECRET); authUrl = authUrl.replace("CODE", code); String jsonString = HTTPRequestUtil.sendPost(authUrl,""); System.out.println("jsonString: " + jsonString); OAuthInfo auth = null; try {  auth = (OAuthInfo) JacksonUtil.parseJSONToObject(OAuthInfo.class, jsonString); } catch (Exception e) {  e.printStackTrace(); } return auth; }

4、在數據庫中查詢 openid

我是定義了一個對象 OauthInfo,包括 openid(用戶的標識)屬性。。。

查詢時庫中存在就直接登錄啦,不存在就先去登錄頁,將登錄賬號同 openid 綁定。

/** * 微信引導頁進入的方法 * @return */ @RequestMapping("/loginByWeiXin") public String loginByWeiXin(HttpServletRequest request, Map<String, Object> map) { // 微信接口自帶 2 個參數 String code = request.getParameter("code"); String state = request.getParameter("state"); System.out.println("code = " + code + ", state = " + state);  if(code != null && !"".equals(code)) {  // 授權成功, 微信獲取用戶openID  OAuthInfo authInfo = WeiXinUtil.getAccess_token(code);  String openid = authInfo.getOpenid();  String access_token = authInfo.getAccess_token();    if(access_token == null) {  // Code 使用過 異常  System.out.println("Code 使用過 異常.....");  return "redirect:" + WeiXinUtil.getStartURLToGetCode();  }    // 數據庫中查詢微信號是否綁定平臺賬號  SysUser sysUser = weiXinService.getUserByWeiXinID(openid);  if(sysUser == null) {  String randomStr = StringUtil.getRandomString(50);  request.getSession().setAttribute(openid, randomStr);  // 尚未綁定賬號  System.out.println("尚未綁定賬號.....");  return "redirect:/index.jsp?openid=" + openid + "&state=" + randomStr;  }  userController.doSomeLoginWorkToHomePage(sysUser.getMcid(), map);  // 登錄成功  return "homePage"; }  // 未授權 return "redirect:" + WeiXinUtil.getStartURLToGetCode();  }

四、踩過的坑

1、access_token 與普通 access_token。這尼瑪是啥關系?

答:access_token: 登錄授權會得到一個 access_token, 這個是用于標識登錄用戶(沒有使用次數限制),綁定流程中用的都是 登錄 access_token。

普通 access_token:   調用其它微信應用接口時需要帶上的 access_token,普通 access_token時效為 2H, 每個應用每天調用次數<= 2000次。(實際開發時必須加以處理。。我采用的是 quartz 調度^^)

2、uuid、openid。我該綁定uuid?不不不,openid?不不,到底綁哪個?

答:uuid:微信號綁定后才有的標識,對于任何應用該微信賬號的 uuid 均相同。同一個微信賬號不同的應用 uuid 相同。

  openid:微信號對于每個應用均有一個不變的 openid,同一個微信賬號不同的應用 openid 不同。

五、參考的鏈接!感謝!!

Java微信公眾平臺開發(1) 接入微信公眾平臺

文中也許會存在我理解上的錯誤,望各位不吝賜教,小弟定會加以改正。

更多精彩內容請點擊《java微信開發教程匯總》歡迎大家學習閱讀。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 毛片视频播放 | 久久国产精品电影 | 免费在线观看国产精品 | 欧洲精品久久 | 黄色作爱视频 | 精品一区二区久久久久久久网精 | 欧美另类综合 | 亚洲精品在线观看免费 | 伊人一二三四区 | 国产一区二区免费在线观看 | 悠悠成人资源亚洲一区二区 | 精品久久久久久久久久 | www.99xxxx.com| 久久91亚洲精品久久91综合 | 精品国产一区二区在线观看 | 亚洲精品a级 | 99欧美精品 | 草人人 | 九九热精品在线视频 | 黄色高清视频网站 | 国产伊人色 | 在线成人一区二区 | 今井夏帆av一区二区 | 久久久久久三区 | free国产hd老熟bbw | 一级电影免费看 | 精品一区二区三区毛片 | 久久99网 | 国产色片| 久久国产精品二国产精品 | 一级做人爱c黑人影片 | 久久精品亚洲精品国产欧美kt∨ | 国产三级在线视频观看 | 亚洲视频在线观看免费视频 | 久久精品性视频 | 亚洲最新黄色网址 | 欧美成人免费电影 | 亚洲精品一二三区 | 日本aaaa片毛片免费观蜜桃 | 久草最新在线 | 日韩做爰视频免费 |