forked from RyanGoodwill/backend
Merge branch 'main' into personal/heshunme/auth-restart-1
# Conflicts: # src/main/java/org/cmh/backend/authentication/controller/UserController.java # src/main/resources/application.properties
This commit is contained in:
commit
55e40288df
@ -2,9 +2,15 @@ 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 {
|
||||||
|
|
||||||
@ -12,4 +18,17 @@ 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
package org.cmh.backend.Utils;
|
package org.cmh.backend.Utils;
|
||||||
|
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
import org.aspectj.lang.annotation.Before;
|
import org.aspectj.lang.annotation.Before;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@ -7,14 +8,26 @@ import org.springframework.stereotype.Component;
|
|||||||
@Aspect
|
@Aspect
|
||||||
@Component
|
@Component
|
||||||
public class JwtVerifyAspect {
|
public class JwtVerifyAspect {
|
||||||
@Before("@annotation(JwtVerify) && args(request,..)")
|
@Before("@annotation(JwtVerify)&&args(..)")
|
||||||
public void verifyJwtToken(Object request) throws JwtValidationException {
|
public void verifyJwtToken(JoinPoint joinPoint) throws JwtValidationException {
|
||||||
if (request instanceof JwtRequest) {
|
Object[] args = joinPoint.getArgs();
|
||||||
String token = ((JwtRequest) request).getToken();
|
for (Object arg : args) {
|
||||||
if (!JwtUtil.isTokenValid(token)) {
|
if (arg instanceof JwtRequest jwtRequest) {
|
||||||
throw new JwtValidationException("JWT token is invalid");
|
String token = jwtRequest.getToken();
|
||||||
|
if (!JwtUtil.isTokenValid(token)) {
|
||||||
|
throw new JwtValidationException("请求未正确携带身份令牌");
|
||||||
|
}
|
||||||
|
return; // 只接受第一个 JwtRequest 对象,收到后不再校验其他参数
|
||||||
|
}
|
||||||
|
// JWTRequest对象优先,否则再检查其他字符串参数
|
||||||
|
if (arg instanceof String token){
|
||||||
|
if (JwtUtil.isTokenValid(token)){
|
||||||
|
// 验证成功就直接退出。
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
throw new JwtValidationException("请求未正确携带身份令牌");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,23 +0,0 @@
|
|||||||
package org.cmh.backend.authentication.controller;
|
|
||||||
|
|
||||||
import org.cmh.backend.authentication.model.UserHS;
|
|
||||||
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<UserHS> getUser(@PathVariable String username) {
|
|
||||||
UserHS user = userService.getUserByUsername(username);
|
|
||||||
return ResponseEntity.ok(user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -22,5 +22,8 @@ server.servlet.encoding.force=true
|
|||||||
server.servlet.encoding.charset=utf-8
|
server.servlet.encoding.charset=utf-8
|
||||||
# verificationCode
|
# verificationCode
|
||||||
verification.code.images.path=src/main/resources/static/verificationCodeImages
|
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
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user