4 changed files with 108 additions and 0 deletions
@ -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" |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
@ -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; |
||||||
|
} |
||||||
|
} |
||||||
@ -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; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue