溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

java微信掃描公眾號(hào)二維碼實(shí)現(xiàn)登陸功能

發(fā)布時(shí)間:2020-10-23 18:28:03 來(lái)源:腳本之家 閱讀:330 作者:zhengshuo 欄目:編程語(yǔ)言

本文實(shí)例為大家分享了java微信掃描公眾號(hào)二維碼實(shí)現(xiàn)登陸的具體代碼,供大家參考,具體內(nèi)容如下

前提條件:

1.微信公眾平臺(tái)為服務(wù)號(hào),
2.服務(wù)號(hào)實(shí)現(xiàn)了賬號(hào)綁定功能,即將open_id 與業(yè)務(wù)系統(tǒng)中的用戶(hù)名有對(duì)應(yīng)關(guān)系

具體實(shí)現(xiàn)原理:

1.用戶(hù)訪問(wèn)業(yè)務(wù)系統(tǒng)登陸頁(yè)時(shí),調(diào)用二維碼接口,獲得二維碼的ticketid,同時(shí)將sessionid,ticketid和二維碼的seceneid保存
2.返回登陸頁(yè)時(shí),根據(jù)ticketid獲得微信二維碼
3.頁(yè)面通過(guò)ajax發(fā)送請(qǐng)求,判斷是否已經(jīng)掃描成功。
4.公眾平臺(tái)服務(wù)監(jiān)測(cè)到掃描事件,更新seceneid中掃描二維碼的業(yè)務(wù)系統(tǒng)用戶(hù)名
5.當(dāng)ajax監(jiān)測(cè)到掃描成功,并返回有業(yè)務(wù)系統(tǒng)用戶(hù)名,即可做模擬登陸!

具體代碼:

根據(jù)sceneID獲取,獲取ticketId, sceneId可以為sessionID,或者自定義的其他任何值,但必須保證不重復(fù)

注意:這里請(qǐng)求的type可以為臨時(shí)二維碼或永久二維碼,具體區(qū)別可以參看微信公眾平臺(tái)的開(kāi)發(fā)者文檔。

public static String getSceneTicket(String type,String sceneId){ 
  WxScene scene = new WxScene(); 
  scene.setAction_name(type); 
  scene.setSceneId(Integer.parseInt(sceneId)); 
  scene.setExpire_seconds(1800); 
  String jsonScene = JSONObject.fromObject(scene).toString(); 
  String url = WeixinContents.qr_scene_ticket_url.replaceAll("ACCESS_TOKEN", getAccessToken(WeixinContents.appid,WeixinContents.appsecret).getToken()); 
  System.out.println(jsonScene); 
  JSONObject jsonObject = httpRequest(url, "POST", jsonScene); 
  int result = 0; 
  String ticket = ""; 
  if (null != jsonObject) { 
   if (jsonObject.containsKey("errcode")) { 
    result = jsonObject.getInt("errcode"); 
   }else{ 
    ticket = jsonObject.getString("ticket"); 
   } 
  } 
  return ticket; 
 } 

2.掃描二維碼登陸的幾個(gè)action

@ActionKey("/") 
 @ClearInterceptor(ClearLayer.ALL) 
 public void index() { 
  LoginUser u = (LoginUser)getSessionAttr("LoginUser"); 
  setAttr("root", this.getRequest().getContextPath()); 
  if(null==u){ 
   setAttr("ticketId",wxTicket()); 
   render("/WEB-INF/login.html"); 
  }else{ 
   redirect("/index"); 
  } 
 } 
 private String wxTicket() { 
  int sceneId = Db.queryInt("select SEQ_WX_SCENE.Nextval from dual"); 
  String ticketId = WeixinHttpUtils.getSceneTicket("QR_SCENE", sceneId+""); 
  setSessionAttr("ticketId",ticketId); 
  setSessionAttr("sessionId",this.getRequest().getSession().getId()); 
  String sql = "insert into wx_scence_logon(id,sessionid,ticketid,SCENCE_ID) values(sys_guid(),?,?,?)"; 
  Db.update(sql,this.getRequest().getSession().getId(),ticketId,sceneId); 
  return ticketId; 
 } 
 @ActionKey("/cxTicket") 
 @ClearInterceptor(ClearLayer.ALL) 
 public void cxTicket(){ 
  String ticketId = getPara("ticketId"); 
  String sessionid = getPara("sessionId"); 
  int i=0; 
  while(i<10){ 
   Record r = Db.findFirst("select id from wx_scence_logon l,wx_user u where u.open_id = l.open_id and l.ticketid = ? and l.sessionId = ?",ticketId,sessionid); 
   if(r!=null&&StringUtils.isNotEmpty(r.getStr("id"))){ 
     setAttr("success","1"); 
     setAttr("logonId",r.getStr("id")); 
     break; 
   }else{ 
    setAttr("success","0"); 
    try { 
     Thread.sleep(5000); 
     i++; 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
   } 
  } 
  render(new JsonRender().forIE()); 
 } 
 @ActionKey("/ticketLogon") 
 @ClearInterceptor(ClearLayer.ALL) 
 public void ticketLogon(){ 
  String id = getPara("ticketId"); 
  Record r = Db.findFirst("select user_id as username from wx_scence_logon l,wx_user u where u.open_id = l.open_id and l.id = ?",id); 
  if(r!=null&&StringUtils.isNotEmpty(r.getStr("username"))){ 
   String username = r.getStr("username"); 
   LoginUser user = LoginUser.dao.findFirst("select user_id,xm,department_id,departmentname,userpw from gy_user u where u.username = ? and u.userlockstate = '1' ",username); 
   String permSql = "select distinct p.* from hr_user_role t,hr_role r,hr_role_perm m,hr_perms p where t.role_id = r.id and m.role_id = r.id and m.perm_id = p.id and user_id = ?"; 
   List<Record> perms = Db.find(permSql,new Object[]{user.getStr("user_id")}); 
   if(perms!=null&&perms.size()>0){ 
    this.getSession().removeAttribute("USER_PERMS"); 
    setSessionAttr("USER_PERMS",perms); 
    getRequest().getSession().setAttribute("LoginUser",user); 
   } 
  } 
  this.redirect("/main"); 
 } 

3. 登陸頁(yè)面

二維碼顯示
<img src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=${ticketId!}" width="250px"/> 

定時(shí)查詢(xún)掃描狀態(tài)

function wxCxTikcet(){ 
 $.getJSON("${root!}/cxTicket",{ticketId:"${ticketId}",sessionId:"${sessionId}"},function(data){ 
  //alert(data.success=="1"); 
  if(data.success=='1'){ 
   _logon(data.logonId); 
   //window.location.href="${root!}/ticketLogon/" rel="external nofollow" +data.logonId; 
  } 
 }); 
} 
var t_int = window.setInterval("wxCxTikcet()",5000); 

4.公眾平臺(tái)代碼

}else if(eventType.equalsIgnoreCase(MessageUtil.EVENT_TYPE_SCAN)){ 
       String scene_id = eventKey; 
       if(Integer.parseInt(eventKey)==0){ 
        respContent = "掃描參數(shù)出錯(cuò)!請(qǐng)刷新重試!"; 
       }else{ 
        respContent = getSceneContent(scene_id,fromUserName); 
       } 
     } 

private static String getSceneContent(String sceneId,String fromUserName){ 
  String sql ="select * from WX_SCENCE_LOGON where scence_id = ?"; 
  Record r = Db.findFirst(sql,sceneId); 
  if(r!=null){ 
   String updateSql = "update wx_scence_logon set open_id = ? where id = ?"; 
   Db.update(updateSql,fromUserName,r.getStr("id")); 
   return "您已成功登陸***系統(tǒng)!"; 
  } 
} 

說(shuō)明,框架使用jfinal 1.5

大家可以參考專(zhuān)題:java二維碼進(jìn)行學(xué)習(xí)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI