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

Java二维码登录流程实现代码(包含短地址生成,含部分代码)

程序员文章站 2024-03-11 22:02:49
近年来,二维码的使用越来越风生水起,笔者最近手头也遇到了一个需要使用二维码扫码登录网站的活,所以研究了一下这一套机制,并用代码实现了整个流程,接下来就和大家聊聊二维码登录及...

近年来,二维码的使用越来越风生水起,笔者最近手头也遇到了一个需要使用二维码扫码登录网站的活,所以研究了一下这一套机制,并用代码实现了整个流程,接下来就和大家聊聊二维码登录及的那些事儿。

二维码原理

二维码是微信搞起来的,当年微信扫码二维码登录网页微信的时候,感觉很神奇,然而,我们了解了它的原理,也就没那么神奇了。二维码实际上就是通过黑白的点阵包含了一个url请求信息。端上扫码,请求url,做对应的操作。

一般性扫码操作的原理

微信登录、支付宝扫码支付都是这个原理:

1. 请求二维码

桌面端向服务器发起请求一个二维码的。

2. 生成包含唯一id的二维码

桌面端会随机生成一个id,id唯一标识这个二维码,以便后续操作。

3. 端上扫码

移动端扫码二维码,解chu出二维码中的url请求。

4. 移动端发送请求到服务器

移动端向服务器发送url请求,请求中包含两个信息,唯一id标识扫的是哪个码,端上浏览器中特定的cookie或者header参数等会标识由哪个用户来进行扫码的。

5. 服务器端通知扫码成功

服务器端收到二维码中信息的url请求时,通知端上已经扫码成功,并添加必要的登录cookie等信息。这里的通知方式一般有几种:websocket、轮训hold住请求直到超时、隔几秒轮训。

二维码中url的艺术

如何实现自有客户端和其他客户端扫码(如微信)表现的不同

比如,在业务中,你可能想要这样的操作,如果是你公司的二维码被其他app(如微信)所扫描,想要跳转一个提示页,提示页上可以有一个app的下载链接;而当被你自己的app所扫描时,直接进行对应的请求。

这种情况下,可以这样来做,所有二维码中的链接都进行一层加密,然后统一用另一个链接来处理。

如:www.test.com/qr?p=xxxxxx,p参数中包含服务器与客户端约定的加解密算法(可以是对称的也可以是非对称的),端上扫码到这种特定路径的时候,直接用解密算法解p参数,得到www.testqr.com/qrcode?key=s1arv,这样就可以向服务器发起请求了,而其他客户端因为不知道这个规则,只能直接去请求www.test.com/qr?p=xxxxxx,这个请求返回提示页。

如何让二维码更简单

很多时候,又要马儿跑,又要马儿不吃草。想要二维码中带有很多参数,但是又不想要二维码太复杂,难以被扫码出来。这时候,就需要考虑如何在不影响业务的情况下让二维码变的简单。

  • 与端上约定规则:比如定义编码信息中i参数为1,2,3表示不同的uri,端上来匹配遇到不同的i参数时请求哪个接口
  • 简化一切能简化的地方:简化uri,简化参数中的key、value。如www.a.com/q?k=s1arv就比www.abc.def.adfg.edu.com.cn/qrcode/scan?k=77179574e98a7c860007df62a5dbd98b 要简化很多,生成的二维码要好扫很多。
  • 简化唯一id参数:上一条中前一个请求中参数值只有5位,后一个请求中参数值为生成的32位md5值。生成一个端的key至关重要。

示例代码

生成二维码(去掉白边,增加中间的logo)

需要导入jar包:zxing的 core-2.0.jar

import java.awt.basicstroke;
import java.awt.color;
import java.awt.graphics;
import java.awt.graphics2d;
import java.awt.image;
import java.awt.shape;
import java.awt.geom.roundrectangle2d;
import java.awt.image.bufferedimage;
import java.io.bytearrayoutputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.util.hashmap;
import java.util.map;

import javax.imageio.imageio;
import com.google.zxing.barcodeformat;
import com.google.zxing.encodehinttype;
import com.google.zxing.multiformatwriter;
import com.google.zxing.common.bitmatrix;
import com.google.zxing.qrcode.decoder.errorcorrectionlevel;

public class qrcodeutil {
  private static final int black = color.black.getrgb();
  private static final int white = color.white.getrgb();
  private static final int default_qr_size = 183;
  private static final string default_qr_format = "png";
  private static final byte[] empty_bytes = new byte[0];
  
  public static byte[] createqrcode(string content, int size, string extension) {
    return createqrcode(content, size, extension, null);
  }

  /**
   * 生成带图片的二维码
   * @param content 二维码中要包含的信息
   * @param size 大小
   * @param extension 文件格式扩展
   * @param insertimg 中间的logo图片
   * @return
   */
  public static byte[] createqrcode(string content, int size, string extension, image insertimg) {
    if (size <= 0) {
      throw new illegalargumentexception("size (" + size + ") cannot be <= 0");
    }
    bytearrayoutputstream baos = null;
    try {
      map<encodehinttype, object> hints = new hashmap<encodehinttype, object>();
      hints.put(encodehinttype.character_set, "utf-8");
      hints.put(encodehinttype.error_correction, errorcorrectionlevel.m);

      //使用信息生成指定大小的点阵
      bitmatrix m = new multiformatwriter().encode(content, barcodeformat.qr_code, size, size, hints);
      
      //去掉白边
      m = updatebit(m, 0);
      
      int width = m.getwidth();
      int height = m.getheight();
      
      //将bitmatrix中的信息设置到bufferdimage中,形成黑白图片
      bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);
      for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
          image.setrgb(i, j, m.get(i, j) ? black : white);
        }
      }
      if (insertimg != null) {
        // 插入中间的logo图片
        insertimage(image, insertimg, m.getwidth());
      }
      //将因为去白边而变小的图片再放大
      image = zoominimage(image, size, size);
      baos = new bytearrayoutputstream();
      imageio.write(image, extension, baos);
      
      return baos.tobytearray();
    } catch (exception e) {
    } finally {
      if(baos != null)
        try {
          baos.close();
        } catch (ioexception e) {
          // todo auto-generated catch block
          e.printstacktrace();
        }
    }
    return empty_bytes;
  }
  
  /**
   * 自定义二维码白边宽度
   * @param matrix
   * @param margin
   * @return
   */
  private static bitmatrix updatebit(bitmatrix matrix, int margin) {
    int tempm = margin * 2;
    int[] rec = matrix.getenclosingrectangle(); // 获取二维码图案的属性
    int reswidth = rec[2] + tempm;
    int resheight = rec[3] + tempm;
    bitmatrix resmatrix = new bitmatrix(reswidth, resheight); // 按照自定义边框生成新的bitmatrix
    resmatrix.clear();
    for (int i = margin; i < reswidth - margin; i++) { // 循环,将二维码图案绘制到新的bitmatrix中
      for (int j = margin; j < resheight - margin; j++) {
        if (matrix.get(i - margin + rec[0], j - margin + rec[1])) {
          resmatrix.set(i, j);
        }
      }
    }
    return resmatrix;
  }
  
  // 图片放大缩小
  public static bufferedimage zoominimage(bufferedimage originalimage, int width, int height) {
    bufferedimage newimage = new bufferedimage(width, height, originalimage.gettype());
    graphics g = newimage.getgraphics();
    g.drawimage(originalimage, 0, 0, width, height, null);
    g.dispose();
    return newimage;
  }
  
  private static void insertimage(bufferedimage source, image insertimg, int size) {
    try {
      int width = insertimg.getwidth(null);
      int height = insertimg.getheight(null);
      width = width > size / 6 ? size / 6 : width; // logo设为二维码的六分之一大小
      height = height > size / 6 ? size / 6 : height;
      graphics2d graph = source.creategraphics();
      int x = (size - width) / 2;
      int y = (size - height) / 2;
      graph.drawimage(insertimg, x, y, width, height, null);
      shape shape = new roundrectangle2d.float(x, y, width, width, 6, 6);
      graph.setstroke(new basicstroke(3f));
      graph.draw(shape);
      graph.dispose();
    } catch (exception e) {
      e.printstacktrace();
    }
  }

  public static byte[] createqrcode(string content) {
    return createqrcode(content, default_qr_size, default_qr_format);
  }

  public static void main(string[] args){
    try {
      fileoutputstream fos = new fileoutputstream("ab.png");
      fos.write(createqrcode("test"));
      fos.close();
    } catch (exception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }
    
  }
  
}

生成短链接

基本思路:

短网址映射算法的理论:

1.将长网址加随机数用用md5算法生成32位签名串,分为4段,每段8个字符

2.对这4段循环处理,取每段的8个字符, 将他看成16进制字符串与0x3fffffff(30位1)的位与操作,超过30位的忽略处理

3.将每段得到的这30位又分成6段,每5位的数字作为字母表的索引取得特定字符,依次进行获得6位字符串;

4.这样一个md5字符串可以获得4个6位串,取里面的任意一个就可作为这个长url的短url地址。

5.最好是用一个key-value数据库存储,万一发生碰撞换一个,如果四个都发生碰撞,重新生成md5(因为有随机数,会生成不一样的md5)

public class shorturlutil {

  /**
   * 传入32位md5值
   * @param md5
   * @return
   */
  public static string[] shorturl(string md5) {
    // 要使用生成 url 的字符
    string[] chars = new string[] { "a", "b", "c", "d", "e", "f", "g", "h",
        "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
        "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
        "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h",
        "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
        "u", "v", "w", "x", "y", "z"

    };
    
    string[] resurl = new string[4]; 
    
    for (int i = 0; i < 4; i++) {

      // 把加密字符按照 8 位一组 16 进制与 0x3fffffff 进行位与运算,超过30位的忽略
      string stempsubstring = md5.substring(i * 8, i * 8 + 8);

      // 这里需要使用 long 型来转换,因为 inteper .parseint() 只能处理 31 位 , 首位为符号位 , 如果不用 long ,则会越界
      long lhexlong = 0x3fffffff & long.parselong(stempsubstring, 16);
      string outchars = "";
      for (int j = 0; j < 6; j++) {
        // 把得到的值与 0x0000003d 进行位与运算,取得字符数组 chars 索引
        long index = 0x0000003d & lhexlong;
        // 把取得的字符相加
        outchars += chars[(int) index];
        // 每次循环按位右移 5 位
        lhexlong = lhexlong >> 5;
      }
      // 把字符串存入对应索引的输出数组
      resurl[i] = outchars;
    }
    return resurl;
  }
  
  public static void main(string [] args){
    string[] test = shorturl("fdf8d941f23680be79af83f921b107ac");
    for (string string : test) {
      system.out.println(string);
    }
  }
  
}

说明:核心代码非原创,借鉴了他人的代码,感谢!

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