From 349f8dc0d4893a1b33e4a221eb98086f7fa7dcb9 Mon Sep 17 00:00:00 2001 From: 21819 Date: Sat, 27 Jan 2024 17:14:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=A8=A1=E5=9D=97=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../biutag/outeradmin/config/LoginConfig.java | 19 ++++++ .../config/UserLoginInterceptor.java | 21 +++++++ .../controller/LoginController.java | 59 +++++++++++++++++++ .../biutag/outeradmin/dto/LoginFormData.java | 9 +++ 4 files changed, 108 insertions(+) create mode 100644 mailbox-outer-admin/src/main/java/com/biutag/outeradmin/config/LoginConfig.java create mode 100644 mailbox-outer-admin/src/main/java/com/biutag/outeradmin/config/UserLoginInterceptor.java create mode 100644 mailbox-outer-admin/src/main/java/com/biutag/outeradmin/controller/LoginController.java create mode 100644 mailbox-outer-admin/src/main/java/com/biutag/outeradmin/dto/LoginFormData.java diff --git a/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/config/LoginConfig.java b/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/config/LoginConfig.java new file mode 100644 index 0000000..bde4570 --- /dev/null +++ b/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/config/LoginConfig.java @@ -0,0 +1,19 @@ +package com.biutag.outeradmin.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class LoginConfig implements WebMvcConfigurer { + @Override + public void addInterceptors(InterceptorRegistry registry) { + InterceptorRegistration interceptorRegistration = registry.addInterceptor(new UserLoginInterceptor()); + interceptorRegistration.addPathPatterns("/**"); + interceptorRegistration.excludePathPatterns( + "/login", + "/captcha" + ); + } +} diff --git a/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/config/UserLoginInterceptor.java b/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/config/UserLoginInterceptor.java new file mode 100644 index 0000000..2c71f0d --- /dev/null +++ b/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/config/UserLoginInterceptor.java @@ -0,0 +1,21 @@ +package com.biutag.outeradmin.config; + +import com.biutag.outeradmin.entity.User; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import org.springframework.web.servlet.HandlerInterceptor; + + +public class UserLoginInterceptor implements HandlerInterceptor { + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + HttpSession session = request.getSession(); + User user = (User) session.getAttribute("user"); + if (user != null) + return true; +// response.sendRedirect(request.getContextPath() + "/login"); + + return false; + } +} diff --git a/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/controller/LoginController.java b/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/controller/LoginController.java new file mode 100644 index 0000000..22871b9 --- /dev/null +++ b/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/controller/LoginController.java @@ -0,0 +1,59 @@ +package com.biutag.outeradmin.controller; + +import com.alibaba.fastjson2.JSON; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.biutag.enums.ErrorEnum; +import com.biutag.outeradmin.dto.LoginFormData; +import com.biutag.outeradmin.entity.User; +import com.biutag.outeradmin.mapper.UserMapper; +import com.biutag.outeradmin.service.UserService; +import jakarta.servlet.http.HttpServletRequest; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +public class LoginController { + private final UserService userService; + private final UserMapper userMapper; + + private static String randomCaptcha = ""; + + + @RequestMapping("/login") + public int login(@RequestBody String formData, HttpServletRequest request) { + LoginFormData data = JSON.parseObject(formData, LoginFormData.class); + System.out.println(data.toString()); + System.out.println(randomCaptcha); + if (!data.getCaptcha().equals(randomCaptcha)) { + return ErrorEnum.CAPTCHA_ERROR.getCode(); + } else { + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("phone", data.getAccount()); + User user = userService.getOne(queryWrapper, false); + if (user == null) { + return ErrorEnum.LOGIN_ACCOUNT_ERROR.getCode(); + } else { + return ErrorEnum.SUCCESS.getCode(); + } + } + } + + @RequestMapping("/captcha") + public String captcha(@RequestBody String formData) { + LoginFormData data = JSON.parseObject(formData, LoginFormData.class); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("phone", data.getAccount()); + User user = userService.getOne(queryWrapper, false); + if (user == null) { + return Integer.toString(ErrorEnum.LOGIN_ACCOUNT_ERROR.getCode()); + } else { + randomCaptcha = Integer.toString((int) ((Math.random() * 9 + 1) * 100000)); + System.out.println(randomCaptcha); + return randomCaptcha; + } + + } +} diff --git a/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/dto/LoginFormData.java b/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/dto/LoginFormData.java new file mode 100644 index 0000000..0452a16 --- /dev/null +++ b/mailbox-outer-admin/src/main/java/com/biutag/outeradmin/dto/LoginFormData.java @@ -0,0 +1,9 @@ +package com.biutag.outeradmin.dto; + +import lombok.Data; + +@Data +public class LoginFormData { + private String account; + private String captcha; +}