|
|
@@ -1,6 +1,7 @@
|
|
|
package com.ctsi.Auth.web;
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import com.ctsi.Auth.entity.CaptchaPO;
|
|
|
import com.ctsi.Auth.entity.TokenInfoPO;
|
|
|
import com.ctsi.Auth.entity.UserForm;
|
|
|
import com.ctsi.Auth.entity.UserInfoPO;
|
|
|
@@ -24,8 +25,6 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import javax.servlet.http.HttpSession;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Base64;
|
|
|
import java.util.List;
|
|
|
@@ -48,19 +47,18 @@ public class login {
|
|
|
private static final Logger log = LoggerFactory.getLogger(login.class);
|
|
|
@ApiOperation("登录")
|
|
|
@PostMapping(value = "/login",name = "登录")
|
|
|
- public ApiResult auth(@RequestBody UserForm userForm, HttpServletRequest request){
|
|
|
+ public ApiResult auth(@RequestBody UserForm userForm){
|
|
|
try {
|
|
|
// 验证码校验是否开启
|
|
|
if (captchaType) {
|
|
|
- // 从 Session 中获取之前生成的验证码
|
|
|
- HttpSession session = request.getSession();
|
|
|
- String storedCaptcha = (String) session.getAttribute("captcha");
|
|
|
- if (storedCaptcha == null)
|
|
|
- return ApiResult.result(ErrorCodeEnum.CAPTCHA_INVALID);
|
|
|
- // 从 Session 中删除验证码
|
|
|
- session.removeAttribute("captcha");
|
|
|
+ // 从 redis 中获取之前生成的验证码
|
|
|
+ String captcha =(String) redisUtil.get(userForm.getCaptchaKey());
|
|
|
+ if (captcha == null)
|
|
|
+ return ApiResult.result(ErrorCodeEnum.CAPTCHA_EXPIRED);
|
|
|
+ // 从 redis 中删除验证码
|
|
|
+ redisUtil.remove(userForm.getCaptchaKey());
|
|
|
// 比较传入的验证码和 Session 中的验证码是否相等
|
|
|
- if (!storedCaptcha.equalsIgnoreCase(userForm.getCaptcha()))
|
|
|
+ if (!captcha.equalsIgnoreCase(userForm.getCaptcha()))
|
|
|
return ApiResult.result(ErrorCodeEnum.CAPTCHA_INVALID);
|
|
|
}
|
|
|
SysUser User = sysUserService.queryByUserName(userForm.getUserName());
|
|
|
@@ -107,7 +105,7 @@ public class login {
|
|
|
|
|
|
@ApiOperation("获取验证码")
|
|
|
@GetMapping("/captcha")
|
|
|
- public ApiResult getCaptcha(HttpServletRequest request) {
|
|
|
+ public ApiResult getCaptcha() {
|
|
|
try {
|
|
|
String captcha = CaptchaUtil.generateRandomString();
|
|
|
byte[] captchaImage = CaptchaUtil.generateCaptcha(captcha);
|
|
|
@@ -115,8 +113,11 @@ public class login {
|
|
|
String png_base64 = encoder.encodeToString(captchaImage);;//转换成base64串
|
|
|
png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");
|
|
|
String uuid = UUID.randomUUID().toString();
|
|
|
- redisUtil.set(uuid, captcha, 60L);
|
|
|
- return ApiResult.success("data:image/jpg;base64," + png_base64);
|
|
|
+ redisUtil.set(uuid, captcha, 360L);
|
|
|
+ CaptchaPO captchaPO = new CaptchaPO();
|
|
|
+ captchaPO.setCaptchaKey(uuid);
|
|
|
+ captchaPO.setCaptcha("data:image/jpg;base64," + png_base64);
|
|
|
+ return ApiResult.success(captchaPO);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
return ApiResult.failure("获取验证码失败",null);
|