增加Redis反序列化处理
This commit is contained in:
parent
48a8b40094
commit
42cd3974a6
|
|
@ -49,7 +49,8 @@ public class SupDispatchCarController {
|
|||
@DecryptAndVerify(decryptedClass = CarNeedPlanVo.class)
|
||||
public PageInfo<CarNeedPlanVo> getPlanListBySup(EncryptedReq<CarNeedPlanVo> dto) {
|
||||
String userId= Objects.requireNonNull(UserUtil.getLoginUser()).getUserId().toString();
|
||||
dto.getData().setCreator(userId);dto.getData().setUserId(userId);
|
||||
dto.getData().setCreator(userId);
|
||||
dto.getData().setUserId(userId);
|
||||
PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
|
||||
List<CarNeedPlanVo> list = service.getPlanListBySup(dto.getData());;
|
||||
return new PageInfo<>(list);
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ public class CarNeedPlanVo extends ParentVo {
|
|||
/**
|
||||
* 施工地点
|
||||
*/
|
||||
@NotBlank(message = "请填施工地点")
|
||||
// @NotBlank(message = "请填施工地点")
|
||||
private String projectContent;
|
||||
/**
|
||||
* 需用时间
|
||||
|
|
|
|||
|
|
@ -133,9 +133,11 @@ public class CarNeedPlanServiceImpl implements CarNeedPlanService{
|
|||
}
|
||||
recordService.addRecord(vo.getId(),"0","1","2","紧急内部用车","0");
|
||||
}else{
|
||||
List<FileUploadVo> fileList=uploadService.uploadImage(files,vo.getId(),"car_plan_apply","运输起始点高德地图截图");
|
||||
if(fileList.size()!=files.length){
|
||||
return ServerResponse.createErroe("附件上传失败");
|
||||
if (files != null && files.length > 0) {
|
||||
List<FileUploadVo> fileList=uploadService.uploadImage(files,vo.getId(),"car_plan_apply","运输起始点高德地图截图");
|
||||
if(fileList.size()!=files.length){
|
||||
return ServerResponse.createErroe("附件上传失败");
|
||||
}
|
||||
}
|
||||
recordService.addRecord(vo.getId(),"0","1","2","","0");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,10 +16,9 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.bonus.gzcar.business.utils.IDUtils.isImageFileExtension;
|
||||
|
||||
/**
|
||||
|
|
@ -84,9 +83,15 @@ public class FileUploadService {
|
|||
* 上传文件
|
||||
*/
|
||||
public List<FileUploadVo> uploadImage(MultipartFile[] files , String outId, String table, String type){
|
||||
List<FileUploadVo> list=new ArrayList<>();
|
||||
List<FileUploadVo> list = new ArrayList<>();
|
||||
try {
|
||||
if (files == null || files.length < 1) {
|
||||
return list;
|
||||
}
|
||||
for (MultipartFile file : files) {
|
||||
if (file == null) {
|
||||
continue;
|
||||
}
|
||||
String fileName = file.getOriginalFilename();
|
||||
String suffix=IDUtils.getSuffix(fileName);
|
||||
String path="/"+DateTimeHelper.getNowYMD()+"/"+ IDUtils.createID()+suffix;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
package com.bonus.gzcar.manager.common.config;
|
||||
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.SerializationException;
|
||||
import org.springframework.util.SerializationUtils;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* 自定义 Redis 序列化器
|
||||
* @author 阮世耀
|
||||
*/
|
||||
public class CustomRedisSerializer implements RedisSerializer<Object> {
|
||||
|
||||
@Override
|
||||
public byte[] serialize(Object object) throws SerializationException {
|
||||
// 使用默认的序列化逻辑
|
||||
return SerializationUtils.serialize(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object deserialize(byte[] bytes) throws SerializationException {
|
||||
if (bytes == null) {
|
||||
return null;
|
||||
}
|
||||
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream objectInputStream = new CustomObjectInputStream(byteArrayInputStream)) {
|
||||
return objectInputStream.readObject();
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
throw new SerializationException("Failed to deserialize object", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义 ObjectInputStream,用于修复类路径
|
||||
*/
|
||||
private static class CustomObjectInputStream extends ObjectInputStream {
|
||||
public CustomObjectInputStream(InputStream in) throws IOException {
|
||||
super(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
|
||||
try {
|
||||
// 默认尝试加载类
|
||||
return super.resolveClass(desc);
|
||||
} catch (ClassNotFoundException e) {
|
||||
// 如果类路径不对,手动指定正确的类路径
|
||||
if (desc.getName().equals("com.bonus.aqgqj.manager.security.entity.SelfUserEntity")) {
|
||||
return Class.forName("com.bonus.gzcar.manager.security.entity.SelfUserEntity");
|
||||
}
|
||||
throw e; // 如果不是目标类,抛出异常
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,13 @@
|
|||
package com.bonus.gzcar.manager.common.util;
|
||||
|
||||
import com.bonus.gzcar.manager.common.config.CustomRedisSerializer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.BoundSetOperations;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
|
|
@ -104,8 +106,40 @@ public class RedisService
|
|||
*/
|
||||
public <T> T getCacheObject(final String key)
|
||||
{
|
||||
// 设置键的序列化器
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
// 设置值的序列化器为自定义的序列化器
|
||||
redisTemplate.setValueSerializer(new CustomRedisSerializer());
|
||||
|
||||
ValueOperations<String, T> operation = redisTemplate.opsForValue();
|
||||
return operation.get(key);
|
||||
T t = null;
|
||||
try {
|
||||
t = operation.get(key);
|
||||
} catch (Exception e) {
|
||||
// 如果反序列化失败,尝试修复类路径 -- 2025/03/19 阮世耀
|
||||
try {
|
||||
// 设置键的序列化器
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
// 设置值的序列化器为自定义的序列化器
|
||||
redisTemplate.setValueSerializer(new CustomRedisSerializer());
|
||||
operation = (ValueOperations<String, T>) redisTemplate.opsForValue();
|
||||
t = operation.get(key);
|
||||
} catch (Exception ee) {
|
||||
System.err.println("反序列化失败,尝试修复类路径发生异常:" + ee.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (t == null) {
|
||||
// 如果反序列化失败,尝试修复类路径 -- 2025/03/19 阮世耀
|
||||
try {
|
||||
operation = (ValueOperations<String, T>) redisTemplate.opsForValue();
|
||||
t= operation.get(key);
|
||||
} catch (Exception e) {
|
||||
System.err.println("反序列化失败,尝试修复类路径发生异常:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -46,6 +46,14 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
|
|||
filterChain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
if (uri.contains(".jpg") || uri.contains(".png") || uri.contains(".jpeg") || uri.contains("onlinePreview")) {
|
||||
filterChain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
if (uri.contains(".xlsx") || uri.contains(".pdf") || uri.contains(".xls") || uri.contains(".docx") || uri.contains(".doc")) {
|
||||
filterChain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
// if(uri.contains("/app/")){
|
||||
// filterChain.doFilter(request, response);
|
||||
// return;
|
||||
|
|
@ -59,8 +67,6 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
|
|||
Claims claims = JwtUtils.parseToken(jwtToken);
|
||||
Integer userId = (Integer) claims.get(SecurityConstants.DETAILS_USER_ID);
|
||||
String userName = (String) claims.get(SecurityConstants.DETAILS_USERNAME);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ResultUtil.responseJson(response,ResultUtil.resultCode(401,"请先登录"));
|
||||
|
|
@ -69,6 +75,7 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
|
|||
SelfUserEntity loginUser = tokenService.getLoginUser(request);
|
||||
if(Objects.isNull(loginUser)){
|
||||
ResultUtil.responseJson(response,ResultUtil.resultCode(401,"登录过期,请重新登录"));
|
||||
System.err.println("异常401,token信息:" + jwtToken + ",地址" + uri);
|
||||
return;
|
||||
}else{
|
||||
// 验证令牌有效期,相差不足10分钟,自动刷新缓存
|
||||
|
|
|
|||
Loading…
Reference in New Issue