feat:jwt中的hutool-json改为fastjson
This commit is contained in:
@@ -1,50 +0,0 @@
|
||||
package com.tiesheng.util;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.alibaba.fastjson.support.config.FastJsonConfig;
|
||||
import com.tiesheng.util.config.DesensitizeValueFilter;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CommonUtil {
|
||||
|
||||
|
||||
/**
|
||||
* FastJson配置
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static FastJsonConfig fastJsonConfig() {
|
||||
FastJsonConfig config = new FastJsonConfig();
|
||||
config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,
|
||||
SerializerFeature.WriteNullStringAsEmpty,
|
||||
SerializerFeature.WriteEnumUsingName,
|
||||
SerializerFeature.DisableCircularReferenceDetect
|
||||
);
|
||||
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
config.setSerializeFilters(new DesensitizeValueFilter());
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 格式化数据
|
||||
*
|
||||
* @param value
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String writeJsonString(Object value) throws IOException {
|
||||
FastJsonConfig fastJsonConfig = fastJsonConfig();
|
||||
ByteArrayOutputStream outnew = new ByteArrayOutputStream();
|
||||
JSON.writeJSONStringWithFastJsonConfig(outnew, fastJsonConfig.getCharset(),
|
||||
value, fastJsonConfig.getSerializeConfig(),
|
||||
fastJsonConfig.getSerializeFilters(),
|
||||
fastJsonConfig.getDateFormat(), JSON.DEFAULT_GENERATE_FEATURE,
|
||||
fastJsonConfig.getSerializerFeatures());
|
||||
return outnew.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.tiesheng.util.config;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.serializer.ValueFilter;
|
||||
import com.tiesheng.annotation.desensitize.Desensitize;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
/**
|
||||
* 脱敏过滤类
|
||||
*
|
||||
* @author hao
|
||||
*/
|
||||
public class DesensitizeValueFilter implements ValueFilter {
|
||||
|
||||
@Override
|
||||
public Object process(Object object, String name, Object value) {
|
||||
if (ObjectUtil.isEmpty(value) || !(value instanceof String)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
Field field = ReflectUtil.getField(object.getClass(), name);
|
||||
if (ObjectUtil.isEmpty(field)) {
|
||||
return value;
|
||||
}
|
||||
Desensitize desensitize = field.getAnnotation(Desensitize.class);
|
||||
if (String.class != field.getType() || ObjectUtil.isEmpty(desensitize)) {
|
||||
return value;
|
||||
}
|
||||
String originVal = String.valueOf(value);
|
||||
return StrUtil.hide(originVal, desensitize.prefix(),
|
||||
StrUtil.length(originVal) - desensitize.suffix());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -26,6 +28,18 @@ import java.util.List;
|
||||
@ConfigurationProperties(prefix = "tiesheng.global")
|
||||
public class GlobalConfig {
|
||||
|
||||
static {
|
||||
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
// FASTJSON 设置全局序列化配置
|
||||
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.WriteMapNullValue.getMask();
|
||||
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.WriteNullStringAsEmpty.getMask();
|
||||
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.WriteEnumUsingName.getMask();
|
||||
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.WriteDateUseDateFormat.getMask();
|
||||
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask();
|
||||
}
|
||||
|
||||
|
||||
private String host;
|
||||
private String service;
|
||||
private String version;
|
||||
|
||||
@@ -4,7 +4,7 @@ package com.tiesheng.util.config;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import com.tiesheng.util.jwt.JWT;
|
||||
import com.tiesheng.util.jwt.JWTValidator;
|
||||
@@ -81,7 +81,7 @@ public class TsTokenConfig {
|
||||
try {
|
||||
JWT decode = JWT.of(token);
|
||||
JWTValidator.of(decode).validateDate();
|
||||
tokenBean = JSONUtil.toBean(decode.getPayloads(), TokenBean.class);
|
||||
tokenBean = JSON.toJavaObject(decode.getPayloads(), TokenBean.class);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.tiesheng.util.jwt;
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.core.date.format.GlobalCustomFormat;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.json.JSONConfig;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.Charset;
|
||||
@@ -21,9 +19,6 @@ import java.util.Map;
|
||||
public class Claims implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// 时间使用秒级时间戳表示
|
||||
private final JSONConfig CONFIG = JSONConfig.create().setDateFormat(GlobalCustomFormat.FORMAT_SECONDS);
|
||||
|
||||
private JSONObject claimJSON;
|
||||
|
||||
/**
|
||||
@@ -39,7 +34,7 @@ public class Claims implements Serializable {
|
||||
claimJSON.remove(name);
|
||||
return;
|
||||
}
|
||||
claimJSON.set(name, value);
|
||||
claimJSON.put(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +58,7 @@ public class Claims implements Serializable {
|
||||
*/
|
||||
public Object getClaim(String name) {
|
||||
init();
|
||||
return this.claimJSON.getObj(name);
|
||||
return this.claimJSON.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +78,7 @@ public class Claims implements Serializable {
|
||||
* @param charset 编码
|
||||
*/
|
||||
public void parse(String tokenPart, Charset charset) {
|
||||
this.claimJSON = JSONUtil.parseObj(Base64.decodeStr(tokenPart, charset), CONFIG);
|
||||
this.claimJSON = JSON.parseObject(Base64.decodeStr(tokenPart, charset));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,7 +89,7 @@ public class Claims implements Serializable {
|
||||
|
||||
private void init() {
|
||||
if (null == this.claimJSON) {
|
||||
this.claimJSON = new JSONObject(CONFIG);
|
||||
this.claimJSON = new JSONObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.tiesheng.util.jwt.signers.AlgorithmUtil;
|
||||
import com.tiesheng.util.jwt.signers.JWTSigner;
|
||||
import com.tiesheng.util.jwt.signers.JWTSignerUtil;
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
package com.tiesheng.util.jwt;
|
||||
|
||||
import com.tiesheng.util.jwt.signers.JWTSigner;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* JSON Web Token (JWT)工具类
|
||||
*/
|
||||
public class JWTUtil {
|
||||
|
||||
/**
|
||||
* 创建HS256(HmacSHA256) JWT Token
|
||||
*
|
||||
* @param payload 荷载信息
|
||||
* @param key HS256(HmacSHA256)密钥
|
||||
* @return JWT Token
|
||||
*/
|
||||
public static String createToken(Map<String, Object> payload, byte[] key) {
|
||||
return createToken(null, payload, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建HS256(HmacSHA256) JWT Token
|
||||
*
|
||||
* @param headers 头信息
|
||||
* @param payload 荷载信息
|
||||
* @param key HS256(HmacSHA256)密钥
|
||||
* @return JWT Token
|
||||
*/
|
||||
public static String createToken(Map<String, Object> headers, Map<String, Object> payload, byte[] key) {
|
||||
return JWT.create()
|
||||
.addHeaders(headers)
|
||||
.addPayloads(payload)
|
||||
.setKey(key)
|
||||
.sign();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建JWT Token
|
||||
*
|
||||
* @param payload 荷载信息
|
||||
* @param signer 签名算法
|
||||
* @return JWT Token
|
||||
*/
|
||||
public static String createToken(Map<String, Object> payload, JWTSigner signer) {
|
||||
return createToken(null, payload, signer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建JWT Token
|
||||
*
|
||||
* @param headers 头信息
|
||||
* @param payload 荷载信息
|
||||
* @param signer 签名算法
|
||||
* @return JWT Token
|
||||
*/
|
||||
public static String createToken(Map<String, Object> headers, Map<String, Object> payload, JWTSigner signer) {
|
||||
return JWT.create()
|
||||
.addHeaders(headers)
|
||||
.addPayloads(payload)
|
||||
.setSigner(signer)
|
||||
.sign();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析JWT Token
|
||||
*
|
||||
* @param token token
|
||||
* @return {@link JWT}
|
||||
*/
|
||||
public static JWT parseToken(String token) {
|
||||
return JWT.of(token);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证JWT Token有效性
|
||||
*
|
||||
* @param token JWT Token
|
||||
* @param key HS256(HmacSHA256)密钥
|
||||
* @return 是否有效
|
||||
*/
|
||||
public static boolean verify(String token, byte[] key) {
|
||||
return JWT.of(token).setKey(key).verify();
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证JWT Token有效性
|
||||
*
|
||||
* @param token JWT Token
|
||||
* @param signer 签名器
|
||||
* @return 是否有效
|
||||
*/
|
||||
public static boolean verify(String token, JWTSigner signer) {
|
||||
return JWT.of(token).verify(signer);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user