Compare commits
No commits in common. "73d8b5452fa77e4c88775bcf29999ae4cf4ff56f" and "c0c6fed086f27b958ebfcba0c3ee1861feea2582" have entirely different histories.
73d8b5452f
...
c0c6fed086
@ -2,15 +2,9 @@ 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 {
|
||||
|
||||
@ -18,17 +12,4 @@ public class GlobalExceptionHandler {
|
||||
public ResponseEntity<Object> handleJwtInvalidException(JwtValidationException ex) {
|
||||
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,6 +1,5 @@
|
||||
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;
|
||||
@ -8,26 +7,14 @@ import org.springframework.stereotype.Component;
|
||||
@Aspect
|
||||
@Component
|
||||
public class JwtVerifyAspect {
|
||||
@Before("@annotation(JwtVerify)&&args(..)")
|
||||
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("请求未正确携带身份令牌");
|
||||
}
|
||||
return; // 只接受第一个 JwtRequest 对象,收到后不再校验其他参数
|
||||
}
|
||||
// JWTRequest对象优先,否则再检查其他字符串参数
|
||||
if (arg instanceof String token){
|
||||
if (JwtUtil.isTokenValid(token)){
|
||||
// 验证成功就直接退出。
|
||||
return;
|
||||
}
|
||||
@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");
|
||||
}
|
||||
}
|
||||
throw new JwtValidationException("请求未正确携带身份令牌");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
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.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/users")
|
||||
public class UserController {
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
@GetMapping("/{username}")
|
||||
public ResponseEntity<User> getUser(@PathVariable String username) {
|
||||
User user = userService.getUserByUsername(username);
|
||||
return ResponseEntity.ok(user);
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<String> registerUser(@RequestBody User user) {
|
||||
// 检查用户名是否已经存在
|
||||
if (userService.isUsernameTaken(user.getUsername())) {
|
||||
return ResponseEntity.status(400).body("用户名已存在");
|
||||
}
|
||||
// 保存用户信息
|
||||
userService.saveUser(user);
|
||||
return ResponseEntity.ok("User registered successfully");
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<String> login(@RequestBody User user) {
|
||||
// 处理登录逻辑
|
||||
User existingUser = userService.getUserByUsername(user.getUsername());
|
||||
if (existingUser != null && passwordEncoder.matches(user.getPassword(), existingUser.getPassword())) {
|
||||
return ResponseEntity.ok("登录成功");
|
||||
} else {
|
||||
return ResponseEntity.status(401).body("用户名或密码错误");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -20,10 +20,5 @@ 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
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user