Browse Source

登录模块实现

dev_ycq
21819 2 years ago
parent
commit
349f8dc0d4
  1. 19
      mailbox-outer-admin/src/main/java/com/biutag/outeradmin/config/LoginConfig.java
  2. 21
      mailbox-outer-admin/src/main/java/com/biutag/outeradmin/config/UserLoginInterceptor.java
  3. 59
      mailbox-outer-admin/src/main/java/com/biutag/outeradmin/controller/LoginController.java
  4. 9
      mailbox-outer-admin/src/main/java/com/biutag/outeradmin/dto/LoginFormData.java

19
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"
);
}
}

21
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;
}
}

59
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<User> 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<User> 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;
}
}
}

9
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;
}
Loading…
Cancel
Save