替换带代码里所有 fastjson 的内容,统一使用 jackson

不考虑使用 gson 的原因,是基本停止了维护
This commit is contained in:
YunaiV 2021-01-24 12:15:59 +08:00
parent 9c1c265993
commit eadc4f749a
17 changed files with 106 additions and 131 deletions

41
pom.xml
View File

@ -31,7 +31,6 @@
<!-- <swagger.version>2.9.2</swagger.version>--> <!-- <swagger.version>2.9.2</swagger.version>-->
<!-- <kaptcha.version>2.3.2</kaptcha.version>--> <!-- <kaptcha.version>2.3.2</kaptcha.version>-->
<!-- <pagehelper.boot.version>1.3.0</pagehelper.boot.version>--> <!-- <pagehelper.boot.version>1.3.0</pagehelper.boot.version>-->
<!-- <fastjson.version>1.2.74</fastjson.version>-->
<!-- <oshi.version>5.3.6</oshi.version>--> <!-- <oshi.version>5.3.6</oshi.version>-->
<!-- <jna.version>5.6.0</jna.version>--> <!-- <jna.version>5.6.0</jna.version>-->
<!-- <commons.io.version>2.5</commons.io.version>--> <!-- <commons.io.version>2.5</commons.io.version>-->
@ -55,7 +54,6 @@
<lombok.version>1.16.14</lombok.version> <lombok.version>1.16.14</lombok.version>
<mapstruct.version>1.4.1.Final</mapstruct.version> <mapstruct.version>1.4.1.Final</mapstruct.version>
<jjwt.version>0.9.1</jjwt.version> <jjwt.version>0.9.1</jjwt.version>
<fastjson.version>1.2.75</fastjson.version>
<hutool.version>5.5.6</hutool.version> <hutool.version>5.5.6</hutool.version>
<easyexcel.verion>2.2.7</easyexcel.verion> <easyexcel.verion>2.2.7</easyexcel.verion>
</properties> </properties>
@ -64,32 +62,6 @@
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<!-- &lt;!&ndash; 解析客户端操作系统、浏览器等 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>eu.bitwalker</groupId>-->
<!-- <artifactId>UserAgentUtils</artifactId>-->
<!-- <version>${bitwalker.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash; 获取系统信息 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>com.github.oshi</groupId>-->
<!-- <artifactId>oshi-core</artifactId>-->
<!-- <version>${oshi.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>net.java.dev.jna</groupId>-->
<!-- <artifactId>jna</artifactId>-->
<!-- <version>${jna.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>net.java.dev.jna</groupId>-->
<!-- <artifactId>jna-platform</artifactId>-->
<!-- <version>${jna.version}</version>-->
<!-- </dependency>-->
<!-- &lt;!&ndash;velocity代码生成使用模板 &ndash;&gt;--> <!-- &lt;!&ndash;velocity代码生成使用模板 &ndash;&gt;-->
<!-- <dependency>--> <!-- <dependency>-->
<!-- <groupId>org.apache.velocity</groupId>--> <!-- <groupId>org.apache.velocity</groupId>-->
@ -211,25 +183,12 @@
<version>${jjwt.version}</version> <version>${jjwt.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>${lombok.version}</version> <version>${lombok.version}</version>
</dependency> </dependency>
<dependency>
<!-- 解析客户端操作系统、浏览器等 TODO 芋艿:后面看看 hutool 有没替代 -->
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.21</version>
</dependency>
<dependency> <dependency>
<groupId>org.mapstruct</groupId> <groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId> <!-- use mapstruct-jdk8 for Java 8 or higher --> <artifactId>mapstruct</artifactId> <!-- use mapstruct-jdk8 for Java 8 or higher -->

View File

@ -61,9 +61,10 @@ service.interceptors.response.use(res => {
} }
).then(() => { ).then(() => {
store.dispatch('LogOut').then(() => { store.dispatch('LogOut').then(() => {
if (location.pathname !== '/login') { // 避免重复跳转 // if (location.pathname !== '/login') { // 避免重复跳转
//
// }
location.href = '/index'; location.href = '/index';
}
}) })
}) })
} else if (code === 500) { } else if (code === 500) {

View File

@ -4,7 +4,6 @@ import cn.iocoder.dashboard.common.exception.ErrorCode;
import cn.iocoder.dashboard.common.exception.GlobalException; import cn.iocoder.dashboard.common.exception.GlobalException;
import cn.iocoder.dashboard.common.exception.ServiceException; import cn.iocoder.dashboard.common.exception.ServiceException;
import cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants; import cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -69,13 +68,11 @@ public final class CommonResult<T> implements Serializable {
return result; return result;
} }
@JSONField(serialize = false) // 避免 fastjson 序列化
@JsonIgnore // 避免 jackson 序列化 @JsonIgnore // 避免 jackson 序列化
public boolean isSuccess() { public boolean isSuccess() {
return GlobalErrorCodeConstants.SUCCESS.getCode().equals(code); return GlobalErrorCodeConstants.SUCCESS.getCode().equals(code);
} }
@JSONField(serialize = false) // 避免 fastjson 序列化
@JsonIgnore // 避免 jackson 序列化 @JsonIgnore // 避免 jackson 序列化
public boolean isError() { public boolean isError() {
return !isSuccess(); return !isSuccess();

View File

@ -11,8 +11,8 @@ import cn.iocoder.dashboard.framework.logger.operatelog.core.service.OperateLogF
import cn.iocoder.dashboard.framework.security.core.util.SecurityUtils; import cn.iocoder.dashboard.framework.security.core.util.SecurityUtils;
import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils;
import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogCreateReqVO; import cn.iocoder.dashboard.modules.system.controller.logger.vo.operatelog.SysOperateLogCreateReqVO;
import cn.iocoder.dashboard.util.json.JSONUtils;
import cn.iocoder.dashboard.util.servlet.ServletUtils; import cn.iocoder.dashboard.util.servlet.ServletUtils;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
@ -319,7 +319,7 @@ public class OperateLogAspect {
// 被忽略时标记为 ignore 字符串避免和 null 混在一起 // 被忽略时标记为 ignore 字符串避免和 null 混在一起
args.put(argName, !isIgnoreArgs(argValue) ? argValue : "[ignore]"); args.put(argName, !isIgnoreArgs(argValue) ? argValue : "[ignore]");
} }
return JSON.toJSONString(args); return JSONUtils.toJSONString(args);
} }
private static String obtainResultData(Object result) { private static String obtainResultData(Object result) {
@ -327,7 +327,7 @@ public class OperateLogAspect {
if (result instanceof CommonResult) { if (result instanceof CommonResult) {
result = ((CommonResult<?>) result).getData(); result = ((CommonResult<?>) result).getData();
} }
return JSON.toJSONString(result); return JSONUtils.toJSONString(result);
} }
private static boolean isIgnoreArgs(Object object) { private static boolean isIgnoreArgs(Object object) {

View File

@ -0,0 +1,45 @@
package cn.iocoder.dashboard.framework.mybatis.core.type;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.Set;
/**
* 参考 {@link com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler} 实现
* 在我们将字符串反序列化为 Set 并且泛型为 Long 如果每个元素的数值太小会被处理成 Integer 类型导致可能存在隐性的 BUG
*
* 例如说哦{@link SysUserDO#getPostIds()} 属性
*
* @author 芋道源码
*/
public class JacksonLongSetTypeHandler extends AbstractJsonTypeHandler<Object> {
// TODO 芋艿需要将 Spring 的设置下进来
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final TypeReference<Set<Long>> typeReference = new TypeReference<Set<Long>>(){};
@Override
protected Object parse(String json) {
try {
return objectMapper.readValue(json, typeReference);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected String toJson(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -1,7 +1,6 @@
package cn.iocoder.dashboard.framework.redis.config; package cn.iocoder.dashboard.framework.redis.config;
import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener; import cn.iocoder.dashboard.framework.redis.core.pubsub.AbstractChannelMessageListener;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -29,7 +28,7 @@ public class RedisConfig {
// 使用 String 序列化方式序列化 KEY // 使用 String 序列化方式序列化 KEY
template.setKeySerializer(RedisSerializer.string()); template.setKeySerializer(RedisSerializer.string());
// 使用 JSON 序列化方式库是 FastJSON 序列化 VALUE // 使用 JSON 序列化方式库是 FastJSON 序列化 VALUE
template.setValueSerializer(new GenericFastJsonRedisSerializer()); template.setValueSerializer(RedisSerializer.json());
return template; return template;
} }

View File

@ -1,6 +1,6 @@
package cn.iocoder.dashboard.framework.redis.core.pubsub; package cn.iocoder.dashboard.framework.redis.core.pubsub;
import com.alibaba.fastjson.annotation.JSONField; import com.fasterxml.jackson.annotation.JsonIgnore;
/** /**
* Redis Channel Message 接口 * Redis Channel Message 接口
@ -12,7 +12,7 @@ public interface ChannelMessage {
* *
* @return Channel * @return Channel
*/ */
@JSONField(serialize = false) // 必须序列化 @JsonIgnore // 必须序列化
String getChannel(); String getChannel();
} }

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.framework.security.core; package cn.iocoder.dashboard.framework.security.core;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import com.alibaba.fastjson.annotation.JSONField; import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data; import lombok.Data;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
@ -49,43 +49,43 @@ public class LoginUser implements UserDetails {
private Integer status; private Integer status;
@Override @Override
@JSONField(serialize = false) // 避免序列化 @JsonIgnore// 避免序列化
public String getPassword() { public String getPassword() {
return password; return password;
} }
@Override @Override
@JSONField(serialize = false) // 避免序列化 @JsonIgnore
public String getUsername() { public String getUsername() {
return username; return username;
} }
@Override @Override
@JSONField(serialize = false) // 避免序列化 @JsonIgnore// 避免序列化
public boolean isEnabled() { public boolean isEnabled() {
return CommonStatusEnum.ENABLE.getStatus().equals(status); return CommonStatusEnum.ENABLE.getStatus().equals(status);
} }
@Override @Override
@JSONField(serialize = false) // 避免序列化 @JsonIgnore// 避免序列化
public Collection<? extends GrantedAuthority> getAuthorities() { public Collection<? extends GrantedAuthority> getAuthorities() {
return null; return null;
} }
@Override @Override
@JSONField(serialize = false) // 避免序列化 @JsonIgnore// 避免序列化
public boolean isAccountNonExpired() { public boolean isAccountNonExpired() {
return true; // 返回 true不依赖 Spring Security 判断 return true; // 返回 true不依赖 Spring Security 判断
} }
@Override @Override
@JSONField(serialize = false) // 避免序列化 @JsonIgnore// 避免序列化
public boolean isAccountNonLocked() { public boolean isAccountNonLocked() {
return true; // 返回 true不依赖 Spring Security 判断 return true; // 返回 true不依赖 Spring Security 判断
} }
@Override @Override
@JSONField(serialize = false) // 避免序列化 @JsonIgnore// 避免序列化
public boolean isCredentialsNonExpired() { public boolean isCredentialsNonExpired() {
return true; // 返回 true不依赖 Spring Security 判断 return true; // 返回 true不依赖 Spring Security 判断
} }

View File

@ -1,15 +1,9 @@
package cn.iocoder.dashboard.framework.web.config; package cn.iocoder.dashboard.framework.web.config;
import cn.iocoder.dashboard.util.servlet.ServletUtils;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
@ -18,10 +12,6 @@ import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
/** /**
* Web 配置类 * Web 配置类
@ -40,46 +30,6 @@ public class WebConfiguration implements WebMvcConfigurer {
&& clazz.getPackage().getName().startsWith(webProperties.getControllerPackage())); && clazz.getPackage().getName().startsWith(webProperties.getControllerPackage()));
} }
// ========== MessageConverter 相关 ==========
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 创建 FastJsonHttpMessageConverter 对象
// 重写 canRead canWrite 方法判断只处理自己写的 API 为前缀的 URL原因是FastJSON 和一些三方框架集成存在问题例如说
// 1. Spring Boot Admin 由于 Registration 基于 Builder 构造对象导致它无法反序列化
// 2. Spring Boot Actuator 貌似也存在问题具体还没去排查
// 但是为什么不替换回 Jackson
// 原因是一些 Number 数值比较小时反序列化回来是 Integer 类型实际是 Long 类型此时在序列化时会报 Integer 无法转换成 Long 的异常
FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter() {
@Override
protected boolean canRead(MediaType mediaType) {
return isApiPrefix() && super.canRead(mediaType);
}
@Override
protected boolean canWrite(MediaType mediaType) {
return isApiPrefix() && super.canWrite(mediaType);
}
private boolean isApiPrefix() {
HttpServletRequest request = ServletUtils.getRequest();
return request != null && request.getRequestURI().startsWith(webProperties.getApiPrefix());
}
};
// 自定义 FastJson 配置
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setCharset(Charset.defaultCharset()); // 设置字符集
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, // 剔除循环引用
SerializerFeature.WriteNonStringKeyAsString); // 解决 Integer 作为 Key 转换为 String 类型避免浏览器报错
fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
// 设置支持的 MediaType
fastJsonHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
// 添加到 converters
converters.add(0, fastJsonHttpMessageConverter); // 注意添加到最开头放在 MappingJackson2XmlHttpMessageConverter 前面
}
// ========== Filter 相关 ========== // ========== Filter 相关 ==========
/** /**

View File

@ -1,13 +1,13 @@
package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.logger; package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.logger;
import cn.iocoder.dashboard.common.pojo.CommonResult; import cn.iocoder.dashboard.common.pojo.CommonResult;
import cn.iocoder.dashboard.framework.logger.operatelog.core.enums.OperateTypeEnum;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO; import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user.SysUserDO;
import cn.iocoder.dashboard.framework.logger.operatelog.core.enums.OperateTypeEnum;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler; import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -75,7 +75,7 @@ public class SysOperateLogDO extends BaseDO {
* 拓展字段有些复杂的业务需要记录一些字段 * 拓展字段有些复杂的业务需要记录一些字段
* 例如说记录订单编号则可以添加 key "orderId"value 为订单编号 * 例如说记录订单编号则可以添加 key "orderId"value 为订单编号
*/ */
@TableField(typeHandler = FastjsonTypeHandler.class) @TableField(typeHandler = JacksonTypeHandler.class)
private Map<String, Object> exts; private Map<String, Object> exts;
/** /**

View File

@ -2,13 +2,13 @@ package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.dashboard.framework.mybatis.core.type.JacksonLongSetTypeHandler;
import cn.iocoder.dashboard.framework.security.core.enums.DataScopeEnum; import cn.iocoder.dashboard.framework.security.core.enums.DataScopeEnum;
import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum; import cn.iocoder.dashboard.modules.system.enums.permission.RoleCodeEnum;
import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum; import cn.iocoder.dashboard.modules.system.enums.permission.SysRoleTypeEnum;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -71,7 +71,7 @@ public class SysRoleDO extends BaseDO {
* *
* 适用于 {@link #dataScope} 的值为 {@link DataScopeEnum#DEPT_CUSTOM} * 适用于 {@link #dataScope} 的值为 {@link DataScopeEnum#DEPT_CUSTOM}
*/ */
@TableField(typeHandler = FastjsonTypeHandler.class) @TableField(typeHandler = JacksonLongSetTypeHandler.class)
private Set<Long> dataScopeDeptIds; private Set<Long> dataScopeDeptIds;
} }

View File

@ -2,11 +2,11 @@ package cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.user;
import cn.iocoder.dashboard.common.enums.CommonStatusEnum; import cn.iocoder.dashboard.common.enums.CommonStatusEnum;
import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.dashboard.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.dashboard.framework.mybatis.core.type.JacksonLongSetTypeHandler;
import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum; import cn.iocoder.dashboard.modules.system.enums.common.SysSexEnum;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@ -54,7 +54,7 @@ public class SysUserDO extends BaseDO {
/** /**
* 岗位编号数组 * 岗位编号数组
*/ */
@TableField(typeHandler = FastjsonTypeHandler.class) @TableField(typeHandler = JacksonLongSetTypeHandler.class)
private Set<Long> postIds; private Set<Long> postIds;
/** /**
* 用户邮箱 * 用户邮箱

View File

@ -1,7 +1,7 @@
package cn.iocoder.dashboard.modules.system.dal.redis.dao.auth; package cn.iocoder.dashboard.modules.system.dal.redis.dao.auth;
import cn.iocoder.dashboard.framework.security.core.LoginUser; import cn.iocoder.dashboard.framework.security.core.LoginUser;
import com.alibaba.fastjson.JSON; import cn.iocoder.dashboard.util.json.JSONUtils;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@ -22,12 +22,12 @@ public class SysLoginUserRedisDAO {
public LoginUser get(String sessionId) { public LoginUser get(String sessionId) {
String redisKey = formatKey(sessionId); String redisKey = formatKey(sessionId);
return JSON.parseObject(stringRedisTemplate.opsForValue().get(redisKey), LoginUser.class); return JSONUtils.parseObject(stringRedisTemplate.opsForValue().get(redisKey), LoginUser.class);
} }
public void set(String sessionId, LoginUser loginUser) { public void set(String sessionId, LoginUser loginUser) {
String redisKey = formatKey(sessionId); String redisKey = formatKey(sessionId);
stringRedisTemplate.opsForValue().set(redisKey, JSON.toJSONString(loginUser), LOGIN_USER.getTimeout()); stringRedisTemplate.opsForValue().set(redisKey, JSONUtils.toJSONString(loginUser), LOGIN_USER.getTimeout());
} }
public void delete(String accessToken) { public void delete(String accessToken) {

View File

@ -1,12 +1,9 @@
package cn.iocoder.dashboard.modules.system.dal.redis.dao.common; package cn.iocoder.dashboard.modules.system.dal.redis.dao.common;
import cn.iocoder.dashboard.framework.security.core.LoginUser;
import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.Duration; import java.time.Duration;
import static cn.iocoder.dashboard.modules.system.dal.redis.RedisKeyConstants.CAPTCHA_CODE; import static cn.iocoder.dashboard.modules.system.dal.redis.RedisKeyConstants.CAPTCHA_CODE;

View File

@ -1,6 +1,9 @@
package cn.iocoder.dashboard.util.json; package cn.iocoder.dashboard.util.json;
import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
/** /**
* JSON 工具类 * JSON 工具类
@ -9,16 +12,35 @@ import com.alibaba.fastjson.JSON;
*/ */
public class JSONUtils { public class JSONUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
// TODO 芋艿需要将 Spring 的设置下进来
public static void setObjectMapper(ObjectMapper objectMapper) {
JSONUtils.objectMapper = objectMapper;
}
public static String toJSONString(Object object) { public static String toJSONString(Object object) {
return JSON.toJSONString(object); try {
return objectMapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
} }
public static <T> T parseObject(String text, Class<T> clazz) { public static <T> T parseObject(String text, Class<T> clazz) {
return JSON.parseObject(text, clazz); try {
return objectMapper.readValue(text, clazz);
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
public static <T> T parseObject(byte[] bytes, Class<T> clazz) { public static <T> T parseObject(byte[] bytes, Class<T> clazz) {
return JSON.parseObject(bytes, clazz); try {
return objectMapper.readValue(bytes, clazz);
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
} }

View File

@ -2,7 +2,7 @@ package cn.iocoder.dashboard.util.servlet;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSON; import cn.iocoder.dashboard.util.json.JSONUtils;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
@ -28,7 +28,7 @@ public class ServletUtils {
*/ */
@SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE否则会乱码 @SuppressWarnings("deprecation") // 必须使用 APPLICATION_JSON_UTF8_VALUE否则会乱码
public static void writeJSON(HttpServletResponse response, Object object) { public static void writeJSON(HttpServletResponse response, Object object) {
String content = JSON.toJSONString(object); String content = JSONUtils.toJSONString(object);
ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE); ServletUtil.write(response, content, MediaType.APPLICATION_JSON_UTF8_VALUE);
} }

View File

@ -31,6 +31,11 @@ spring:
url: http://127.0.0.1:8080/${spring.boot.admin.context-path} # 设置 Spring Boot Admin Server 地址 url: http://127.0.0.1:8080/${spring.boot.admin.context-path} # 设置 Spring Boot Admin Server 地址
# Spring Boot Admin Server 服务端的相关配置 # Spring Boot Admin Server 服务端的相关配置
context-path: /admin # 配置 Spring context-path: /admin # 配置 Spring
# Jackson 配置项
jackson:
serialization:
write-dates-as-timestamps: true # 设置时间的格式,使用时间戳
write-date-timestamps-as-nanoseconds: false # 设置不使用 nanoseconds 的格式。例如说 1611460870.401,而是直接 1611460870401
# 芋道配置项,设置当前项目所有自定义的配置 # 芋道配置项,设置当前项目所有自定义的配置
yudao: yudao: