|
@@ -1,12 +1,18 @@
|
|
|
package com.backendsys.modules.system.service.impl;
|
|
|
|
|
|
import com.backendsys.config.Kaptcha.KaptchaUtil;
|
|
|
+import com.backendsys.exception.CustException;
|
|
|
import com.backendsys.modules.common.config.redis.utils.RedisUtil;
|
|
|
+import com.backendsys.modules.common.config.security.utils.CaptchaUtil;
|
|
|
+import com.backendsys.modules.common.config.security.utils.CountUtilV2;
|
|
|
+import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
|
|
|
import com.backendsys.modules.system.dao.SysMobileAreaDao;
|
|
|
+import com.backendsys.modules.system.dao.SysUserDao;
|
|
|
+import com.backendsys.modules.system.entity.SysAuth;
|
|
|
import com.backendsys.modules.system.entity.SysMobileArea;
|
|
|
+import com.backendsys.modules.system.entity.SysUser;
|
|
|
import com.backendsys.modules.system.service.SysAuthV2Service;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.backendsys.utils.response.ResultEnum;
|
|
|
import com.google.code.kaptcha.Producer;
|
|
|
import jakarta.servlet.ServletOutputStream;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
@@ -20,29 +26,39 @@ import java.awt.image.BufferedImage;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Service
|
|
|
public class SysAuthV2ServiceImpl implements SysAuthV2Service {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private HttpRequestUtil httpRequestUtil;
|
|
|
@Autowired
|
|
|
private RedisUtil redisUtil;
|
|
|
+ @Autowired
|
|
|
+ private CountUtilV2 countUtilV2;
|
|
|
+ @Autowired
|
|
|
+ private CaptchaUtil captchaUtil;
|
|
|
+
|
|
|
@Autowired
|
|
|
private Producer captchaProducer;
|
|
|
@Value("${CAPTCHA_DURATION}")
|
|
|
private Integer CAPTCHA_DURATION;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SysUserDao sysUserDao;
|
|
|
@Autowired
|
|
|
private SysMobileAreaDao sysMobileAreaDao;
|
|
|
|
|
|
@Override
|
|
|
- public void renderCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
|
+ public void renderCaptcha(HttpServletResponse response) throws IOException {
|
|
|
byte[] captchaChallengeAsJpeg;
|
|
|
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
|
|
|
try {
|
|
|
String createText = captchaProducer.createText();
|
|
|
// 获得当前 (UA + IP) 生成的 Key
|
|
|
- String captchaRedisKey = KaptchaUtil.getKaptchaKey(request);
|
|
|
+ String captchaRedisKey = httpRequestUtil.getKaptchaKey();
|
|
|
// 保存 验证码字符串 到 redis 中
|
|
|
redisUtil.setCacheObject(captchaRedisKey, createText, this.CAPTCHA_DURATION, TimeUnit.MILLISECONDS);
|
|
|
// 返回 BufferedImage 对象并转为 byte 写入到 byte 数组中
|
|
@@ -68,4 +84,48 @@ public class SysAuthV2ServiceImpl implements SysAuthV2Service {
|
|
|
return sysMobileAreaDao.selectMobileAreaList(sysMobileArea);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> login(SysAuth sysAuth) {
|
|
|
+
|
|
|
+ System.out.println(sysAuth);
|
|
|
+ System.out.println(httpRequestUtil.getIpAddr());
|
|
|
+
|
|
|
+ String username = sysAuth.getUsername();
|
|
|
+ String password = sysAuth.getPassword();
|
|
|
+ String captcha = sysAuth.getCaptcha();
|
|
|
+
|
|
|
+ // 判断是否处于 5次的错误状态
|
|
|
+ countUtilV2.checkErrorStatus("login-error", sysAuth.getUsername());
|
|
|
+
|
|
|
+ // [Method] 判断验证码是否正确 (RedisKey: (ua + ip))
|
|
|
+ String captchaRedisKey = httpRequestUtil.getKaptchaKey();
|
|
|
+ if (!captchaUtil.isCaptchaValid(captcha, captchaRedisKey)) {
|
|
|
+ redisUtil.delete(captchaRedisKey);
|
|
|
+ throw new CustException("验证码错误", ResultEnum.INVALID_CREDENTIALS.getCode());
|
|
|
+ }
|
|
|
+
|
|
|
+ // [Method] 判断 用户 是否存在 && 密码是否正确
|
|
|
+// SysUser sysUser = sysUserDao.selectOne()
|
|
|
+
|
|
|
+// Map<String, Object> sysUserSimple = sysUserMapper.queryUserByIdOrName(null, username, null, null);
|
|
|
+// if (sysUserSimple != null) {
|
|
|
+// sysUserDTO.setUser_id((Long) sysUserSimple.get("id"));
|
|
|
+// }
|
|
|
+// if (!(sysUserSimple != null && isUserPasswordValid(sysUserSimple, password))) {
|
|
|
+// stringRedisTemplate.delete(captchaRedisKey);
|
|
|
+// // 添加错误标记 (2分钟内错误5次,则出现提示)
|
|
|
+// countUtil.setErrorCount("login-error", username);
|
|
|
+// //
|
|
|
+// throw new CustException("用户名或密码错误", ResultEnum.INVALID_CREDENTIALS.getCode());
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 1.作废验证码密钥
|
|
|
+// stringRedisTemplate.delete(captchaRedisKey);
|
|
|
+//
|
|
|
+// // [登录成功]
|
|
|
+// Map<String, Object> result = loginSuccess(request, sysUserDTO);
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
}
|