- 浏览: 103543 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
sheungxin:
@仑山中鸟 @zl_xzl 不好意思,代码已丢。。。自己动手试 ...
自定义类加载器与spring的集成 -
昆仑山中鸟:
同求代码,看的云里雾里。自定义监听器WebContextLis ...
自定义类加载器与spring的集成 -
xzl_xzl:
代码都是片段,能否提供下具体的代码,正好我也有类似的需求。但我 ...
自定义类加载器与spring的集成 -
sheungxin:
@add2ws 我试下,多谢!
基于oracle的增量数据采集实现总结 -
add2ws:
用得着这么麻烦么,直接用kettle插入更新,配合oracle ...
基于oracle的增量数据采集实现总结
- 参考文档
http://blog.csdn.net/jiang1013nan/article/details/22651439
实现手机扫描二维码进行登录
http://www.daxueit.com/article/2581.html
- 思路梳理:
2、APP端扫描二维码,校验二维码有效性,通知服务端扫描成功(APP需要处于登录状态)
3、服务端收到扫描成功通知(可校验),推送消息(推送机制)给浏览器端,二维码转换状态为已扫描
4、同时返回授权地址到APP端,由APP端跳转到授权界面,具有确认、取消两种操作,点击传递唯一码、token、状态到服务端
5、服务端接收到授权结果,通知给浏览器改为状态
6、APP端接收到授权结果,改变状态
7、二维码增加失效机制
- 存在的思考
● 解决方案:改唯一码为“授权链接+唯一码”,客户端扫描后会先校验再跳转到授权页面;非客户端扫描无校验信息及客户端登录信息,由授权页面直接跳转到友好提示页面,提供APP安装检测、APP启动、APP下载等。
2. sessionid直接放在二维码唯一码中,加密确保安全性
3. 服务端未维护二维码、当前会话的关系,无法有效的进行二维码有效性校验(步骤5,授权请求中也需要校验)
4. 如何使用临时时间戳?
5. 服务器端用户session保存机制
6. 消息推送:浏览器轮询、服务端轮询、MQ、第三方推送
- 简单实现
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); } }
附件中为完整的代码片段
- qrcode.zip (6.2 KB)
- 下载次数: 21
- QRcode.jar (1 MB)
- 下载次数: 17
相关推荐
java实现手机扫描二维码后网站跳转新页面,提供zxing和hutools的方式实现二维码的生成,动态刷新,验证跳转功能。
在使用这个时,要把http协议改成https协议
扫描同一张二维码,会根据客户端类型(安卓,ios)自动下载想对应的app。内容包括:源码、扫码下载代码说明等。
JS调用安卓手机摄像头扫描二维码
安卓手机版本在6.0以后需要动态获取相机权限 1.获取相机权限 <!-- 获取手机相机的权限 --> 2.添加依赖 implementation 'cn.yipianfengye.android:zxing-library:2.2' 3.activity_main.xml <?xml ...
unigui电脑微信扫码登陆,unigui开发
主要为大家详细介绍了Vue实现手机扫描二维码预览页面效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
网页调用摄像头实现二维码扫描功能,很强大,,,网上搜的很多H5调用MUI的二维码扫描功能都用不了,不支持,。这个很强大,实现了网页调用摄像头进行二维码扫描功能,帅!
基于H5流媒体,调用手机摄像头实现扫码 实现原理:H5调用系统媒体设备》选择前置摄像头》捕获视频流》渲染到视频播放器》定时抓取视频截图》渲染到canvas》转换为图片流》使用图像识别库对图片流进行二维码识别 优势...
显示登录扫描二维码,使用base64编码显示二维码 二维码显示成功后,使用SSE方式开启二维码session监听状态,状态:0 二维码生成成功 ,1 手机端扫码成功 2手机端确认登录 -1 sessionId过期失效 调用登录接口,跳转到...
avigator.mediaDevices....里面的testh.html本来想实现网页调用摄像头解析扫描的二维码。目前用不到了基本完成了。希望对别人有帮助。 调用开启手机后头的摄像头,把图像扫描到image上 然后用脚本把图片解析出来。
最近有朋友问小编这样一个问题,先给大家说下项目需求:扫描二维码打开app如果用户没有这个app则提示它跳转。 用网页直接来调用app是不打可能的,必须原生那边先做一些配置。 首先,安卓和苹果的调用方法是不同的。 ...
通过改动简化zxing开源项目实现摇晃手机,扫描二维码并显示。附中文注释,数据库创建。
项目使用环境以及工具: Eclipse,JDK1.7,struts2,HTML5,Jquery,...HTML5技术支持WebApp在手机上拍照,显示在页面上并上传到服务器。这是手机微博应用中常见的功能,当然你也可以在其它类型应用中适当使用此技术。
简单用zxing实现二维码扫描功能,直接调用摄像头,读者也可以根据里面的代码进行将扫描得到的二维码进行存储。
本程序使用VS2019进行制作,利用ASP.NET+H5+Zxing+js+sweetalert2 实现手机端和PC端调用摄像头进行二维码扫一扫,实现前端采集数据,后端进行分析并向前端传值,pc端可运行,手机端鸿蒙系统,安卓,IOS4.X以上均可...
1.这个是QT开发安卓 2.在QWidget 中通过按钮触发 3.QWidget与QML混合编程 4.使用ZXing库 5.支持开启闪光灯
用ionic+angular开发移动app,主要实现了二维码扫描,上传服务器那边用$jsonp,具体根据个人功能要进行修改,应该不难,相信可以自行解决。