|
|
@@ -1,20 +1,25 @@
|
|
|
package com.ruoyi.thirduser.controller;
|
|
|
|
|
|
+import com.ruoyi.common.MailUtils;
|
|
|
import com.ruoyi.common.annotation.Anonymous;
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
import com.ruoyi.common.core.domain.model.LoginBody;
|
|
|
import com.ruoyi.common.core.domain.model.RegisterBody;
|
|
|
+import com.ruoyi.common.core.redis.RedisCache;
|
|
|
import com.ruoyi.common.utils.SecurityUtils;
|
|
|
+import com.ruoyi.thirduser.service.SendMailService;
|
|
|
import com.ruoyi.thirduser.service.ThirdUserloginService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@Slf4j
|
|
|
@RestController
|
|
|
@@ -23,6 +28,12 @@ public class LoginController {
|
|
|
@Autowired
|
|
|
private ThirdUserloginService thirdUserloginService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SendMailService sendMailService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisCache redisCache;
|
|
|
+
|
|
|
@PostMapping("/mailLogin")
|
|
|
@Anonymous
|
|
|
public AjaxResult mailLogin(@RequestBody LoginBody loginBody){
|
|
|
@@ -49,10 +60,37 @@ public class LoginController {
|
|
|
|
|
|
@PostMapping("/forgetPwd")
|
|
|
@Anonymous
|
|
|
- public AjaxResult forgetPwd(HttpServletRequest request){
|
|
|
+ public AjaxResult forgetPwd(HttpServletRequest request) {
|
|
|
String account = request.getParameter("username");
|
|
|
+ if (redisCache.hasKey(Constants.RESET_PWD_KEY+account)){
|
|
|
+ long expire = redisCache.getExpire(Constants.RESET_PWD_KEY + account);
|
|
|
+ return AjaxResult.error("Try again in "+expire+" seconds");
|
|
|
+ }
|
|
|
+ sendMailService.sendMailCode(account);
|
|
|
String lng = request.getParameter("locale");
|
|
|
return AjaxResult.success(true);
|
|
|
+ }
|
|
|
|
|
|
+ @GetMapping("/restPwd/{token}")
|
|
|
+ @Anonymous
|
|
|
+ public AjaxResult restPwd(@PathVariable String token) {
|
|
|
+ try {
|
|
|
+ // 使用AES算法创建Cipher对象
|
|
|
+ Cipher cipher = Cipher.getInstance("AES");
|
|
|
+ // 创建密钥对象
|
|
|
+ SecretKeySpec secretKeySpec = new SecretKeySpec(Constants.RESET_PWD_KEY.getBytes(), "AES");
|
|
|
+ // 初始化Cipher对象为解密模式
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
|
|
|
+ // 解密数据
|
|
|
+ byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(token));
|
|
|
+ String username = new String(decryptedBytes);
|
|
|
+ //System.out.println("解密后的邮箱: " + username);
|
|
|
+ if (!redisCache.hasKey(Constants.RESET_PWD_KEY+username)){
|
|
|
+ return AjaxResult.error("The connection is invalid!");
|
|
|
+ }
|
|
|
+ return thirdUserloginService.updateUserPwd(username);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return AjaxResult.error();
|
|
|
+ }
|
|
|
}
|
|
|
}
|