欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java微信扫描公众号二维码实现登陆功能

程序员文章站 2023-11-12 19:24:52
本文实例为大家分享了java微信扫描公众号二维码实现登陆的具体代码,供大家参考,具体内容如下 前提条件: 1.微信公众平台为服务号, 2.服务号实现了账号绑定...

本文实例为大家分享了java微信扫描公众号二维码实现登陆的具体代码,供大家参考,具体内容如下

前提条件:

1.微信公众平台为服务号,
2.服务号实现了账号绑定功能,即将open_id 与业务系统中的用户名有对应关系

具体实现原理:

1.用户访问业务系统登陆页时,调用二维码接口,获得二维码的ticketid,同时将sessionid,ticketid和二维码的seceneid保存
2.返回登陆页时,根据ticketid获得微信二维码
3.页面通过ajax发送请求,判断是否已经扫描成功。
4.公众平台服务监测到扫描事件,更新seceneid中扫描二维码的业务系统用户名
5.当ajax监测到扫描成功,并返回有业务系统用户名,即可做模拟登陆!

具体代码:

根据sceneid获取,获取ticketid, sceneid可以为sessionid,或者自定义的其他任何值,但必须保证不重复

注意:这里请求的type可以为临时二维码或永久二维码,具体区别可以参看微信公众平台的开发者文档。

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.扫描二维码登陆的几个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. 登陆页面

二维码显示
<img src="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=${ticketid!}" width="250px"/> 

定时查询扫描状态

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.公众平台代码

}else if(eventtype.equalsignorecase(messageutil.event_type_scan)){ 
       string scene_id = eventkey; 
       if(integer.parseint(eventkey)==0){ 
        respcontent = "扫描参数出错!请刷新重试!"; 
       }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 "您已成功登陆***系统!"; 
  } 
} 

说明,框架使用jfinal 1.5

大家可以参考专题:java二维码进行学习

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。