Unverified Commit 12e52f86 authored by Yang Libin's avatar Yang Libin Committed by GitHub

feat: update util packages (#39)

* feat: update util packages * feat: remove json and common-lang3 packages
parent 0853d8ee
......@@ -76,21 +76,11 @@
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
......
......@@ -10,6 +10,9 @@ import java.util.Base64;
*/
public class Base64Util {
private Base64Util() {
}
public static byte[] encodeUrl(byte[] input) {
byte[] base64 = Base64.getEncoder().encode(input);
for (int i = 0; i < base64.length; ++i) {
......
......@@ -16,6 +16,9 @@ import java.util.Map;
* @since 2021/7/28 14:55
*/
public class HttpUtil {
private HttpUtil() {
}
/**
* Send a get request
*
......@@ -106,7 +109,7 @@ public class HttpUtil {
Map<String, String> headers) throws IOException {
// set content type
if (headers == null) {
headers = new HashMap<String, String>();
headers = new HashMap<>(2);
}
headers.put("Content-Type", "application/x-www-form-urlencoded");
......
package io.github.doocs.im.util;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.commons.lang3.SerializationException;
import java.io.IOException;
import com.fasterxml.jackson.databind.json.JsonMapper;
/**
* JSON tool
......@@ -18,48 +15,22 @@ import java.io.IOException;
*/
public class JsonUtil {
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final ObjectMapper INDENT_OUTPUT_OBJECT_MAPPER = new ObjectMapper();
static {
OBJECT_MAPPER.enable(MapperFeature.USE_GETTERS_AS_SETTERS);
OBJECT_MAPPER.enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS);
JsonMapper.builder().enable(MapperFeature.USE_GETTERS_AS_SETTERS);
JsonMapper.builder().enable(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS);
OBJECT_MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
INDENT_OUTPUT_OBJECT_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
INDENT_OUTPUT_OBJECT_MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
INDENT_OUTPUT_OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
private JsonUtil() {
}
public static String obj2Str(Object object) {
String str = "";
try {
str = OBJECT_MAPPER.writeValueAsString(object);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return str;
public static String obj2Str(Object object) throws JsonProcessingException {
return OBJECT_MAPPER.writeValueAsString(object);
}
public static <T> T str2Obj(String str, Class<T> cls) {
T object = null;
try {
object = OBJECT_MAPPER.readValue(str, cls);
} catch (IOException e) {
e.printStackTrace();
}
return object;
}
public static void writeValueWithoutNullValue(Object object) {
try {
if (null != object) {
INDENT_OUTPUT_OBJECT_MAPPER.writeValueAsString(object);
}
} catch (IOException e) {
throw new SerializationException(e);
}
public static <T> T str2Obj(String str, Class<T> cls) throws JsonProcessingException {
return OBJECT_MAPPER.readValue(str, cls);
}
}
package io.github.doocs.im.util;
import org.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
......@@ -9,6 +9,8 @@ import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.Deflater;
/**
......@@ -19,6 +21,10 @@ import java.util.zip.Deflater;
*/
public class SigUtil {
private SigUtil() {
}
/**
* 【功能说明】用于签发 TRTC 和 IM 服务中必须要使用的 UserSig 鉴权票据
* <p>
......@@ -116,13 +122,12 @@ public class SigUtil {
private static String genUserSig(long sdkAppId, String key, String userid, long expire, byte[] userbuf) {
long currTime = System.currentTimeMillis() / 1000;
JSONObject sigDoc = new JSONObject();
Map<String, Object> sigDoc = new HashMap<>(8);
sigDoc.put("TLS.ver", "2.0");
sigDoc.put("TLS.identifier", userid);
sigDoc.put("TLS.sdkappid", sdkAppId);
sigDoc.put("TLS.expire", expire);
sigDoc.put("TLS.time", currTime);
String base64UserBuf = null;
if (null != userbuf) {
base64UserBuf = Base64.getEncoder().encodeToString(userbuf).replaceAll("\\s*", "");
......@@ -134,7 +139,11 @@ public class SigUtil {
}
sigDoc.put("TLS.sig", sig);
Deflater compressor = new Deflater();
compressor.setInput(sigDoc.toString().getBytes(StandardCharsets.UTF_8));
try {
compressor.setInput(JsonUtil.obj2Str(sigDoc).getBytes(StandardCharsets.UTF_8));
} catch (JsonProcessingException e) {
compressor.setInput(new byte[]{});
}
compressor.finish();
byte[] compressedBytes = new byte[2048];
int compressedBytesLength = compressor.deflate(compressedBytes);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment