`
sheungxin
  • 浏览: 103543 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

实现手机扫描二维码登录

阅读更多
  • 参考文档
实现网站二维码扫描登录
http://blog.csdn.net/jiang1013nan/article/details/22651439

实现手机扫描二维码进行登录
http://www.daxueit.com/article/2581.html

  • 思路梳理:
1、生成一个唯一码(标识符+sessionid+时间戳,对称加密),转换为二维码。

2、APP端扫描二维码,校验二维码有效性,通知服务端扫描成功(APP需要处于登录状态)

3、服务端收到扫描成功通知(可校验),推送消息(推送机制)给浏览器端,二维码转换状态为已扫描

4、同时返回授权地址到APP端,由APP端跳转到授权界面,具有确认、取消两种操作,点击传递唯一码、token、状态到服务端

5、服务端接收到授权结果,通知给浏览器改为状态

6、APP端接收到授权结果,改变状态

7、二维码增加失效机制

  • 存在的思考
  1. 非客户端扫描结果为字符串
  ● 解决方案:改唯一码为“授权链接+唯一码”,客户端扫描后会先校验再跳转到授权页面;非客户端扫描无校验信息及客户端登录信息,由授权页面直接跳转到友好提示页面,提供APP安装检测、APP启动、APP下载等。
  2. sessionid直接放在二维码唯一码中,加密确保安全性
  3. 服务端未维护二维码、当前会话的关系,无法有效的进行二维码有效性校验(步骤5,授权请求中也需要校验)
  4. 如何使用临时时间戳?
  5. 服务器端用户session保存机制
  6. 消息推送:浏览器轮询、服务端轮询、MQ、第三方推送

  • 简单实现
1、二维码生成:QRcode、zixing,下列代码中使用QRcode,需要注意的二维码排错率、尺寸、存储内容大小三者关系,否则可能存在二维码解析失败的问题
import java.awt.Color;  
import java.awt.Graphics2D;  
import java.awt.Image;
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  

import javax.imageio.ImageIO;  

import jp.sourceforge.qrcode.QRCodeDecoder;  
import jp.sourceforge.qrcode.exception.DecodingFailedException;
import com.swetake.util.Qrcode;  
   
public class TwoDimensionCode {  
       
    /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param imgPath 图片路径 
     */ 
    public void encoderQRCode(String content, String imgPath) {  
        this.encoderQRCode(content, imgPath, "png", 7, null);  
    }  
       
    /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param output 输出流 
     */ 
    public void encoderQRCode(String content, OutputStream output) {  
        this.encoderQRCode(content, output, "png", 7, null);  
    }  
       
    /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param imgPath 图片路径 
     * @param imgType 图片类型 
     */ 
    public void encoderQRCode(String content, String imgPath, String imgType) {  
        this.encoderQRCode(content, imgPath, imgType, 7, null);  
    }  
       
    /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param output 输出流 
     * @param imgType 图片类型 
     */ 
    public void encoderQRCode(String content, OutputStream output, String imgType) {  
        this.encoderQRCode(content, output, imgType, 7, null);  
    }  
   
    /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param imgPath 图片路径 
     * @param imgType 图片类型 
     * @param size 二维码尺寸 
     */ 
    public void encoderQRCode(String content, String imgPath, String imgType, int size,String logoPath) {  
        try {  
            BufferedImage bufImg = this.qRCodeCommon(content, imgType, size,logoPath);  
               
            File imgFile = new File(imgPath);
            if (!imgFile.exists()){
                imgFile.mkdirs();
            }
            // 生成二维码QRCode图片  
            ImageIO.write(bufImg, imgType, imgFile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
   
    /** 
     * 生成二维码(QRCode)图片 
     * @param content 存储内容 
     * @param output 输出流 
     * @param imgType 图片类型 
     * @param size 二维码尺寸 
     */ 
    public void encoderQRCode(String content, OutputStream output, String imgType, int size,String logoPath) {  
        try {  
            BufferedImage bufImg = this.qRCodeCommon(content, imgType, size, logoPath);  
            // 生成二维码QRCode图片  
            ImageIO.write(bufImg, imgType, output);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
       
    /** 
     * 生成二维码(QRCode)图片的公共方法 
     * @param content 存储内容 
     * @param imgType 图片类型 
     * @param size 二维码尺寸 
     * @return 
     */ 
    private BufferedImage qRCodeCommon(String content, String imgType,int size,String logoPath) {  
        BufferedImage bufImg = null;  
        try {  
            Qrcode qrcodeHandler = new Qrcode();  
            // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小  
            qrcodeHandler.setQrcodeErrorCorrect('M');  
            qrcodeHandler.setQrcodeEncodeMode('B');  
            // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大  
            qrcodeHandler.setQrcodeVersion(size);  
            // 获得内容的字节数组,设置编码格式  
            byte[] contentBytes = content.getBytes("utf-8");  
            // 图片尺寸  
            int imgSize = 67 + 12 * (size - 1);  
            bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);  
            Graphics2D gs = bufImg.createGraphics();  
            // 设置背景颜色  
            gs.setBackground(Color.WHITE);  
            gs.clearRect(0, 0, imgSize, imgSize);  
   
            // 设定图像颜色> BLACK  
            gs.setColor(Color.BLACK);  
            // 设置偏移量,不设置可能导致解析出错  
            int pixoff = 2;  
            // 输出内容> 二维码  
            if (contentBytes.length > 0 && contentBytes.length < 800) {  
                boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes);  
                for (int i = 0; i < codeOut.length; i++) {  
                    for (int j = 0; j < codeOut.length; j++) {  
                        if (codeOut[j][i]) {  
                            gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);  
                        }  
                    }  
                }  
            } else {  
                throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800].");  
            }  
            
            if(logoPath!=null){
            	File logoFile=new File(logoPath);
                if(logoFile.isFile()&&logoFile.exists()){
                	 Image logo = ImageIO.read(logoFile);//实例化一个Image对象。  
                     int widthLogo = logo.getWidth(null)>bufImg.getWidth()*2/10?(bufImg.getWidth()*2/10):logo.getWidth(null);  
                     int heightLogo = logo.getHeight(null)>bufImg.getHeight()*2/10?(bufImg.getHeight()*2/10):logo.getWidth(null);  
                     //logo放在中心 
                     int x = (bufImg.getWidth() - widthLogo) / 2;  
                     int y = (bufImg.getHeight() - heightLogo) / 2;  
                     gs.drawImage(logo, x, y, widthLogo, heightLogo, null);  
                }
            }
            gs.dispose();  
            bufImg.flush();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return bufImg;  
    }  
       
    /** 
     * 解析二维码(QRCode) 
     * @param imgPath 图片路径 
     * @return 
     */ 
    public String decoderQRCode(String imgPath) {  
        // QRCode 二维码图片的文件  
        File imageFile = new File(imgPath);  
        BufferedImage bufImg = null;  
        String content = null;  
        try {  
            bufImg = ImageIO.read(imageFile);  
            QRCodeDecoder decoder = new QRCodeDecoder();  
            content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8");   
        } catch (IOException e) {  
            System.out.println("Error: " + e.getMessage());  
            e.printStackTrace();  
        } catch (DecodingFailedException dfe) {  
            System.out.println("Error: " + dfe.getMessage());  
            dfe.printStackTrace();  
        }  
        return content;  
    }  
       
    /** 
     * 解析二维码(QRCode) 
     * @param input 输入流 
     * @return 
     */ 
    public String decoderQRCode(InputStream input) {  
        BufferedImage bufImg = null;  
        String content = null;  
        try {  
            bufImg = ImageIO.read(input);  
            QRCodeDecoder decoder = new QRCodeDecoder();  
            content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8");   
        } catch (IOException e) {  
            System.out.println("Error: " + e.getMessage());  
            e.printStackTrace();  
        } catch (DecodingFailedException dfe) {  
            System.out.println("Error: " + dfe.getMessage());  
            dfe.printStackTrace();  
        }  
        return content;  
    }    
}


2、接口处理类:样例中通知浏览器使用前端轮询,建议改为后端轮询、推送(MQ、第三方推送等);样例中session使用自定义session管理器进行维护
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import com.hec.interceptor.MySessionContext;
import com.hec.util.MethodException;
import com.hec.util.StringUtil;
import com.hec.util.qrcode.QrcodeContentUtils;
import com.hec.util.qrcode.TwoDimensionCode;
import com.opensymphony.xwork2.ActionSupport;

@Controller
public class QrcodeLoginAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private Map<String, Object> resultMap;

	//@MethodException(remark = "", description = "获取登录二维码")
	public String show() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response=ServletActionContext.getResponse();
		ServletOutputStream out = null;
        try {
        	out=response.getOutputStream();
        	MySessionContext.getSession(request.getSession().getId()).setAttribute("qrcodeStatus", 0);
            //生成二维码
            TwoDimensionCode handler = new TwoDimensionCode();
            handler.encoderQRCode(QrcodeContentUtils.uniqueEncrypUrl(request.getSession().getId()), out, "png", 14, QrcodeContentUtils.getWebPath()+"/website/skin/default/img/logo.png");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
        return null;
	}

	@MethodException(remark = "", description = "二维码扫描确认")
	public String confirm(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String key=request.getParameter("key");
		String token=request.getParameter("token");
		if(StringUtil.isNotNullOrEmpty(key)){//二维码唯一码
			//确保为登录状态
			HttpSession appSession=MySessionContext.getSession(token);
			if(appSession!=null&&appSession.getAttribute("userinfo")!=null){
				resultMap=new HashMap<String, Object>();
				String mes_code=null;
				String[] array=QrcodeContentUtils.parseEncrypUniqueCode(key);
				if(array!=null){
					mes_code=QrcodeContentUtils.checkEncrypUniqueCode(array);
					if(mes_code.equals("suc")){
						//更新二维码扫描登录状态为已扫描
						MySessionContext.getSession(array[1]).setAttribute("qrcodeStatus", 1);
					}
				}else{
					mes_code= "err_001";//key格式错误
				}
				resultMap.put("status", mes_code);
				return "success";
			}else{
				return "error";//跳转到友好提示页
			}
		}else{
			return "error";//跳转到友好提示页
		}
		
	}

	//@MethodException(remark = "", description = "二维码扫描结果轮询")
	public String polling(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		Integer qrcodeStatus=(Integer) session.getAttribute("qrcodeStatus");
		resultMap=new HashMap<String, Object>();
		resultMap.put("qrcodeStatus", qrcodeStatus);
		return "success";
	}
	
	@MethodException(remark = "", description = "授权二维码登录")
	public String grantAuth(){
		resultMap=new HashMap<String, Object>();
		String mes_code=null;
		HttpServletRequest request = ServletActionContext.getRequest();
		String key=request.getParameter("key");
		String token=request.getParameter("token");
		if(StringUtil.isNotNullOrEmpty(key)){
			//确保为登录状态
			HttpSession appSession=MySessionContext.getSession(token);
			if(appSession!=null&&appSession.getAttribute("userinfo")!=null){
				String[] array=QrcodeContentUtils.parseEncrypUniqueCode(key);
				if(array!=null){
					mes_code=QrcodeContentUtils.checkEncrypUniqueCode(array);
					if(mes_code.equals("suc")){
						HttpSession pcSession=MySessionContext.getSession(array[1]);
						//更新二维码扫描登录状态为已授权
						pcSession.setAttribute("qrcodeStatus", 2);
						//复制 APP中session中用户信息到PC端
						pcSession.setAttribute("userinfo", appSession.getAttribute("userinfo"));
					}
					
				}else{
					mes_code= "err_001";//key格式错误
				}
			}else{
				mes_code= "err_004";//客户端未登录
			}
		}else{
			mes_code= "err_005";//缺少参数 key
		}
		resultMap=new HashMap<String, Object>();
		resultMap.put("status", mes_code);
		return "success";
	} 
	
	@MethodException(remark = "", description = "拒绝授权二维码登录")
	public String refuseAuth(){
		resultMap=new HashMap<String, Object>();
		String mes_code=null;
		HttpServletRequest request = ServletActionContext.getRequest();
		String key=request.getParameter("key");
		String token=request.getParameter("token");
		if(StringUtil.isNotNullOrEmpty(key)){
			//确保为登录状态
			HttpSession appSession=MySessionContext.getSession(token);
			if(appSession!=null&&appSession.getAttribute("userinfo")!=null){
				String[] array=QrcodeContentUtils.parseEncrypUniqueCode(key);
				if(array!=null){
					mes_code=QrcodeContentUtils.checkEncrypUniqueCode(array);
					if(mes_code.equals("suc")){
						HttpSession pcSession=MySessionContext.getSession(array[1]);
						//更新二维码扫描登录状态为拒绝
						pcSession.setAttribute("qrcodeStatus", 3);
					}
				}else{
					mes_code= "err_001";//key格式错误
				}
			}else{
				mes_code= "err_004";//客户端未登录
			}
		}else{
			mes_code= "err_005";//缺少参数 key
		}
		resultMap=new HashMap<String, Object>();
		resultMap.put("status", mes_code);
		return "success";
	}
	
	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}
}


3、自定义session管理器:集群环境存在session共享问题、性能不佳,可引入第三方cache进行管理
import java.util.HashSet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionControl implements HttpSessionListener {

	@Override
	public void sessionCreated(HttpSessionEvent event) {
		MySessionContext.addSession(event.getSession());
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent event) {
		HttpSession session = event.getSession();
		MySessionContext.delSession(session);
	}

}

import java.util.HashMap;

import javax.servlet.http.HttpSession;

public class MySessionContext {

	private static HashMap<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();

    public static synchronized void addSession(HttpSession session) {
        if (session != null) {
        	sessionMap.put(session.getId(), session);
        }
    }

    public static synchronized void delSession(HttpSession session) {
        if (session != null) {
        	sessionMap.remove(session.getId());
        }
    }

    public static synchronized HttpSession getSession(String session_id) {
        if (session_id == null)
        	return null;
        return sessionMap.get(session_id);
    }
}


附件中为完整的代码片段
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics