图形验证码

This commit is contained in:
none
2023-02-15 16:54:03 +08:00
parent a9296cdfe2
commit 2813e98d9e
35 changed files with 2769 additions and 112 deletions

View File

@@ -2,6 +2,7 @@ package xyz.playedu.api.controller;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -19,17 +20,17 @@ public class ExceptionController {
// }
@ExceptionHandler(ServiceException.class)
public JsonResponse<String> serviceExceptionHandler(ServiceException e) {
public JsonResponse serviceExceptionHandler(ServiceException e) {
return JsonResponse.error(e.getMessage(), 1);
}
@ExceptionHandler(HttpMessageNotReadableException.class)
public JsonResponse<String> serviceExceptionHandler(HttpMessageNotReadableException e) {
public JsonResponse serviceExceptionHandler(HttpMessageNotReadableException e) {
return JsonResponse.error("参数为空", 406);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public JsonResponse<String> serviceExceptionHandler(MethodArgumentNotValidException e) {
public JsonResponse serviceExceptionHandler(MethodArgumentNotValidException e) {
StringBuffer errorMsg = new StringBuffer();
List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
for (ObjectError tmpError : allErrors) {
@@ -39,4 +40,9 @@ public class ExceptionController {
return JsonResponse.error(msg, 406);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public JsonResponse serviceExceptionHandler(HttpRequestMethodNotSupportedException e) {
return JsonResponse.error("请求method错误", 400);
}
}

View File

@@ -5,8 +5,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.playedu.api.domain.AdminUser;
import xyz.playedu.api.exception.ServiceException;
import xyz.playedu.api.service.impl.AdminUserServiceImpl;
import xyz.playedu.api.service.AdminUserService;
import xyz.playedu.api.types.PaginationResult;
import xyz.playedu.api.types.JsonResponse;
@@ -14,10 +13,10 @@ import xyz.playedu.api.types.JsonResponse;
public class AdminUserController {
@Autowired
private AdminUserServiceImpl adminUserService;
private AdminUserService adminUserService;
@GetMapping("/admin/user/index")
public JsonResponse<Object> List(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "10") Integer size) {
public JsonResponse List(@RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "size", defaultValue = "10") Integer size) {
PaginationResult<AdminUser> result = adminUserService.paginate(page, size, null);
return JsonResponse.data(result);
}

View File

@@ -1,29 +1,44 @@
package xyz.playedu.api.controller.admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.playedu.api.domain.AdminUser;
import xyz.playedu.api.middleware.ImageCaptchaCheckMiddleware;
import xyz.playedu.api.request.LoginRequest;
import xyz.playedu.api.service.AdminUserService;
import xyz.playedu.api.types.JsonResponse;
import java.util.HashMap;
import xyz.playedu.api.util.MD5Util;
@RestController
@RequestMapping("/admin/v1/auth")
public class LoginController {
@Autowired
private AdminUserService adminUserService;
@PostMapping("/login")
public JsonResponse<Object> login(@RequestBody @Validated LoginRequest loginRequest) {
HashMap<String, String> map = new HashMap<>();
map.put("email", loginRequest.email);
map.put("password", loginRequest.password);
return JsonResponse.data(map);
@ImageCaptchaCheckMiddleware
public JsonResponse login(@RequestBody @Validated LoginRequest loginRequest) {
AdminUser adminUser = adminUserService.findByEmail(loginRequest.email);
if (adminUser == null) {
return JsonResponse.error("邮箱不存在");
}
String password = MD5Util.md5(loginRequest.getPassword() + adminUser.getSalt());
if (password != adminUser.getPassword()) {
return JsonResponse.error("密码错误");
}
if (adminUser.getIsBanLogin() == 1) {
return JsonResponse.error("当前用户禁止登录");
}
return JsonResponse.success("success");
}
@PostMapping("/logout")
public JsonResponse<String> logout() {
public JsonResponse logout() {
return JsonResponse.success("success");
}

View File

@@ -0,0 +1,32 @@
package xyz.playedu.api.controller.admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.playedu.api.service.ImageCaptchaService;
import xyz.playedu.api.types.ImageCaptchaResult;
import xyz.playedu.api.types.JsonResponse;
import java.io.IOException;
import java.util.HashMap;
@RestController
@RequestMapping("/admin/v1/system")
public class SystemController {
@Autowired
private ImageCaptchaService imageCaptchaService;
@GetMapping("/image-captcha")
public JsonResponse imageCaptcha() throws IOException {
ImageCaptchaResult imageCaptchaResult = imageCaptchaService.generate();
HashMap<String, String> data = new HashMap();
data.put("key", imageCaptchaResult.getKey());
data.put("image", imageCaptchaResult.getImage());
return JsonResponse.data(data);
}
}