mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2025-09-04 19:38:02 +00:00
add 增加 邮箱验证码发送接口
add 增加 邮箱登陆接口
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.ruoyi.auth.controller;
|
||||
|
||||
import com.ruoyi.auth.form.EmailLoginBody;
|
||||
import com.ruoyi.auth.form.LoginBody;
|
||||
import com.ruoyi.auth.form.RegisterBody;
|
||||
import com.ruoyi.auth.form.SmsLoginBody;
|
||||
@@ -58,6 +59,21 @@ public class TokenController {
|
||||
return R.ok(ajax);
|
||||
}
|
||||
|
||||
/**
|
||||
* 邮件登录
|
||||
*
|
||||
* @param body 登录信息
|
||||
* @return 结果
|
||||
*/
|
||||
@PostMapping("/emailLogin")
|
||||
public R<Map<String, Object>> emailLogin(@Validated @RequestBody EmailLoginBody body) {
|
||||
Map<String, Object> ajax = new HashMap<>();
|
||||
// 生成令牌
|
||||
String token = sysLoginService.emailLogin(body.getEmail(), body.getEmailCode());
|
||||
ajax.put(Constants.ACCESS_TOKEN, token);
|
||||
return R.ok(ajax);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小程序登录(示例)
|
||||
*
|
||||
|
@@ -0,0 +1,30 @@
|
||||
package com.ruoyi.auth.form;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* 短信登录对象
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class EmailLoginBody {
|
||||
|
||||
/**
|
||||
* 邮箱
|
||||
*/
|
||||
@NotBlank(message = "{user.email.not.blank}")
|
||||
@Email(message = "{user.email.not.valid}")
|
||||
private String email;
|
||||
|
||||
/**
|
||||
* 邮箱code
|
||||
*/
|
||||
@NotBlank(message = "{email.code.not.blank}")
|
||||
private String emailCode;
|
||||
|
||||
}
|
@@ -14,13 +14,13 @@ import javax.validation.constraints.NotBlank;
|
||||
public class SmsLoginBody {
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
* 手机号
|
||||
*/
|
||||
@NotBlank(message = "{user.phonenumber.not.blank}")
|
||||
private String phonenumber;
|
||||
|
||||
/**
|
||||
* 用户密码
|
||||
* 短信code
|
||||
*/
|
||||
@NotBlank(message = "{sms.code.not.blank}")
|
||||
private String smsCode;
|
||||
|
@@ -78,6 +78,18 @@ public class SysLoginService {
|
||||
return StpUtil.getTokenValue();
|
||||
}
|
||||
|
||||
public String emailLogin(String email, String emailCode) {
|
||||
// 通过邮箱查找用户
|
||||
LoginUser userInfo = remoteUserService.getUserInfoByEmail(email);
|
||||
|
||||
checkLogin(LoginType.EMAIL, userInfo.getUsername(), () -> !validateEmailCode(email, emailCode));
|
||||
// 生成token
|
||||
LoginHelper.loginByDevice(userInfo, DeviceType.APP);
|
||||
|
||||
recordLogininfor(userInfo.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
|
||||
return StpUtil.getTokenValue();
|
||||
}
|
||||
|
||||
public String xcxLogin(String xcxCode) {
|
||||
// xcxCode 为 小程序调用 wx.login 授权后获取
|
||||
// todo 自行实现 校验 appid + appsrcret + xcxCode 调用登录凭证校验接口 获取 session_key 与 openid
|
||||
@@ -171,6 +183,18 @@ public class SysLoginService {
|
||||
return code.equals(smsCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验邮箱验证码
|
||||
*/
|
||||
private boolean validateEmailCode(String email, String emailCode) {
|
||||
String code = RedisUtils.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + email);
|
||||
if (StringUtils.isBlank(code)) {
|
||||
recordLogininfor(email, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
|
||||
throw new CaptchaExpireException();
|
||||
}
|
||||
return code.equals(emailCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录校验
|
||||
*/
|
||||
|
Reference in New Issue
Block a user