| | |
| | | package com.ruoyi.framework.web.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.ruoyi.common.constant.CacheConstants; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.constant.UserConstants; |
| | |
| | | import com.ruoyi.framework.manager.AsyncManager; |
| | | import com.ruoyi.framework.manager.factory.AsyncFactory; |
| | | import com.ruoyi.framework.security.context.AuthenticationContextHolder; |
| | | import com.ruoyi.system.domain.TbDept; |
| | | import com.ruoyi.system.service.ISysConfigService; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import com.ruoyi.system.service.TbDeptService; |
| | |
| | | // 用户验证 |
| | | Authentication authentication = null; |
| | | // 用户验证 |
| | | SysUser user = userService.selectUserByUserName(username); |
| | | SysUser user = userService.selectPlatUserByUserName(username); |
| | | if (StringUtils.isNull(user)){ |
| | | log.info("登录用户:{} 不存在.", username); |
| | | throw new ServiceException(MessageUtils.message("user.not.exists")); |
| | |
| | | throw new ServiceException("不可用短信验证码登录"); |
| | | } |
| | | // 校验验证码 |
| | | Object cacheObject = redisCache.getCacheObject(user.getPhonenumber()); |
| | | Object cacheObject = redisCache.getCacheObject(user.getPhoneNumber()); |
| | | if(!code.equals(String.valueOf(cacheObject))){ |
| | | log.info("登录用户:{} 短信验证码错误{}", username,code); |
| | | throw new ServiceException("短信验证码错误"); |
| | |
| | | /** |
| | | * 登录验证 |
| | | * |
| | | * @param account 用户名 |
| | | * @param username 用户名 |
| | | * @param password 密码 |
| | | * @param uuid uuid |
| | | * @param loginBodyUuid |
| | | |
| | | * @return 结果 |
| | | */ |
| | | public LoginUser loginPwd(String account, String password, String code, String uuid) |
| | | public LoginUser loginPwd(String username, String password, String code, String uuid) |
| | | { |
| | | // 验证码校验 |
| | | validateCaptcha(account, code, uuid); |
| | | validateCaptcha(username, code, uuid); |
| | | // 登录前置校验 |
| | | loginPreCheck(account, password); |
| | | loginPreCheck(username, password); |
| | | // 用户验证 |
| | | Authentication authentication = null; |
| | | // 用户验证 |
| | | TbDept dept = tbDeptService.getOne(Wrappers.<TbDept>lambdaQuery().eq(TbDept::getAccount, account)); |
| | | if (StringUtils.isNull(dept)){ |
| | | log.info("登录用户:{} 不存在.", account); |
| | | SysUser user = userService.selectDeptUserByUserName(username); |
| | | if (StringUtils.isNull(user)){ |
| | | log.info("登录用户:{} 不存在.", username); |
| | | throw new ServiceException(MessageUtils.message("user.not.exists")); |
| | | } |
| | | try |
| | | { |
| | | UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(account, password); |
| | | UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); |
| | | AuthenticationContextHolder.setContext(authenticationToken); |
| | | // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername |
| | | authentication = authenticationManager.authenticate(authenticationToken); |
| | |
| | | { |
| | | if (e instanceof BadCredentialsException) |
| | | { |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(account, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"))); |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match"))); |
| | | throw new UserPasswordNotMatchException(); |
| | | } |
| | | else |
| | | { |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(account, Constants.LOGIN_FAIL, e.getMessage())); |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, e.getMessage())); |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | |
| | | { |
| | | AuthenticationContextHolder.clearContext(); |
| | | } |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(account, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); |
| | | AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"))); |
| | | LoginUser loginUser = (LoginUser) authentication.getPrincipal(); |
| | | recordLoginInfo(loginUser.getUserId()); |
| | | // 生成token |
| | |
| | | sysUser.setLoginDate(DateUtils.getNowDate()); |
| | | userService.updateUserProfile(sysUser); |
| | | } |
| | | |
| | | |
| | | } |