Compare commits
No commits in common. "9d6ab53f5f50e56804d1d8180074a92fea8e1b2b" and "fc2c97b502cb56315a1485b60c11e1253573f6a2" have entirely different histories.
9d6ab53f5f
...
fc2c97b502
@ -2,15 +2,9 @@ package org.cmh.backend.Utils;
|
|||||||
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
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.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
@ -18,17 +12,4 @@ public class GlobalExceptionHandler {
|
|||||||
public ResponseEntity<Object> handleJwtInvalidException(JwtValidationException ex) {
|
public ResponseEntity<Object> handleJwtInvalidException(JwtValidationException ex) {
|
||||||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(MissingServletRequestParameterException.class)
|
|
||||||
public ResponseEntity<Map<String, String>> handleMissingServletRequestParameterException(MissingServletRequestParameterException ex) {
|
|
||||||
HashMap<String, String> 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<String> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
|
|
||||||
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -15,19 +15,11 @@ public class JwtVerifyAspect {
|
|||||||
if (arg instanceof JwtRequest jwtRequest) {
|
if (arg instanceof JwtRequest jwtRequest) {
|
||||||
String token = jwtRequest.getToken();
|
String token = jwtRequest.getToken();
|
||||||
if (!JwtUtil.isTokenValid(token)) {
|
if (!JwtUtil.isTokenValid(token)) {
|
||||||
throw new JwtValidationException("请求未正确携带身份令牌");
|
throw new JwtValidationException("JWT token is invalid");
|
||||||
}
|
|
||||||
return; // 只接受第一个 JwtRequest 对象,收到后不再校验其他参数
|
|
||||||
}
|
|
||||||
// JWTRequest对象优先,否则再检查其他字符串参数
|
|
||||||
if (arg instanceof String token){
|
|
||||||
if (JwtUtil.isTokenValid(token)){
|
|
||||||
// 验证成功就直接退出。
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new JwtValidationException("请求未正确携带身份令牌");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user