12-dimensional Code scan login
1,web to generate a QR code, pass the UUID, and deposit into the database
2,web End Rotation Query information, whether there is a database scan QR code information
3, mobile phone scan QR code, get uuid, pass user name, password Web side
4,web Query the database user information (userlogin), the login information (user name, password) into the database.
5,web rotation found the user login information, complete information, the landing was successful.
1.1 Web Side 1.1.1 Import Dependency package (QR code generation Qrcode.jar) jar package Download: Qrcode.jar
1.1.2 User Login Information entity class (Userlogin.java)
@Entity @table (name = "Userlogin") public class Userlogin {@Id @generatedvalue (generator = "Userloginidgenerator") @ Genericgenerator (name = "Userloginidgenerator", strategy = "uuid") private string ID; @Columnprivate string name;@ columnprivate string password; @Columnprivate int uuid;public string getId () {return ID;} public void SetId (String id) {this.id = ID;} public int Getuuid () {return UUID;} public void Setuuid (int uuid) {this.uuid = uuid;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}
1.1.3 Generating QR code (GETQRCODESERVLET.JAVA)
Import Java.io.ioexception;import java.io.printwriter;import java.util.date;import javax.servlet.ServletException; Import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.engine.transaction.jta.platform.internal.jonasjtaplatform;import Sun.org.mozilla.javascript.internal.json.jsonparser;import Com.alibaba.fastjson.jsonobject;import Com.jie.qrcode.util.hibernateutil;import com.jie.qrcode.util.qrcodeutil;import com.jie.qrcode.vo.UserLogin;/** * @ ClassName:GetQrCodeServlet.java * @Description: TODO (generate QR Code) * @author Yangjie * @version V1.0 * @Date 2016-5-16 morning 11:05:2 7 */public class Getqrcodeservlet extends HttpServlet {private static final long serialversionuid = 1l;protected void doGe T (httpservletrequest request,httpservletresponse response) throws Servletexception, IOException {doPost (Request, Response);} protected void DoPost (HttpServletRequest request,httpservletresponse response) throws Servletexception, IOException {printwriter out = Response.getwriter ();//generate unique Idint uuid = (int) (Math.random () * 100000);//QR code content//String content =//"http://192.168.0.19 9:8080/qrcodelogin/phoneloginservlet?uuid= "//+ uuid; Jsonobject obj = new Jsonobject (); Obj.put ("url", "Http://192.168.0.199:8080/QrCodeLogin/PhoneLoginServlet"); obj.put ("UUID", uuid); String content = obj.tostring (); System.out.println ("obj:" + content);//Generate two-dimensional code string imgname = uuid + "_" + (int) (New Date (). GetTime ()/+) + ". png"; String contextroot = Request.getcontextpath (); String Webpath = Request.getsession (). Getservletcontext (). Getrealpath ("/"); System.out.println (Webpath); String Imgpath = Webpath + "/img/qrcodeimg/" + imgname; String Logopath = Webpath + "/img/logo.png"; Qrcodeutil handler = new Qrcodeutil ();//Handler.encoderqrcode (content, Imgpath, "PNG"); Handler.encoderqrcode (content , Imgpath, Logopath, "PNG"),//UUID save sessionfactory SF = Hibernateutil.Getsessionfacotry (); Session session = Sf.opensession (); session.begintransaction ();//transaction start userlogin Userlogin = new Userlogin (); Userlogin.setuuid (UUID); Session.save (userlogin);//The UUID is saved to the database Session.gettransaction (). commit ();// Transaction commit session.close (); sf = null;//generated picture access address string qrcodeimg = "http://192.168.0.199:8080/QrCodeLogin/img/qrCodeImg/" + Imgname; String jsonstr = "{\" uuid\ ":" + uuid + ", \" qrcodeimg\ ": \" "+ qrcodeimg+" \ "}"; Out.print (JSONSTR); Out.flush (); Out.close () ;}}
1.1.4 with long connection, check login status (Longconnectioncheckservlet.java)
Import Java.io.ioexception;import java.io.printwriter;import Java.util.date;import Java.util.list;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.hibernate.criteria;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.criterion.restrictions;import Com.jie.qrcode.util.hibernateutil;import Com.jie.qrcode.vo.UserLogin ;/** * @ClassName: Longconnectioncheckservlet.java * @Description: TODO (check login status with long connection) * @author Yangjie * @v Ersion V1.0 * @Date 2016-5-16 morning 11:09:32 */public class Longconnectioncheckservlet extends HttpServlet {private static final long serialversionuid = 1l;public void doget (HttpServletRequest request, HttpServletResponse Respon SE) throws Servletexception, IOException {doPost (request, response);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws SERVLEtexception, IOException {int uuid = Integer.parseint (Request.getparameter ("uuid")); String jsonstr = ""; System.out.println ("in"); System.out.println ("UUID:" + uuid); long inTime = new Date (). GetTime (); Boolean bool = True;while (bool) {try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} Querying database Login table information based on UUID string username = ""; Sessionfactory SF = Hibernateutil.getsessionfacotry (); Session session = Sf.opensession (); Criteria = Session.createcriteria (Userlogin.class), Criteria.add (Restrictions.eq ("uuid", uuid)); list<userlogin> result = Criteria.list (), if (result.size () > 0) {for (Userlogin s:result) {username = S.getname (); if (! "". Equals (username) &&username!=null) {//If the user name information is not empty, the login succeeds bool = False;session.clear (); Session.begintransaction (); Session.delete (s);//delete the QR code login information, indicating that you are logged in Session.gettransaction (). commit (); Jsonstr = "{\" uname\ ": \" "+ Username +" \ "}"; SYSTEM.OUT.PRINTLN (username);}} else {if (New Date (). GetTime ()-inTime >) {bool = fAlse;}} Session.close (); sf = null;} SYSTEM.OUT.PRINTLN ("Login OK:" + jsonstr); PrintWriter out = Response.getwriter (); Out.print (JSONSTR); Out.flush (); Out.close ();}}
1.1.5 receive cell phone QR code return information (Phoneloginservlet.java)
Import Java.io.ioexception;import java.io.printwriter;import java.util.list;import javax.servlet.ServletException; Import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.hibernate.criteria;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.criterion.restrictions;import Com.jie.qrcode.util.hibernateutil;import com.jie.qrcode.vo.userlogin;/** * @ClassName: Phoneloginservlet.java * @ Description:todo (cell phone QR code login) * @author Yangjie * @version V1.0 * @Date 2016-5-16 11:20:16 * * public class Phoneloginservlet extends HttpServlet {private static final long Serialversionuid = 1l;public PHONELOGINSERVL ET () {super ();//TODO auto-generated constructor stub}protected void doget (HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {doPost (request, response);} protected void DoPost (HttpServletRequest request,httpServletresponse response) throws Servletexception, IOException {int uuid = Integer.parseint (Request.getparameter ("uuid ")); String uname = Request.getparameter ("uname"); String upwd = Request.getparameter ("Upwd"); SYSTEM.OUT.PRINTLN (UUID); System.out.println (uname); System.out.println (UPWD);//Verify that the login boolean bool = true;//is logged in Boolean islogin = False;if (bool) {//login information is stored in the database sessionfactory SF = Hibernateutil.getsessionfacotry (); Session session = Sf.opensession (); Criteria = Session.createcriteria (Userlogin.class), Criteria.add (Restrictions.eq ("uuid", uuid)); list<userlogin> result = Criteria.list (), if (result.size () > 0) {for (Userlogin S:result) {s.setname (uname); S.S Etpassword (UPWD); Session.clear (); session.begintransaction ();//Start Transaction session.saveorupdate (s); Session.gettransaction (). commit ();//Transaction commit bool = False;islogin = true;}} else{//does not have the UUID information, indicating that the login, the QR code expires IsLogin = FALSE; System.out.println ("QR code has expired, please refresh the page"); Session.close (); sf = null;} PrintWriter out = Response.getwriter (); Out.print (islogin);//successful Out.flush (); Out.close ();}}
1.1.62-dimensional Code scanning interface (index.html)
<! DOCTYPE html>
1.1.7 Operation effect
1.2 Mobile Port 1.2.1 Import Dependency Engineering barcodelibrary Project download: android QR code scan
1.2.2 Creating a project and introducing a dependency package
1.2.3 Phone QR Code interface (activity_main.xml)<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_ Parent " android:layout_height=" fill_parent " android:orientation=" vertical " android:gravity=" center "> <button android:id=" @+id/btn_scan " android:layout_width=" 120DP " android:layout_height = "Wrap_content" android:layout_gravity= "center" android:gravity= "center" android:background= "@ Android:color/holo_blue_light " android:textcolor=" @android: Color/white " android:text=" Scan Login "/>< /linearlayout>
1.2.4 Mainactivity.javaPackage Com.jie.qrcode;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import Android.widget.toast;import Com.alibaba.fastjson.jsonobject;import Com.lidroid.xutils.httputils;import Com.lidroid.xutils.exception.httpexception;import Com.lidroid.xutils.http.requestparams;import Com.lidroid.xutils.http.responseinfo;import Com.lidroid.xutils.http.callback.requestcallback;import Com.lidroid.xutils.http.client.httprequest;import Com.lidroid.xutils.http.client.httprequest.httpmethod;import Com.zxing.activity.captureactivity;public class Mainactivity extends activity implements Onclicklistener{private Button btnscan; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Btnscan = (Button) Findviewbyid (R.id.btn_scan); Btnscan.setonclicklistener ( this);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubintent Intent = new Intent (this, captureactivity.class); startacti Vityforresult (Intent, 0);} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent data) {//TODO auto-generated method stub Super.onactivityresult (Requestcode, ResultCode, data); if (RESULTCODE==ACTIVITY.RESULT_OK) {String RESULT = Data.getextras (). getString ("result");//Gets the result of the scan//log.i ("result", result); Jsonobject obj = jsonobject.parseobject (result); String uuid = obj.getstring ("uuid"); String Httpurl = obj.getstring ("url"),//toast.maketext (Getapplicationcontext (), result, +). Show ();/ Submit the scanned content to the webpage httputils http = new Httputils (); Requestparams params = new Requestparams ();p arams.addbodyparameter ("uuid", uuid),//LOG.I ("uuid", uuid); Params.addbodyparameter ("uname", "Yangjie");p arams.addbodyparameter ("Upwd", "123456"); Http.send (Httpmethod.post, Httpurl, params, new requestcallback<string> () {@Overridepublic void OnFailure (HttpException arg0, String arg1) {//TODO auto-generated method stublog.i ("Http.send", "Failuer"); Toast.maketext (Getapplicationcontext (), "Login failed, QR code expires, please refresh the page!" ", +). Show ();} @Overridepublic void onsuccess (responseinfo<string> arg0) {//TODO auto-generated method stublog.i ("Http.send", " Success "); Toast.maketext (Getapplicationcontext (), "Landing success!" ", +). Show ();}});}}
1.2.5 Operation effect
1.3 The complete project source code please click to download: QR Code scan Login-Perfect
Android QR Code scan landing Page