From a58da98dd1c901d30c60d86f52cbae2270239099 Mon Sep 17 00:00:00 2001 From: heshunme Date: Wed, 3 Jul 2024 14:40:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=BB=99@JwtVerify=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E6=A0=A1=E9=AA=8C=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E7=9A=84=E7=94=A8=E6=B3=95=EF=BC=8C=E7=8E=B0=E5=9C=A8?= =?UTF-8?q?=E5=8F=AA=E8=A6=81=E6=9C=89=E4=BB=BB=E6=84=8F=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E5=8F=82=E6=95=B0=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E6=98=AFJWT=E5=B0=B1=E4=B9=9F=E8=83=BD=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/cmh/backend/Utils/JwtVerifyAspect.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java b/src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java index 5104981..1e341ea 100644 --- a/src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java +++ b/src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java @@ -15,11 +15,19 @@ public class JwtVerifyAspect { if (arg instanceof JwtRequest jwtRequest) { String token = jwtRequest.getToken(); if (!JwtUtil.isTokenValid(token)) { - throw new JwtValidationException("JWT token is invalid"); + throw new JwtValidationException("请求未正确携带身份令牌"); + } + return; // 只接受第一个 JwtRequest 对象,收到后不再校验其他参数 + } + // JWTRequest对象优先,否则再检查其他字符串参数 + if (arg instanceof String token){ + if (JwtUtil.isTokenValid(token)){ + // 验证成功就直接退出。 + return; } - break; } } + throw new JwtValidationException("请求未正确携带身份令牌"); } } From 9d6ab53f5f50e56804d1d8180074a92fea8e1b2b Mon Sep 17 00:00:00 2001 From: heshunme Date: Wed, 3 Jul 2024 14:49:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=BB=99HttpMessageNotReadableException?= =?UTF-8?q?=E5=92=8CMissingServletRequestParameterException=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E4=BA=86=E5=85=A8=E5=B1=80=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=99=A8=EF=BC=8C=E7=8E=B0=E5=9C=A8=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E5=8F=91=E9=80=81=E7=BB=99=E5=90=8E=E7=AB=AF=E7=9A=84?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E4=B8=8D=E5=AF=B9=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E4=B9=9F=E8=83=BD=E6=AD=A3=E7=A1=AE=E8=BF=94=E5=9B=9E401?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/Utils/GlobalExceptionHandler.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/org/cmh/backend/Utils/GlobalExceptionHandler.java b/src/main/java/org/cmh/backend/Utils/GlobalExceptionHandler.java index b513f14..e9d8af5 100644 --- a/src/main/java/org/cmh/backend/Utils/GlobalExceptionHandler.java +++ b/src/main/java/org/cmh/backend/Utils/GlobalExceptionHandler.java @@ -2,9 +2,15 @@ package org.cmh.backend.Utils; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + @ControllerAdvice public class GlobalExceptionHandler { @@ -12,4 +18,17 @@ public class GlobalExceptionHandler { public ResponseEntity handleJwtInvalidException(JwtValidationException ex) { return new ResponseEntity<>(HttpStatus.UNAUTHORIZED); } + + @ExceptionHandler(MissingServletRequestParameterException.class) + public ResponseEntity> handleMissingServletRequestParameterException(MissingServletRequestParameterException ex) { + HashMap response = new HashMap<>(); + response.put("error", ex.getMessage()); + response.put("stackTrace", Arrays.toString(ex.getStackTrace())); + return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + public ResponseEntity handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) { + return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST); + } } \ No newline at end of file