From 6beeb110c2a397bc2d4c5ca4295e11b21db42801 Mon Sep 17 00:00:00 2001 From: heshunme Date: Wed, 3 Jul 2024 01:59:40 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86=E6=97=A0?= =?UTF-8?q?=E7=94=A8=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserController.java | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 src/main/java/org/cmh/backend/authentication/controller/UserController.java diff --git a/src/main/java/org/cmh/backend/authentication/controller/UserController.java b/src/main/java/org/cmh/backend/authentication/controller/UserController.java deleted file mode 100644 index f4e6aae..0000000 --- a/src/main/java/org/cmh/backend/authentication/controller/UserController.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.cmh.backend.authentication.controller; - -import org.cmh.backend.authentication.model.User; -import org.cmh.backend.authentication.service.UserService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/users") -public class UserController { - @Autowired - private UserService userService; - - @GetMapping("/{username}") - public ResponseEntity getUser(@PathVariable String username) { - User user = userService.getUserByUsername(username); - return ResponseEntity.ok(user); - } -} From df6686fc19cf697539110feaa37f8f7e2a1c9c68 Mon Sep 17 00:00:00 2001 From: heshunme Date: Wed, 3 Jul 2024 13:58:09 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E4=BA=86@JwtVerify?= =?UTF-8?q?=E7=9A=84=E8=83=BD=E5=8A=9B=EF=BC=8C=E7=8E=B0=E5=9C=A8=E8=A2=AB?= =?UTF-8?q?=E4=BF=AE=E9=A5=B0=E7=9A=84=E6=96=B9=E6=B3=95=E7=9A=84=E4=BB=BB?= =?UTF-8?q?=E6=84=8F=E4=B8=80=E4=B8=AA=E5=8F=82=E6=95=B0=E6=98=AF=E7=BB=A7?= =?UTF-8?q?=E6=89=BF=E4=BA=8EJwtRequest=E7=9A=84=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E5=8D=B3=E5=8F=AF=EF=BC=8C=E4=B8=8D=E5=86=8D=E5=BC=BA=E5=88=B6?= =?UTF-8?q?=E4=B8=BA=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=8F=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/cmh/backend/Utils/JwtVerifyAspect.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java b/src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java index 42408c9..26cee34 100644 --- a/src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java +++ b/src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java @@ -1,5 +1,6 @@ package org.cmh.backend.Utils; +import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @@ -7,13 +8,17 @@ import org.springframework.stereotype.Component; @Aspect @Component public class JwtVerifyAspect { - @Before("@annotation(JwtVerify) && args(request,..)") - public void verifyJwtToken(Object request) throws JwtValidationException { - if (request instanceof JwtRequest) { - String token = ((JwtRequest) request).getToken(); - if (!JwtUtil.isTokenValid(token)) { - throw new JwtValidationException("JWT token is invalid"); + @Before("@annotation(JwtVerify)") + public void verifyJwtToken(JoinPoint joinPoint) throws JwtValidationException { + Object[] args = joinPoint.getArgs(); + for (Object arg : args) { + if (arg instanceof JwtRequest jwtRequest) { + String token = jwtRequest.getToken(); + if (!JwtUtil.isTokenValid(token)) { + throw new JwtValidationException("JWT token is invalid"); + } } + break; } } } From fc2c97b502cb56315a1485b60c11e1253573f6a2 Mon Sep 17 00:00:00 2001 From: heshunme Date: Wed, 3 Jul 2024 14:12:42 +0800 Subject: [PATCH 3/6] bugfix@JwtVerify --- src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java | 4 ++-- 1 file changed, 2 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 26cee34..5104981 100644 --- a/src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java +++ b/src/main/java/org/cmh/backend/Utils/JwtVerifyAspect.java @@ -8,7 +8,7 @@ import org.springframework.stereotype.Component; @Aspect @Component public class JwtVerifyAspect { - @Before("@annotation(JwtVerify)") + @Before("@annotation(JwtVerify)&&args(..)") public void verifyJwtToken(JoinPoint joinPoint) throws JwtValidationException { Object[] args = joinPoint.getArgs(); for (Object arg : args) { @@ -17,8 +17,8 @@ public class JwtVerifyAspect { if (!JwtUtil.isTokenValid(token)) { throw new JwtValidationException("JWT token is invalid"); } + break; } - break; } } } From a58da98dd1c901d30c60d86f52cbae2270239099 Mon Sep 17 00:00:00 2001 From: heshunme Date: Wed, 3 Jul 2024 14:40:25 +0800 Subject: [PATCH 4/6] =?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 5/6] =?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 From 441c9841082fbe10cf851d4592e5db1f560f3b6d Mon Sep 17 00:00:00 2001 From: heshunme Date: Thu, 4 Jul 2024 00:31:58 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=85=A8=E9=9B=86=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E5=A4=A7=E5=B0=8F=E4=B8=8A?= =?UTF-8?q?=E9=99=90=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application.properties | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index bceeb5c..2a38a12 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -20,5 +20,10 @@ spring.datasource.hikari.connection-timeout=30000 server.servlet.encoding.enabled=true server.servlet.encoding.force=true server.servlet.encoding.charset=utf-8 - +# verificationCode +verification.code.images.path=src/main/resources/static/verificationCodeImages +# set the max size of a single file +spring.servlet.multipart.max-file-size=50MB +# set the max size of the total request +spring.servlet.multipart.max-request-size=50MB