1,web端生成二维码,传递uuid,并存入数据库
2,web端轮训查询信息,是否有数据库扫描二维码信息
3,手机端扫描二维码,获取UUID,传递用户名、密码web端
4,web查询数据库中用户信息(UserLogin),将登陆信息(用户名,密码)存入数据库。
5,web轮训查到用户登陆信息,信息完整,则登陆成功。
@Entity
@Table(name = "UserLogin")
public class UserLogin {
@Id
@GeneratedValue(generator = "UserLoginIdGenerator")
@GenericGenerator(name = "UserLoginIdGenerator", strategy = "uuid")
private String id;
@Column
private String name;
@Column
private String password;
@Column
private 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;
}
}
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(生成二维码)
* @author yangjie
* @version V1.0
* @Date 2016-5-16 上午11:05:27
*/
public class GetQrCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
// 生成唯一ID
int uuid = (int) (Math.random() * 100000);
// 二维码内容
// String content =
// "http://192.168.0.199: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);
// 生成二维码
String imgName = uuid + "_" + (int) (new Date().getTime() / 1000)
+ ".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保存
SessionFactory sf = HibernateUtil.getSessionFacotry();
Session session = sf.openSession();
session.beginTransaction();// 事务开始
UserLogin userlogin = new UserLogin();
userlogin.setUuid(uuid);
session.save(userlogin);// 将uuid保存到数据库
session.getTransaction().commit();// 事务提交
session.close();
sf = null;
// 生成的图片访问地址
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();
}
}
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(用长连接,检查登录状态)
* @author yangjie
* @version V1.0
* @Date 2016-5-16 上午11:09:32
*/
public class LongConnectionCheckServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
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(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 根据uuid查询数据库登录表信息
String username = "";
SessionFactory sf = HibernateUtil.getSessionFacotry();
Session session = sf.openSession();
Criteria criteria = session.createCriteria(UserLogin.class);
criteria.add(Restrictions.eq("uuid", uuid));
List result = criteria.list();
if (result.size() > 0) {
for (UserLogin s : result) {
username = s.getName();
if(!"".equals(username)&&username!=null){//如果用户名信息不为空,则登陆成功
bool = false;
session.clear();
session.beginTransaction();
session.delete(s);//删除二维码登录信息,表明已登录
session.getTransaction().commit();
}
jsonStr = "{\"uname\":\"" + username + "\"}";
System.out.println(username);
}
} else {
if (new Date().getTime() - inTime > 5000) {
bool = false;
}
}
session.close();
sf = null;
}
System.out.println("login ok : " + jsonStr);
PrintWriter out = response.getWriter();
out.print(jsonStr);
out.flush();
out.close();
}
}
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(手机二维码登陆)
* @author yangjie
* @version V1.0
* @Date 2016-5-16 上午11:20:16
*/
public class PhoneLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public PhoneLoginServlet() {
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);
// 验证登陆
boolean bool = true;
//是否登陆
boolean islogin = false;
if (bool) {
// 将登陆信息存入数据库
SessionFactory sf = HibernateUtil.getSessionFacotry();
Session session = sf.openSession();
Criteria criteria = session.createCriteria(UserLogin.class);
criteria.add(Restrictions.eq("uuid", uuid));
List result = criteria.list();
if (result.size() > 0) {
for (UserLogin s : result) {
s.setName(uname);
s.setPassword(upwd);
session.clear();
session.beginTransaction();//开始事务
session.saveOrUpdate(s);
session.getTransaction().commit();// 事务提交
bool = false;
islogin = true;
}
}else{//没有该uuid信息,表明已登录,二维码过期
islogin = false;
System.out.println("二维码已过期,请刷新页面");
}
session.close();
sf = null;
}
PrintWriter out = response.getWriter();
out.print(islogin);//传递登陆是否成功
out.flush();
out.close();
}
}
Insert title here
扫描二维码登陆
package 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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnScan = (Button) findViewById(R.id.btn_scan);
btnScan.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, CaptureActivity.class);
startActivityForResult(intent, 0);
}
@Override
protected 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");//获取扫描的结果
//Log.i("result",result);
JSONObject obj = JSONObject.parseObject(result);
String uuid = obj.getString("uuid");
String httpUrl = obj.getString("url");
//Toast.makeText(getApplicationContext(), result, 1000).show();
//将扫描内容提交到网页
HttpUtils http = new HttpUtils();
RequestParams params = new RequestParams();
params.addBodyParameter("uuid", uuid);
//Log.i("uuid",uuid);
params.addBodyParameter("uname", "yangjie");
params.addBodyParameter("upwd", "123456");
http.send(HttpMethod.POST, httpUrl, params , new RequestCallBack() {
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Log.i("http.send","failuer");
Toast.makeText(getApplicationContext(), "登陆失败,二维码过期,请刷新页面!", 1000).show();
}
@Override
public void onSuccess(ResponseInfo arg0) {
// TODO Auto-generated method stub
Log.i("http.send","success");
Toast.makeText(getApplicationContext(), "登陆成功!", 1000).show();
}
});
}
}
}