扫描二维码入场逻辑
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
package com.sv.netty.config;
|
||||
|
||||
public enum MessageType {
|
||||
LINK("连接"),
|
||||
OPEN_DOOR("开门"),
|
||||
FAILED("开门校验失败");
|
||||
HB("心跳"),
|
||||
SCAN_CODE("扫码"),
|
||||
ENTER_DOOR("进门"),
|
||||
OUT_DOOR("出门");
|
||||
|
||||
private String message;
|
||||
MessageType(String message) {
|
||||
|
||||
@@ -3,14 +3,23 @@ package com.sv.netty.config;
|
||||
import io.netty.util.AttributeKey;
|
||||
|
||||
public interface NettyConstant {
|
||||
String SERVER_IP = "120.27.209.4";
|
||||
Integer SERVER_PORT = 56791;
|
||||
|
||||
String VENUE_SERVER_IP = "VENUE_SERVER_IP";
|
||||
String VENUE_SERVER_PORT = "VENUE_SERVER_PORT";
|
||||
String VENUE_CLIENT_SN = "VENUE_CLIENT_SN";
|
||||
String VENUE_CLIENT_VID = "VENUE_CLIENT_VID";
|
||||
|
||||
/**
|
||||
* session中存储终端发送的额外参数
|
||||
*/
|
||||
AttributeKey<ChannelParam> CHANNEL_PARAM = AttributeKey.newInstance("CHANNEL_PARAM");
|
||||
|
||||
String SPIT_WORD = "#";
|
||||
/**
|
||||
* 条形码
|
||||
*/
|
||||
String SPIT_WORD = "&&&";
|
||||
|
||||
char BARCODE_BEGIN = '@';
|
||||
char BARCODE_END = '#';
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.sv.netty.config;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class VenueBarCode implements Serializable {
|
||||
|
||||
private Integer venueId;
|
||||
|
||||
private Integer memberId;
|
||||
|
||||
private String deviceName;
|
||||
|
||||
private Integer direction;
|
||||
|
||||
public Integer getVenueId() {
|
||||
return venueId;
|
||||
}
|
||||
|
||||
public void setVenueId(Integer venueId) {
|
||||
this.venueId = venueId;
|
||||
}
|
||||
|
||||
public Integer getMemberId() {
|
||||
return memberId;
|
||||
}
|
||||
|
||||
public void setMemberId(Integer memberId) {
|
||||
this.memberId = memberId;
|
||||
}
|
||||
|
||||
public String getDeviceName() {
|
||||
return deviceName;
|
||||
}
|
||||
|
||||
public void setDeviceName(String deviceName) {
|
||||
this.deviceName = deviceName;
|
||||
}
|
||||
|
||||
public Integer getDirection() {
|
||||
return direction;
|
||||
}
|
||||
|
||||
public void setDirection(Integer direction) {
|
||||
this.direction = direction;
|
||||
}
|
||||
}
|
||||
60
netty-model/src/main/java/com/sv/netty/utils/AesUtil.java
Normal file
60
netty-model/src/main/java/com/sv/netty/utils/AesUtil.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package com.sv.netty.utils;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
/**
|
||||
* @author: wuyou
|
||||
* @create: 2019-05-09 14:18
|
||||
**/
|
||||
public class AesUtil {
|
||||
public static final String SECRET_KEY = "a69i3z8b516gd370";
|
||||
|
||||
/**
|
||||
* Encrypt string.
|
||||
*
|
||||
* @param sSrc the s src
|
||||
* @return the string
|
||||
*/
|
||||
public static String encrypt(String sSrc) {
|
||||
try {
|
||||
if (sSrc == null){
|
||||
return null;
|
||||
}
|
||||
byte[] raw = SECRET_KEY.getBytes();
|
||||
SecretKeySpec sKeySpec = new SecretKeySpec(raw, "AES");
|
||||
// "算法/模式/补码方式"
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
|
||||
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
|
||||
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
|
||||
byte[] encrypted = cipher.doFinal(sSrc.getBytes());
|
||||
// 此处使用BASE64做转码功能,同时能起到2次加密的作用。
|
||||
return Base64.encodeS(encrypted);
|
||||
} catch (Throwable e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt string.
|
||||
*
|
||||
* @param sSrc the s src
|
||||
* @return the string
|
||||
*/
|
||||
public static String decrypt(String sSrc) {
|
||||
try {
|
||||
byte[] raw = SECRET_KEY.getBytes("ASCII");
|
||||
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
|
||||
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
|
||||
// 先用base64解密
|
||||
byte[] encrypted1 = Base64.decode(sSrc);
|
||||
byte[] original = cipher.doFinal(encrypted1);
|
||||
return new String(original);
|
||||
} catch (Throwable th) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
285
netty-model/src/main/java/com/sv/netty/utils/Base64.java
Normal file
285
netty-model/src/main/java/com/sv/netty/utils/Base64.java
Normal file
@@ -0,0 +1,285 @@
|
||||
package com.sv.netty.utils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
/**
|
||||
* @author: wuyou
|
||||
* @create: 2019-05-09 14:19
|
||||
**/
|
||||
public class Base64 {
|
||||
|
||||
private static final byte[] ENCODING_TABLE = {(byte) 'A', (byte) 'B',
|
||||
(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G',
|
||||
(byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L',
|
||||
(byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q',
|
||||
(byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V',
|
||||
(byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a',
|
||||
(byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
|
||||
(byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k',
|
||||
(byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p',
|
||||
(byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u',
|
||||
(byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z',
|
||||
(byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4',
|
||||
(byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9',
|
||||
(byte) '+', (byte) '/'};
|
||||
private static final byte[] DECODING_TABLE;
|
||||
|
||||
static {
|
||||
DECODING_TABLE = new byte[128];
|
||||
for (int i = 0; i < 128; i++) {
|
||||
DECODING_TABLE[i] = (byte) -1;
|
||||
}
|
||||
for (int i = 'A'; i <= 'Z'; i++) {
|
||||
DECODING_TABLE[i] = (byte) (i - 'A');
|
||||
}
|
||||
for (int i = 'a'; i <= 'z'; i++) {
|
||||
DECODING_TABLE[i] = (byte) (i - 'a' + 26);
|
||||
}
|
||||
for (int i = '0'; i <= '9'; i++) {
|
||||
DECODING_TABLE[i] = (byte) (i - '0' + 52);
|
||||
}
|
||||
DECODING_TABLE['+'] = 62;
|
||||
DECODING_TABLE['/'] = 63;
|
||||
}
|
||||
|
||||
public static String encodeS(byte[] data) {
|
||||
return new String(encode(data));
|
||||
}
|
||||
|
||||
public static byte[] encode(byte[] data) {
|
||||
byte[] bytes;
|
||||
int modulus = data.length % 3;
|
||||
if (modulus == 0) {
|
||||
bytes = new byte[(4 * data.length) / 3];
|
||||
} else {
|
||||
bytes = new byte[4 * ((data.length / 3) + 1)];
|
||||
}
|
||||
int dataLength = (data.length - modulus);
|
||||
int a1;
|
||||
int a2;
|
||||
int a3;
|
||||
for (int i = 0, j = 0; i < dataLength; i += 3, j += 4) {
|
||||
a1 = data[i] & 0xff;
|
||||
a2 = data[i + 1] & 0xff;
|
||||
a3 = data[i + 2] & 0xff;
|
||||
bytes[j] = ENCODING_TABLE[(a1 >>> 2) & 0x3f];
|
||||
bytes[j + 1] = ENCODING_TABLE[((a1 << 4) | (a2 >>> 4)) & 0x3f];
|
||||
bytes[j + 2] = ENCODING_TABLE[((a2 << 2) | (a3 >>> 6)) & 0x3f];
|
||||
bytes[j + 3] = ENCODING_TABLE[a3 & 0x3f];
|
||||
}
|
||||
int b1;
|
||||
int b2;
|
||||
int b3;
|
||||
int d1;
|
||||
int d2;
|
||||
switch (modulus) {
|
||||
//nothing left to do
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
d1 = data[data.length - 1] & 0xff;
|
||||
b1 = (d1 >>> 2) & 0x3f;
|
||||
b2 = (d1 << 4) & 0x3f;
|
||||
bytes[bytes.length - 4] = ENCODING_TABLE[b1];
|
||||
bytes[bytes.length - 3] = ENCODING_TABLE[b2];
|
||||
bytes[bytes.length - 2] = (byte) '=';
|
||||
bytes[bytes.length - 1] = (byte) '=';
|
||||
break;
|
||||
case 2:
|
||||
d1 = data[data.length - 2] & 0xff;
|
||||
d2 = data[data.length - 1] & 0xff;
|
||||
b1 = (d1 >>> 2) & 0x3f;
|
||||
b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f;
|
||||
b3 = (d2 << 2) & 0x3f;
|
||||
bytes[bytes.length - 4] = ENCODING_TABLE[b1];
|
||||
bytes[bytes.length - 3] = ENCODING_TABLE[b2];
|
||||
bytes[bytes.length - 2] = ENCODING_TABLE[b3];
|
||||
bytes[bytes.length - 1] = (byte) '=';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static byte[] decode(byte[] data) {
|
||||
byte[] bytes;
|
||||
byte b1;
|
||||
byte b2;
|
||||
byte b3;
|
||||
byte b4;
|
||||
data = discardNonBase64Bytes(data);
|
||||
if (data[data.length - 2] == '=') {
|
||||
bytes = new byte[(((data.length / 4) - 1) * 3) + 1];
|
||||
} else if (data[data.length - 1] == '=') {
|
||||
bytes = new byte[(((data.length / 4) - 1) * 3) + 2];
|
||||
} else {
|
||||
bytes = new byte[((data.length / 4) * 3)];
|
||||
}
|
||||
for (int i = 0, j = 0; i < (data.length - 4); i += 4, j += 3) {
|
||||
b1 = DECODING_TABLE[data[i]];
|
||||
b2 = DECODING_TABLE[data[i + 1]];
|
||||
b3 = DECODING_TABLE[data[i + 2]];
|
||||
b4 = DECODING_TABLE[data[i + 3]];
|
||||
bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
|
||||
bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
|
||||
bytes[j + 2] = (byte) ((b3 << 6) | b4);
|
||||
}
|
||||
if (data[data.length - 2] == '=') {
|
||||
b1 = DECODING_TABLE[data[data.length - 4]];
|
||||
b2 = DECODING_TABLE[data[data.length - 3]];
|
||||
bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
|
||||
} else if (data[data.length - 1] == '=') {
|
||||
b1 = DECODING_TABLE[data[data.length - 4]];
|
||||
b2 = DECODING_TABLE[data[data.length - 3]];
|
||||
b3 = DECODING_TABLE[data[data.length - 2]];
|
||||
bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
|
||||
bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
|
||||
} else {
|
||||
b1 = DECODING_TABLE[data[data.length - 4]];
|
||||
b2 = DECODING_TABLE[data[data.length - 3]];
|
||||
b3 = DECODING_TABLE[data[data.length - 2]];
|
||||
b4 = DECODING_TABLE[data[data.length - 1]];
|
||||
bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
|
||||
bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
|
||||
bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static byte[] decode(String data) {
|
||||
byte[] bytes;
|
||||
byte b1;
|
||||
byte b2;
|
||||
byte b3;
|
||||
byte b4;
|
||||
data = discardNonBase64Chars(data);
|
||||
if (data.charAt(data.length() - 2) == '=') {
|
||||
bytes = new byte[(((data.length() / 4) - 1) * 3) + 1];
|
||||
} else if (data.charAt(data.length() - 1) == '=') {
|
||||
bytes = new byte[(((data.length() / 4) - 1) * 3) + 2];
|
||||
} else {
|
||||
bytes = new byte[((data.length() / 4) * 3)];
|
||||
}
|
||||
for (int i = 0, j = 0; i < (data.length() - 4); i += 4, j += 3) {
|
||||
b1 = DECODING_TABLE[data.charAt(i)];
|
||||
b2 = DECODING_TABLE[data.charAt(i + 1)];
|
||||
b3 = DECODING_TABLE[data.charAt(i + 2)];
|
||||
b4 = DECODING_TABLE[data.charAt(i + 3)];
|
||||
bytes[j] = (byte) ((b1 << 2) | (b2 >> 4));
|
||||
bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2));
|
||||
bytes[j + 2] = (byte) ((b3 << 6) | b4);
|
||||
}
|
||||
if (data.charAt(data.length() - 2) == '=') {
|
||||
b1 = DECODING_TABLE[data.charAt(data.length() - 4)];
|
||||
b2 = DECODING_TABLE[data.charAt(data.length() - 3)];
|
||||
bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4));
|
||||
} else if (data.charAt(data.length() - 1) == '=') {
|
||||
b1 = DECODING_TABLE[data.charAt(data.length() - 4)];
|
||||
b2 = DECODING_TABLE[data.charAt(data.length() - 3)];
|
||||
b3 = DECODING_TABLE[data.charAt(data.length() - 2)];
|
||||
bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4));
|
||||
bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2));
|
||||
} else {
|
||||
b1 = DECODING_TABLE[data.charAt(data.length() - 4)];
|
||||
b2 = DECODING_TABLE[data.charAt(data.length() - 3)];
|
||||
b3 = DECODING_TABLE[data.charAt(data.length() - 2)];
|
||||
b4 = DECODING_TABLE[data.charAt(data.length() - 1)];
|
||||
bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4));
|
||||
bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2));
|
||||
bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private static byte[] discardNonBase64Bytes(byte[] data) {
|
||||
byte[] temp = new byte[data.length];
|
||||
int bytesCopied = 0;
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
if (isValidBase64Byte(data[i])) {
|
||||
temp[bytesCopied++] = data[i];
|
||||
}
|
||||
}
|
||||
byte[] newData = new byte[bytesCopied];
|
||||
System.arraycopy(temp, 0, newData, 0, bytesCopied);
|
||||
return newData;
|
||||
}
|
||||
|
||||
private static String discardNonBase64Chars(String data) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
int length = data.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (isValidBase64Byte((byte) (data.charAt(i)))) {
|
||||
sb.append(data.charAt(i));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static boolean isValidBase64Byte(byte b) {
|
||||
if (b == '=') {
|
||||
return true;
|
||||
} else if ((b < 0) || (b >= 128)) {
|
||||
return false;
|
||||
} else if (DECODING_TABLE[b] == -1) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static byte[] compressBytes(byte input[]) {
|
||||
int cachesize = 1024;
|
||||
|
||||
Deflater compresser = new Deflater();
|
||||
|
||||
compresser.reset();
|
||||
compresser.setInput(input);
|
||||
compresser.finish();
|
||||
byte output[] = new byte[0];
|
||||
ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
|
||||
try {
|
||||
byte[] buf = new byte[cachesize];
|
||||
int got;
|
||||
while (!compresser.finished()) {
|
||||
got = compresser.deflate(buf);
|
||||
o.write(buf, 0, got);
|
||||
}
|
||||
output = o.toByteArray();
|
||||
} finally {
|
||||
try {
|
||||
o.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public static byte[] decompressBytes(byte input[]) {
|
||||
int cachesize = 1024;
|
||||
Inflater decompresser = new Inflater();
|
||||
|
||||
byte output[] = new byte[0];
|
||||
decompresser.reset();
|
||||
decompresser.setInput(input);
|
||||
ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
|
||||
try {
|
||||
byte[] buf = new byte[cachesize];
|
||||
int got;
|
||||
while (!decompresser.finished()) {
|
||||
got = decompresser.inflate(buf);
|
||||
o.write(buf, 0, got);
|
||||
}
|
||||
output = o.toByteArray();
|
||||
} catch (Exception e) {
|
||||
} finally {
|
||||
try {
|
||||
o.close();
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
46
netty-model/src/main/java/com/sv/netty/utils/MakeCode.java
Normal file
46
netty-model/src/main/java/com/sv/netty/utils/MakeCode.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.sv.netty.utils;
|
||||
|
||||
import com.sv.netty.config.NettyConstant;
|
||||
import com.sv.netty.config.VenueBarCode;
|
||||
|
||||
public class MakeCode {
|
||||
|
||||
/**
|
||||
* 生成二维码
|
||||
* @return
|
||||
*/
|
||||
public static String makeCode (Integer venueId, Integer memberId, String deviceName, String direction) {
|
||||
String information = memberId + NettyConstant.SPIT_WORD + venueId + NettyConstant.SPIT_WORD + deviceName + NettyConstant.SPIT_WORD + direction;
|
||||
return NettyConstant.BARCODE_BEGIN + AesUtil.encrypt(information) + NettyConstant.BARCODE_END;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析二维码
|
||||
*/
|
||||
public static VenueBarCode decodeCode (String barcode) {
|
||||
VenueBarCode venueBarCode = null;
|
||||
String decrypt = AesUtil.decrypt(barcode);
|
||||
String [] list = new String[0];
|
||||
if (decrypt!=null && decrypt.contains(NettyConstant.SPIT_WORD)){
|
||||
list = decrypt.split(NettyConstant.SPIT_WORD);
|
||||
}
|
||||
if (list != null && list.length == 4) {
|
||||
venueBarCode = new VenueBarCode();
|
||||
venueBarCode.setMemberId(Integer.parseInt(list[0]));
|
||||
venueBarCode.setVenueId(Integer.parseInt(list[1]));
|
||||
venueBarCode.setDeviceName(list[2]);
|
||||
venueBarCode.setDirection(Integer.parseInt(list[3]));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return venueBarCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* 还原二维码
|
||||
*/
|
||||
public static String reWriteBarcode(String barcode) {
|
||||
return NettyConstant.BARCODE_BEGIN + barcode + NettyConstant.BARCODE_END;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user