替换fastjson为jackson
This commit is contained in:
+8
-7
@@ -1,8 +1,9 @@
|
||||
package com.central.oauth2.common.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.utils.JsonUtil;
|
||||
import com.central.common.utils.RsaUtils;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.security.jwt.Jwt;
|
||||
@@ -46,10 +47,10 @@ public class JwtUtils {
|
||||
* @param rsaPublicKey 公钥
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject decodeAndVerify(String jwtToken, RSAPublicKey rsaPublicKey) {
|
||||
public static JsonNode decodeAndVerify(String jwtToken, RSAPublicKey rsaPublicKey) {
|
||||
SignatureVerifier rsaVerifier = new RsaVerifier(rsaPublicKey);
|
||||
Jwt jwt = JwtHelper.decodeAndVerify(jwtToken, rsaVerifier);
|
||||
return JSONObject.parseObject(jwt.getClaims());
|
||||
return JsonUtil.parse(jwt.getClaims());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +58,7 @@ public class JwtUtils {
|
||||
* @param jwtToken token值
|
||||
* @return
|
||||
*/
|
||||
public static JSONObject decodeAndVerify(String jwtToken) {
|
||||
public static JsonNode decodeAndVerify(String jwtToken) {
|
||||
return decodeAndVerify(jwtToken, getPubKeyObj());
|
||||
}
|
||||
|
||||
@@ -67,8 +68,8 @@ public class JwtUtils {
|
||||
* @param currTime 当前时间
|
||||
* @return 未过期:true,已过期:false
|
||||
*/
|
||||
public static boolean checkExp(JSONObject claims, long currTime) {
|
||||
long exp = claims.getLong("exp");
|
||||
public static boolean checkExp(JsonNode claims, long currTime) {
|
||||
long exp = claims.get("exp").asLong();
|
||||
if (exp < currTime) {
|
||||
return false;
|
||||
}
|
||||
@@ -80,7 +81,7 @@ public class JwtUtils {
|
||||
* @param claims jwt内容
|
||||
* @return 未过期:true,已过期:false
|
||||
*/
|
||||
public static boolean checkExp(JSONObject claims) {
|
||||
public static boolean checkExp(JsonNode claims) {
|
||||
return checkExp(claims, System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,5 +86,10 @@
|
||||
<artifactId>reactor-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+1
@@ -98,6 +98,7 @@ public interface CommonConstant {
|
||||
String SIMPLE_MONTH_FORMAT = "yyyyMM";
|
||||
String SIMPLE_DATE_FORMAT = "yyyyMMdd";
|
||||
String SIMPLE_DATETIME_FORMAT = "yyyyMMddHHmmss";
|
||||
String TIME_ZONE_GMT8 = "GMT+8";
|
||||
|
||||
String DEF_USER_PASSWORD = "123456";
|
||||
|
||||
|
||||
@@ -0,0 +1,179 @@
|
||||
package com.central.common.utils;
|
||||
|
||||
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.json.JsonReadFeature;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* 基于 Jackson 的 json 工具类
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2020/10/8
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
public class JsonUtil {
|
||||
private final static ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
static {
|
||||
// 忽略在json字符串中存在,但是在java对象中不存在对应属性的情况
|
||||
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
// 忽略空Bean转json的错误
|
||||
MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
|
||||
// 允许不带引号的字段名称
|
||||
MAPPER.configure(JsonReadFeature.ALLOW_UNQUOTED_FIELD_NAMES.mappedFeature(), true);
|
||||
// 允许单引号
|
||||
MAPPER.configure(JsonReadFeature.ALLOW_SINGLE_QUOTES.mappedFeature(), true);
|
||||
// allow int startWith 0
|
||||
MAPPER.configure(JsonReadFeature.ALLOW_LEADING_ZEROS_FOR_NUMBERS.mappedFeature(), true);
|
||||
// 允许字符串存在转义字符:\r \n \t
|
||||
MAPPER.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
|
||||
// 排除空值字段
|
||||
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
// 使用驼峰式
|
||||
MAPPER.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
|
||||
// 使用bean名称
|
||||
MAPPER.enable(MapperFeature.USE_STD_BEAN_NAMING);
|
||||
// 所有日期格式都统一为固定格式
|
||||
MAPPER.setDateFormat(new SimpleDateFormat(CommonConstant.DATETIME_FORMAT));
|
||||
MAPPER.setTimeZone(TimeZone.getTimeZone(CommonConstant.TIME_ZONE_GMT8));
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象转换为json字符串
|
||||
* @param o 要转换的对象
|
||||
*/
|
||||
public static String toJSONString(Object o) {
|
||||
return toJSONString(o, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象转换为json字符串
|
||||
* @param o 要转换的对象
|
||||
* @param format 是否格式化json
|
||||
*/
|
||||
public static String toJSONString(Object o, boolean format) {
|
||||
try {
|
||||
if (o == null) {
|
||||
return "";
|
||||
}
|
||||
if (o instanceof Number) {
|
||||
return o.toString();
|
||||
}
|
||||
if (o instanceof String) {
|
||||
return (String)o;
|
||||
}
|
||||
if (format) {
|
||||
return MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(o);
|
||||
}
|
||||
return MAPPER.writeValueAsString(o);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转换为自定义对象
|
||||
* @param json json字符串
|
||||
* @param cls 目标对象
|
||||
*/
|
||||
public static <T> T toObject(String json, Class<T> cls) {
|
||||
if(StringUtils.isBlank(json) || cls == null){
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, cls);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转换为自定义对象
|
||||
* @param json json字符串
|
||||
* @param typeReference 目标对象类型
|
||||
*/
|
||||
public static <T> T toObject(String json, TypeReference<T> typeReference) {
|
||||
if(StringUtils.isBlank(json) || typeReference == null){
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, typeReference);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转换为JsonNode对象
|
||||
* @param json json字符串
|
||||
*/
|
||||
public static JsonNode parse(String json) {
|
||||
if (StringUtils.isBlank(json)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.readTree(json);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 对象转换为map对象
|
||||
* @param o 要转换的对象
|
||||
*/
|
||||
public static <K, V> Map<K, V> toMap(Object o) {
|
||||
if (o == null) {
|
||||
return null;
|
||||
}
|
||||
if (o instanceof String) {
|
||||
return toObject((String)o, Map.class);
|
||||
}
|
||||
return MAPPER.convertValue(o, Map.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转换为list对象
|
||||
* @param json json字符串
|
||||
*/
|
||||
public static <T> List<T> toList(String json) {
|
||||
if (StringUtils.isBlank(json)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(json, List.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* json字符串转换为list
|
||||
* @param json json字符串
|
||||
* @param cls list的元素类型
|
||||
*/
|
||||
public static <T> List<T> toList(String json, Class<T> cls) {
|
||||
if (StringUtils.isBlank(json)) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, cls);
|
||||
return MAPPER.readValue(json, javaType);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
package com.central.common.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.central.common.model.Result;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
@@ -49,7 +48,7 @@ public class WebfluxResponseUtil {
|
||||
response.setStatusCode(HttpStatus.valueOf(httpStatus));
|
||||
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
|
||||
DataBufferFactory dataBufferFactory = response.bufferFactory();
|
||||
DataBuffer buffer = dataBufferFactory.wrap(JSONObject.toJSONString(result).getBytes(Charset.defaultCharset()));
|
||||
DataBuffer buffer = dataBufferFactory.wrap(JsonUtil.toJSONString(result).getBytes(Charset.defaultCharset()));
|
||||
return response.writeWith(Mono.just(buffer)).doOnError((error) -> {
|
||||
DataBufferUtils.release(buffer);
|
||||
});
|
||||
|
||||
+21
-15
@@ -3,9 +3,10 @@ package com.central.es.utils;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.BooleanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.central.common.model.PageResult;
|
||||
import com.central.common.utils.JsonUtil;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
@@ -200,16 +201,16 @@ public class SearchBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回列表结果 List<JSONObject>
|
||||
* 返回列表结果 List<JsonNode>
|
||||
*/
|
||||
public List<JSONObject> getList() throws IOException {
|
||||
public List<JsonNode> getList() throws IOException {
|
||||
return getList(this.get().getHits());
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回分页结果 PageResult<JSONObject>
|
||||
*/
|
||||
public PageResult<JSONObject> getPage() throws IOException {
|
||||
public PageResult<JsonNode> getPage() throws IOException {
|
||||
return this.getPage(null, null);
|
||||
}
|
||||
|
||||
@@ -218,30 +219,31 @@ public class SearchBuilder {
|
||||
* @param page 当前页数
|
||||
* @param limit 每页显示
|
||||
*/
|
||||
public PageResult<JSONObject> getPage(Integer page, Integer limit) throws IOException {
|
||||
public PageResult<JsonNode> getPage(Integer page, Integer limit) throws IOException {
|
||||
this.setPage(page, limit);
|
||||
SearchResponse response = this.get();
|
||||
SearchHits searchHits = response.getHits();
|
||||
long totalCnt = searchHits.getTotalHits().value;
|
||||
List<JSONObject> list = getList(searchHits);
|
||||
return PageResult.<JSONObject>builder().data(list).code(0).count(totalCnt).build();
|
||||
List<JsonNode> list = getList(searchHits);
|
||||
return PageResult.<JsonNode>builder().data(list).code(0).count(totalCnt).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回JSON列表数据
|
||||
*/
|
||||
private List<JSONObject> getList(SearchHits searchHits) {
|
||||
List<JSONObject> list = new ArrayList<>();
|
||||
private List<JsonNode> getList(SearchHits searchHits) {
|
||||
List<JsonNode> list = new ArrayList<>();
|
||||
if (searchHits != null) {
|
||||
searchHits.forEach(item -> {
|
||||
JSONObject jsonObject = JSON.parseObject(item.getSourceAsString());
|
||||
jsonObject.put("id", item.getId());
|
||||
JsonNode jsonNode = JsonUtil.parse(item.getSourceAsString());
|
||||
ObjectNode objectNode = (ObjectNode)jsonNode;
|
||||
objectNode.put("id", item.getId());
|
||||
|
||||
Map<String, HighlightField> highlightFields = item.getHighlightFields();
|
||||
if (highlightFields != null) {
|
||||
populateHighLightedFields(jsonObject, highlightFields);
|
||||
populateHighLightedFields(objectNode, highlightFields);
|
||||
}
|
||||
list.add(jsonObject);
|
||||
list.add(objectNode);
|
||||
});
|
||||
}
|
||||
return list;
|
||||
@@ -257,7 +259,11 @@ public class SearchBuilder {
|
||||
try {
|
||||
String name = field.getName();
|
||||
if (!name.endsWith(".keyword")) {
|
||||
PropertyUtils.setProperty(result, field.getName(), concat(field.fragments()));
|
||||
if (result instanceof ObjectNode) {
|
||||
((ObjectNode)result).put(field.getName(), concat(field.fragments()));
|
||||
} else {
|
||||
PropertyUtils.setProperty(result, field.getName(), concat(field.fragments()));
|
||||
}
|
||||
}
|
||||
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
|
||||
throw new ElasticsearchException("failed to set highlighted value for field: " + field.getName()
|
||||
|
||||
Reference in New Issue
Block a user