From 42cd3974a63aad3b05d16e2a94d97db35aec031f Mon Sep 17 00:00:00 2001 From: syruan <321359594@qq.com> Date: Wed, 26 Mar 2025 14:13:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Redis=E5=8F=8D=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/SupDispatchCarController.java | 3 +- .../backstage/entity/CarNeedPlanVo.java | 2 +- .../service/CarNeedPlanServiceImpl.java | 8 ++- .../system/service/FileUploadService.java | 15 +++-- .../common/config/CustomRedisSerializer.java | 56 +++++++++++++++++++ .../manager/common/util/RedisService.java | 36 +++++++++++- .../jwt/JwtAuthenticationTokenFilter.java | 11 +++- 7 files changed, 118 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/bonus/gzcar/manager/common/config/CustomRedisSerializer.java diff --git a/src/main/java/com/bonus/gzcar/business/backstage/controller/SupDispatchCarController.java b/src/main/java/com/bonus/gzcar/business/backstage/controller/SupDispatchCarController.java index be7fe03..9068b25 100644 --- a/src/main/java/com/bonus/gzcar/business/backstage/controller/SupDispatchCarController.java +++ b/src/main/java/com/bonus/gzcar/business/backstage/controller/SupDispatchCarController.java @@ -49,7 +49,8 @@ public class SupDispatchCarController { @DecryptAndVerify(decryptedClass = CarNeedPlanVo.class) public PageInfo getPlanListBySup(EncryptedReq 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 list = service.getPlanListBySup(dto.getData());; return new PageInfo<>(list); diff --git a/src/main/java/com/bonus/gzcar/business/backstage/entity/CarNeedPlanVo.java b/src/main/java/com/bonus/gzcar/business/backstage/entity/CarNeedPlanVo.java index ec97d57..b89451a 100644 --- a/src/main/java/com/bonus/gzcar/business/backstage/entity/CarNeedPlanVo.java +++ b/src/main/java/com/bonus/gzcar/business/backstage/entity/CarNeedPlanVo.java @@ -65,7 +65,7 @@ public class CarNeedPlanVo extends ParentVo { /** * 施工地点 */ - @NotBlank(message = "请填施工地点") +// @NotBlank(message = "请填施工地点") private String projectContent; /** * 需用时间 diff --git a/src/main/java/com/bonus/gzcar/business/backstage/service/CarNeedPlanServiceImpl.java b/src/main/java/com/bonus/gzcar/business/backstage/service/CarNeedPlanServiceImpl.java index b7977ab..0ae9f93 100644 --- a/src/main/java/com/bonus/gzcar/business/backstage/service/CarNeedPlanServiceImpl.java +++ b/src/main/java/com/bonus/gzcar/business/backstage/service/CarNeedPlanServiceImpl.java @@ -133,9 +133,11 @@ public class CarNeedPlanServiceImpl implements CarNeedPlanService{ } recordService.addRecord(vo.getId(),"0","1","2","紧急内部用车","0"); }else{ - List fileList=uploadService.uploadImage(files,vo.getId(),"car_plan_apply","运输起始点高德地图截图"); - if(fileList.size()!=files.length){ - return ServerResponse.createErroe("附件上传失败"); + if (files != null && files.length > 0) { + List 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"); } diff --git a/src/main/java/com/bonus/gzcar/business/system/service/FileUploadService.java b/src/main/java/com/bonus/gzcar/business/system/service/FileUploadService.java index ce7a513..dfaa7ad 100644 --- a/src/main/java/com/bonus/gzcar/business/system/service/FileUploadService.java +++ b/src/main/java/com/bonus/gzcar/business/system/service/FileUploadService.java @@ -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 uploadImage(MultipartFile[] files , String outId, String table, String type){ - List list=new ArrayList<>(); + List 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; diff --git a/src/main/java/com/bonus/gzcar/manager/common/config/CustomRedisSerializer.java b/src/main/java/com/bonus/gzcar/manager/common/config/CustomRedisSerializer.java new file mode 100644 index 0000000..25cade8 --- /dev/null +++ b/src/main/java/com/bonus/gzcar/manager/common/config/CustomRedisSerializer.java @@ -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 { + + @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; // 如果不是目标类,抛出异常 + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/bonus/gzcar/manager/common/util/RedisService.java b/src/main/java/com/bonus/gzcar/manager/common/util/RedisService.java index aa70a35..1182f34 100644 --- a/src/main/java/com/bonus/gzcar/manager/common/util/RedisService.java +++ b/src/main/java/com/bonus/gzcar/manager/common/util/RedisService.java @@ -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 getCacheObject(final String key) { + // 设置键的序列化器 + redisTemplate.setKeySerializer(new StringRedisSerializer()); + // 设置值的序列化器为自定义的序列化器 + redisTemplate.setValueSerializer(new CustomRedisSerializer()); + ValueOperations 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) redisTemplate.opsForValue(); + t = operation.get(key); + } catch (Exception ee) { + System.err.println("反序列化失败,尝试修复类路径发生异常:" + ee.getMessage()); + } + } + + if (t == null) { + // 如果反序列化失败,尝试修复类路径 -- 2025/03/19 阮世耀 + try { + operation = (ValueOperations) redisTemplate.opsForValue(); + t= operation.get(key); + } catch (Exception e) { + System.err.println("反序列化失败,尝试修复类路径发生异常:" + e.getMessage()); + } + } + + return t; } /** diff --git a/src/main/java/com/bonus/gzcar/manager/security/jwt/JwtAuthenticationTokenFilter.java b/src/main/java/com/bonus/gzcar/manager/security/jwt/JwtAuthenticationTokenFilter.java index e769fa9..2d8596e 100644 --- a/src/main/java/com/bonus/gzcar/manager/security/jwt/JwtAuthenticationTokenFilter.java +++ b/src/main/java/com/bonus/gzcar/manager/security/jwt/JwtAuthenticationTokenFilter.java @@ -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分钟,自动刷新缓存