|
@@ -1,20 +1,40 @@
|
|
|
package cn.iocoder.yudao.userserver.modules.member.service.auth.impl;
|
|
|
|
|
|
+import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
|
|
|
+import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
|
|
import cn.iocoder.yudao.framework.security.core.LoginUser;
|
|
|
import cn.iocoder.yudao.userserver.modules.member.controller.auth.vo.MbrAuthLoginReqVO;
|
|
|
import cn.iocoder.yudao.userserver.modules.member.convert.user.MbrAuthConvert;
|
|
|
import cn.iocoder.yudao.userserver.modules.member.dal.dataobject.user.MbrUserDO;
|
|
|
+import cn.iocoder.yudao.userserver.modules.member.enums.MbrErrorCodeConstants;
|
|
|
import cn.iocoder.yudao.userserver.modules.member.service.auth.MbrAuthService;
|
|
|
import cn.iocoder.yudao.userserver.modules.member.service.user.MbrUserService;
|
|
|
+import cn.iocoder.yudao.userserver.modules.system.enums.logger.SysLoginLogTypeEnum;
|
|
|
+import cn.iocoder.yudao.userserver.modules.system.enums.logger.SysLoginResultEnum;
|
|
|
+import cn.iocoder.yudao.userserver.modules.system.service.auth.SysUserSessionService;
|
|
|
+import cn.iocoder.yudao.userserver.modules.system.service.logger.SysLoginLogService;
|
|
|
+import cn.iocoder.yudao.userserver.modules.system.service.logger.dto.SysLoginLogCreateReqDTO;
|
|
|
+import cn.iocoder.yudao.userserver.modules.system.service.logger.impl.SysLoginLogServiceImpl;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
+import org.springframework.security.authentication.BadCredentialsException;
|
|
|
+import org.springframework.security.authentication.DisabledException;
|
|
|
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
+import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.AuthenticationException;
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.Assert;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.iocoder.yudao.userserver.modules.member.enums.MbrErrorCodeConstants.*;
|
|
|
+
|
|
|
/**
|
|
|
* Auth Service 实现类
|
|
|
*
|
|
@@ -30,6 +50,10 @@ public class MbrAuthServiceImpl implements MbrAuthService {
|
|
|
|
|
|
@Resource
|
|
|
private MbrUserService userService;
|
|
|
+ @Resource
|
|
|
+ private SysLoginLogService loginLogService;
|
|
|
+ @Resource
|
|
|
+ private SysUserSessionService userSessionService;
|
|
|
|
|
|
@Override
|
|
|
public UserDetails loadUserByUsername(String mobile) throws UsernameNotFoundException {
|
|
@@ -44,7 +68,57 @@ public class MbrAuthServiceImpl implements MbrAuthService {
|
|
|
|
|
|
@Override
|
|
|
public String login(MbrAuthLoginReqVO reqVO, String userIp, String userAgent) {
|
|
|
- return null;
|
|
|
+ // 使用手机 + 密码,进行登录。
|
|
|
+ LoginUser loginUser = this.login0(reqVO.getMobile(), reqVO.getPassword());
|
|
|
+
|
|
|
+ // 缓存登录用户到 Redis 中,返回 sessionId 编号
|
|
|
+ return userSessionService.createUserSession(loginUser, userIp, userAgent);
|
|
|
+ }
|
|
|
+
|
|
|
+ private LoginUser login0(String username, String password) {
|
|
|
+ final SysLoginLogTypeEnum logTypeEnum = SysLoginLogTypeEnum.LOGIN_USERNAME;
|
|
|
+ // 用户验证
|
|
|
+ Authentication authentication;
|
|
|
+ try {
|
|
|
+ // 调用 Spring Security 的 AuthenticationManager#authenticate(...) 方法,使用账号密码进行认证
|
|
|
+ // 在其内部,会调用到 loadUserByUsername 方法,获取 User 信息
|
|
|
+ authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
|
|
|
+ } catch (BadCredentialsException badCredentialsException) {
|
|
|
+ this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.BAD_CREDENTIALS);
|
|
|
+ throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
|
|
+ } catch (DisabledException disabledException) {
|
|
|
+ this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.USER_DISABLED);
|
|
|
+ throw exception(AUTH_LOGIN_USER_DISABLED);
|
|
|
+ } catch (AuthenticationException authenticationException) {
|
|
|
+ log.error("[login0][username({}) 发生未知异常]", username, authenticationException);
|
|
|
+ this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.UNKNOWN_ERROR);
|
|
|
+ throw exception(AUTH_LOGIN_FAIL_UNKNOWN);
|
|
|
+ }
|
|
|
+ // 登录成功的日志
|
|
|
+ Assert.notNull(authentication.getPrincipal(), "Principal 不会为空");
|
|
|
+ this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.SUCCESS);
|
|
|
+ return (LoginUser) authentication.getPrincipal();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void createLoginLog(String mobile, SysLoginLogTypeEnum logTypeEnum, SysLoginResultEnum loginResult) {
|
|
|
+ // 获得用户
|
|
|
+ MbrUserDO user = userService.getUserByMobile(mobile);
|
|
|
+ // 插入登录日志
|
|
|
+ SysLoginLogCreateReqDTO reqDTO = new SysLoginLogCreateReqDTO();
|
|
|
+ reqDTO.setLogType(logTypeEnum.getType());
|
|
|
+ reqDTO.setTraceId(TracerUtils.getTraceId());
|
|
|
+ if (user != null) {
|
|
|
+ reqDTO.setUserId(user.getId());
|
|
|
+ }
|
|
|
+ reqDTO.setUsername(mobile);
|
|
|
+ reqDTO.setUserAgent(ServletUtils.getUserAgent());
|
|
|
+ reqDTO.setUserIp(ServletUtils.getClientIP());
|
|
|
+ reqDTO.setResult(loginResult.getResult());
|
|
|
+ loginLogService.createLoginLog(reqDTO);
|
|
|
+ // 更新最后登录时间
|
|
|
+ if (user != null && Objects.equals(SysLoginResultEnum.SUCCESS.getResult(), loginResult.getResult())) {
|
|
|
+ userService.updateUserLogin(user.getId(), ServletUtils.getClientIP());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|