邮箱登录功能

dev
Shen jia feng 2024-04-12 14:38:53 +08:00
parent 5d2cb358bb
commit 47571b1f06
14 changed files with 136 additions and 64 deletions

View File

@ -34,7 +34,8 @@ public class TokenController {
@PostMapping("login") @PostMapping("login")
public Result<?> login (@RequestBody LoginBody form) { public Result<?> login (@RequestBody LoginBody form) {
// 用户登录 // 用户登录
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword()); // LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
LoginUser userInfo = sysLoginService.login(form.getEmail(), form.getPassword());
// 获取登录token // 获取登录token
return Result.success(tokenService.createToken(userInfo)); return Result.success(tokenService.createToken(userInfo));
} }
@ -66,7 +67,7 @@ public class TokenController {
@PostMapping("register") @PostMapping("register")
public Result<?> register (@RequestBody RegisterBody registerBody) { public Result<?> register (@RequestBody RegisterBody registerBody) {
// 用户注册 // 用户注册
sysLoginService.register(registerBody.getUsername(), registerBody.getPassword()); sysLoginService.register(registerBody.getEmail(), registerBody.getPassword());
return Result.success(); return Result.success();
} }
} }

View File

@ -1,34 +1,51 @@
package com.muyu.auth.form; package com.muyu.auth.form;
import lombok.Data;
/** /**
* *
* *
* @author muyu * @author muyu
*/ */
@Data
public class LoginBody { public class LoginBody {
/** /**
* *
*/ */
private String username; private String username;
/**
*
*/
private String email;
// public LoginBody() {
// }
// public LoginBody(String email) {
// this.email = email;
// }
// -----------------------
/** /**
* *
*/ */
private String password; private String password;
public String getUsername () { // public String getUsername () {
return username; // return username;
} // }
//
public void setUsername (String username) { // public void setUsername (String username) {
this.username = username; // this.username = username;
} // }
//
public String getPassword () { // public String getPassword () {
return password; // return password;
} // }
//
public void setPassword (String password) { // public void setPassword (String password) {
this.password = password; // this.password = password;
} // }
} }

View File

@ -40,36 +40,43 @@ public class SysLoginService {
/** /**
* *
*/ */
public LoginUser login (String username, String password) { public LoginUser login (String email, String password) {
// public LoginUser login (String username, String password) {
// 用户名或密码为空 错误 // 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password)) { // if (StringUtils.isAnyBlank(username, password)) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写"); if (StringUtils.isAnyBlank(email, password)) {
throw new ServiceException("用户/密码必须填写"); // recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户/密码必须填写");
throw new ServiceException("用户/邮箱 密码必须填写");
} }
// 密码如果不在指定范围内 错误 // 密码如果不在指定范围内 错误
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) { || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围"); // recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户密码不在指定范围");
throw new ServiceException("用户密码不在指定范围"); throw new ServiceException("用户密码不在指定范围");
} }
// 用户名不在指定范围内 错误 // 用户名不在指定范围内 错误
if (username.length() < UserConstants.USERNAME_MIN_LENGTH if (email.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) { || email.length() > UserConstants.USERNAME_MAX_LENGTH) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户名不在指定范围");
throw new ServiceException("用户名不在指定范围"); throw new ServiceException("用户名不在指定范围");
} }
// IP黑名单校验 // IP黑名单校验
String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST)); String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) { if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "很遗憾访问IP已被列入系统黑名单"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "很遗憾访问IP已被列入系统黑名单");
throw new ServiceException("很遗憾访问IP已被列入系统黑名单"); throw new ServiceException("很遗憾访问IP已被列入系统黑名单");
} }
// 查询用户信息 // 查询用户信息
Result<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER); // Result<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
Result<LoginUser> userResult = remoteUserService.getUserInfo(email, SecurityConstants.INNER);
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) { if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在"); // recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + username + " 不存在"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "登录用户不存在");
// throw new ServiceException("登录用户:" + username + " 不存在");
throw new ServiceException("登录用户:" + email + " 不存在");
} }
if (Result.FAIL == userResult.getCode()) { if (Result.FAIL == userResult.getCode()) {
@ -79,15 +86,19 @@ public class SysLoginService {
LoginUser userInfo = userResult.getData(); LoginUser userInfo = userResult.getData();
SysUser user = userResult.getData().getSysUser(); SysUser user = userResult.getData().getSysUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) { if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除"); // recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
throw new ServiceException("对不起,您的账号:" + username + " 已被删除"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
// throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
throw new ServiceException("对不起,您的账号:" + email + " 已被删除");
} }
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) { if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员"); // recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + username + " 已停用"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + email + " 已停用");
} }
passwordService.validate(user, password); passwordService.validate(user, password);
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功"); // recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
recordLogService.recordLogininfor(email, Constants.LOGIN_SUCCESS, "登录成功");
return userInfo; return userInfo;
} }
@ -98,13 +109,15 @@ public class SysLoginService {
/** /**
* *
*/ */
public void register (String username, String password) { // public void register (String username, String password) {
public void register (String email, String password) {
// 用户名或密码为空 错误 // 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password)) { // if (StringUtils.isAnyBlank(username, password)) {
if (StringUtils.isAnyBlank(email, password)) {
throw new ServiceException("用户/密码必须填写"); throw new ServiceException("用户/密码必须填写");
} }
if (username.length() < UserConstants.USERNAME_MIN_LENGTH if (email.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) { || email.length() > UserConstants.USERNAME_MAX_LENGTH) {
throw new ServiceException("账户长度必须在2到20个字符之间"); throw new ServiceException("账户长度必须在2到20个字符之间");
} }
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
@ -114,14 +127,15 @@ public class SysLoginService {
// 注册用户信息 // 注册用户信息
SysUser sysUser = new SysUser(); SysUser sysUser = new SysUser();
sysUser.setUserName(username); sysUser.setUserName(email);
sysUser.setNickName(username); sysUser.setNickName(email);
sysUser.setPassword(SecurityUtils.encryptPassword(password)); sysUser.setPassword(SecurityUtils.encryptPassword(password));
Result<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER); Result<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
if (Result.FAIL == registerResult.getCode()) { if (Result.FAIL == registerResult.getCode()) {
throw new ServiceException(registerResult.getMsg()); throw new ServiceException(registerResult.getMsg());
} }
recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功"); // recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
recordLogService.recordLogininfor(email, Constants.REGISTER, "注册成功");
} }
} }

View File

@ -28,9 +28,11 @@ public class SysRecordLogService {
* *
* @return * @return
*/ */
public void recordLogininfor (String username, String status, String message) { // public void recordLogininfor (String username, String status, String message) {
public void recordLogininfor (String email, String status, String message) {
SysLogininfor logininfor = new SysLogininfor(); SysLogininfor logininfor = new SysLogininfor();
logininfor.setUserName(username); // logininfor.setUserName(username);
logininfor.setUserName(email);
logininfor.setIpaddr(IpUtils.getIpAddr()); logininfor.setIpaddr(IpUtils.getIpAddr());
logininfor.setMsg(message); logininfor.setMsg(message);
// 日志状态 // 日志状态

View File

@ -1,6 +1,11 @@
package com.muyu.common.system.domain; package com.muyu.common.system.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable; import java.io.Serializable;
import java.util.Set; import java.util.Set;
@ -9,6 +14,10 @@ import java.util.Set;
* *
* @author muyu * @author muyu
*/ */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class LoginUser implements Serializable { public class LoginUser implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -26,6 +35,12 @@ public class LoginUser implements Serializable {
* *
*/ */
private String username; private String username;
//----------------------
/**
*
*/
private String email;
// ------------------
/** /**
* *

View File

@ -37,6 +37,8 @@ public class SysLogininfor extends BaseEntity {
@Excel(name = "用户账号") @Excel(name = "用户账号")
private String userName; private String userName;
/** /**
* 0 1 * 0 1
*/ */

View File

@ -16,16 +16,19 @@ import org.springframework.web.bind.annotation.*;
*/ */
@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = RemoteUserFallbackFactory.class) @FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = RemoteUserFallbackFactory.class)
public interface RemoteUserService { public interface RemoteUserService {
/** /**
* *
* *
* @param username * @param email
* @param source * @param source
* *
* @return * @return
*/ */
@GetMapping("/user/info/{username}") // @GetMapping("/user/info/{username}")
public Result<LoginUser> getUserInfo (@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM_SOURCE) String source); @GetMapping("/user/info/{email}")
public Result<LoginUser> getUserInfo (@PathVariable("email") String email, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/** /**
* *

View File

@ -23,7 +23,8 @@ public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserServ
log.error("用户服务调用失败:{}", throwable.getMessage()); log.error("用户服务调用失败:{}", throwable.getMessage());
return new RemoteUserService() { return new RemoteUserService() {
@Override @Override
public Result<LoginUser> getUserInfo (String username, String source) { // public Result<LoginUser> getUserInfo (String username, String source) {
public Result<LoginUser> getUserInfo (String email, String source) {
return Result.error("获取用户失败:" + throwable.getMessage()); return Result.error("获取用户失败:" + throwable.getMessage());
} }

View File

@ -9,10 +9,10 @@ import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType; import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.service.TokenService; import com.muyu.common.security.service.TokenService;
import com.muyu.common.security.utils.SecurityUtils; import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.remote.RemoteFileService; import com.muyu.common.system.domain.LoginUser;
import com.muyu.common.system.domain.SysFile; import com.muyu.common.system.domain.SysFile;
import com.muyu.common.system.domain.SysUser; import com.muyu.common.system.domain.SysUser;
import com.muyu.common.system.domain.LoginUser; import com.muyu.common.system.remote.RemoteFileService;
import com.muyu.system.domain.resp.ProfileResp; import com.muyu.system.domain.resp.ProfileResp;
import com.muyu.system.service.SysUserService; import com.muyu.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -44,7 +44,8 @@ public class SysProfileController extends BaseController {
@GetMapping @GetMapping
public Result profile () { public Result profile () {
String username = SecurityUtils.getUsername(); String username = SecurityUtils.getUsername();
SysUser user = userService.selectUserByUserName(username); // SysUser user = userService.selectUserByUserName(username);
SysUser user = userService.selectUserByUserEmail(username);
return Result.success( return Result.success(
ProfileResp.builder() ProfileResp.builder()
.roleGroup( userService.selectUserRoleGroup(username) ) .roleGroup( userService.selectUserRoleGroup(username) )
@ -87,7 +88,8 @@ public class SysProfileController extends BaseController {
@PutMapping("/updatePwd") @PutMapping("/updatePwd")
public Result updatePwd (String oldPassword, String newPassword) { public Result updatePwd (String oldPassword, String newPassword) {
String username = SecurityUtils.getUsername(); String username = SecurityUtils.getUsername();
SysUser user = userService.selectUserByUserName(username); // SysUser user = userService.selectUserByUserName(username);
SysUser user = userService.selectUserByUserEmail(username);
String password = user.getPassword(); String password = user.getPassword();
if (!SecurityUtils.matchesPassword(oldPassword, password)) { if (!SecurityUtils.matchesPassword(oldPassword, password)) {
return error("修改密码失败,旧密码错误"); return error("修改密码失败,旧密码错误");

View File

@ -97,9 +97,12 @@ public class SysUserController extends BaseController {
* *
*/ */
@InnerAuth @InnerAuth
@GetMapping("/info/{username}") // @GetMapping("/info/{username}")
public Result<LoginUser> info (@PathVariable("username") String username) { @GetMapping("/info/{email}")
SysUser sysUser = userService.selectUserByUserName(username); // public Result<LoginUser> info (@PathVariable("username") String username) {
public Result<LoginUser> info (@PathVariable("email") String email) {
// SysUser sysUser = userService.selectUserByUserName(username);
SysUser sysUser = userService.selectUserByUserEmail(email);
if (StringUtils.isNull(sysUser)) { if (StringUtils.isNull(sysUser)) {
return Result.error("用户名或密码错误"); return Result.error("用户名或密码错误");
} }
@ -120,12 +123,14 @@ public class SysUserController extends BaseController {
@InnerAuth @InnerAuth
@PostMapping("/register") @PostMapping("/register")
public Result<Boolean> register (@RequestBody SysUser sysUser) { public Result<Boolean> register (@RequestBody SysUser sysUser) {
String username = sysUser.getUserName(); // String username = sysUser.getUserName();
String email = sysUser.getUserName();
if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) { if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
return Result.error("当前系统没有开启注册功能!"); return Result.error("当前系统没有开启注册功能!");
} }
if (!userService.checkUserNameUnique(sysUser)) { if (!userService.checkUserNameUnique(sysUser)) {
return Result.error("保存用户'" + username + "'失败,注册账号已存在"); // return Result.error("保存用户'" + username + "'失败,注册账号已存在");
return Result.error("保存用户'" + email + "'失败,注册账号已存在");
} }
return Result.success(userService.registerUser(sysUser)); return Result.success(userService.registerUser(sysUser));
} }

View File

@ -46,7 +46,8 @@ public interface SysUserMapper extends BaseMapper<SysUser> {
* *
* @return * @return
*/ */
public SysUser selectUserByUserName (String userName); // public SysUser selectUserByUserName (String userName);
public SysUser selectUserByUserEmail (String email);
/** /**
* ID * ID

View File

@ -41,11 +41,12 @@ public interface SysUserService extends IService<SysUser> {
/** /**
* *
* *
* @param userName * @param email
* *
* @return * @return
*/ */
public SysUser selectUserByUserName (String userName); // public SysUser selectUserByUserName (String userName);
public SysUser selectUserByUserEmail (String email);
/** /**
* ID * ID

View File

@ -97,9 +97,15 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
* *
* @return * @return
*/ */
// @Override
// public SysUser selectUserByUserName (String userName) {
// return userMapper.selectUserByUserName(userName);
// }
@Override @Override
public SysUser selectUserByUserName (String userName) { public SysUser selectUserByUserEmail (String email) {
return userMapper.selectUserByUserName(userName); // public SysUser selectUserByUserEmail (String userName) {
return userMapper.selectUserByUserEmail(email);
} }
/** /**
@ -463,7 +469,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
for (SysUser user : userList) { for (SysUser user : userList) {
try { try {
// 验证是否存在这个用户 // 验证是否存在这个用户
SysUser u = userMapper.selectUserByUserName(user.getUserName()); // SysUser u = userMapper.selectUserByUserName(user.getUserName());
SysUser u = userMapper.selectUserByUserEmail(user.getUserName());
if (StringUtils.isNull(u)) { if (StringUtils.isNull(u)) {
BeanValidators.validateWithException(validator, user); BeanValidators.validateWithException(validator, user);
user.setPassword(SecurityUtils.encryptPassword(password)); user.setPassword(SecurityUtils.encryptPassword(password));

View File

@ -149,10 +149,11 @@
<!-- 数据范围过滤 --> <!-- 数据范围过滤 -->
${params.dataScope} ${params.dataScope}
</select> </select>
<!-- where u.user_name = #{userName} and u.del_flag = '0'-->
<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult"> <select id="selectUserByUserEmail" parameterType="String" resultMap="SysUserResult">
<include refid="selectUserVo"/> <include refid="selectUserVo"/>
where u.user_name = #{userName} and u.del_flag = '0' where u.user_name = #{email} or u.email =#{email} and u.del_flag = '0'
</select> </select>
<select id="selectUserById" parameterType="Long" resultMap="SysUserResult"> <select id="selectUserById" parameterType="Long" resultMap="SysUserResult">